<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Derivante &#187; php 5.3</title>
	<atom:link href="http://www.derivante.com/tag/php-53/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.derivante.com</link>
	<description>to obtain or receive from a source</description>
	<lastBuildDate>Mon, 26 Apr 2010 18:44:42 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.2.1</generator>
<xhtml:meta xmlns:xhtml="http://www.w3.org/1999/xhtml" name="robots" content="noindex" />
		<item>
		<title>PHP Rapid Application Development with ZF1.8 and AR</title>
		<link>http://www.derivante.com/2009/05/21/php-rapid-application-development-with-zf18-and-ar/</link>
		<comments>http://www.derivante.com/2009/05/21/php-rapid-application-development-with-zf18-and-ar/#comments</comments>
		<pubDate>Thu, 21 May 2009 19:02:42 +0000</pubDate>
		<dc:creator>Clay vanSchalkwijk</dc:creator>
				<category><![CDATA[Framework]]></category>
		<category><![CDATA[Linux]]></category>
		<category><![CDATA[PHP]]></category>
		<category><![CDATA[ORM]]></category>
		<category><![CDATA[php 5.3]]></category>
		<category><![CDATA[phpactiverecord]]></category>
		<category><![CDATA[ZendFramework]]></category>
		<category><![CDATA[Zend_Tool]]></category>

		<guid isPermaLink="false">http://www.derivante.com/?p=627</guid>
		<description><![CDATA[In the days since I began programming in PHP the web has come a long way. With the 5.3 release of PHP, the OOP side of things are finally getting a much needed polish. In the past year there's been (&#8230;)</p><p><a href="http://www.derivante.com/2009/05/21/php-rapid-application-development-with-zf18-and-ar/">Read the rest of this entry &#187;</a></p>]]></description>
			<content:encoded><![CDATA[<p>In the days since I began programming in PHP the web has come a long way.  With the 5.3 release of PHP, the OOP side of things are finally getting a much needed polish.  In the past year there's been a steady rise in the usage of Ruby on Rails for web development as programmers are discovering that coming up with an idea for a site is far more enjoyable than programming one.  Rails offers a very rapid application development environment to get from concept to code in a minimal number of steps.  Unfortunately, PHP has been lacking in this regard. PHP needs a standard framework and easy database interaction. Currently, the steps required to go from concept to code brings with it a huge overhead in scaffolding development.</p>
<p><span id="more-627"></span></p>
<p>It is precisely this overhead that needs to be eliminated.  Time is money and if you are working with a new application on a weekly or monthly basis the scaffolding overhead comprises a great deal of that time.  Not to mention that most web applications today are running to and fro from a database. This time spent fetching and storing data makes up for a significant chunk in the overall development.  2009 is a big step forward for PHP and there are tools out there that tackle this problem.</p>
<p>
Enter ZendFramework 1.8, just recently released it represents Zend's commitment to make PHP a competitive environment for Rapid Application Development (totally RAD).  There are several new tools included within this release, primarily Zend_Tool, Zend_Application, and Zend_Navigation. Zend_Tool_Project allows for the automated creation of new projects complete with a ready to go application shell.  You can liken this shell to a new Hello World! project with the core structure and scaffolding setup.  Rather than going into the details of everything included within the release, you can find a thorough overview within the 1.8 release notes.</p>
<p>This brings me to what I'm really excited about. Recently we pushed out <a href="http://www.derivante.com/2009/05/14/php-activerecord-with-php-53/">PHP ActiveRecord</a> for everyone!  This tutorial goes over the steps necessary to build your environment.  You'll be able to create new projects within the ZendFramework with <a href="http://www.derivante.com/2009/05/14/php-activerecord-with-php-53/">PHP ActiveRecord</a>. I believe developers should spend their time dealing with application logic and not be burdened with configuration, bootstrapping or custom SQL.  For this tutorial I am using the following environment:</p>
<p>- Ubuntu 9.04<br />
- PHP 5.3-RC1<br />
- MySQL 5.4<br />
- Apache 2.2.11<br />
- ZendFramework 1.8 min<br />
- php-activerecord 0.9</p>
<p>For the sake of keeping this tutorial short and concise, there are already several resources out there that deal with compiling PHP 5.3.  I'll assume you've already setup your server with MySQL, PHP and Apache ready to go.  In fact, you can use nginx if you prefer going down that route or even ZendServer CE, although I personally prefer Apache. It certainly doesn't matter what you're serving out PHP with and it will probably be dependent on what you're existing environment is using.</p>
<p><strong>Installing ZendFramework 1.8 and ActiveRecord</strong></p>
<p>We're going to want both libraries in our PHP include directory.  First you should download the latest ZF release, 1.8.1 at the time of this post.   When you have it ready to go on your server it is important to make sure your environment is working.  If you installed PHP 5.3 correctly you should be able to get the following output from CLI:</p>
<pre># php -v
PHP 5.3.0RC1 (cli) (built: May  3 2009 15:31:21)
Copyright (c) 1997-2009 The PHP Group
Zend Engine v2.3.0, Copyright (c) 1998-2009 Zend Technologies
# php -i | grep include_path
include_path => .:/usr/local/php/lib => .:/usr/local/php/lib</pre>
<p>Don't worry if your include path is elsewhere, just make a note of where it is since we'll be dumping the Zend stuff there. Now to extract and install Zend:</p>
<pre># tar xzfv ZendFramework-1.8.1-minimal.tar.gz
# mv ZendFramework-1.8.1-minimal/library/Zend /usr/local/php/lib/
# mv ZendFramework-1.8.1-minimal/bin/zf.* /usr/local/php/bin/
# ln -s /usr/local/php/bin/zf.sh /usr/bin/zf
# zf show version
Zend Framework Version: 1.8.1</pre>
<p>I opted to put the zf.sh into my PHP bin directory and symlink it from my bin dir.  The next step is to download the source from <a href="http://github.com/kla/php-activerecord/">github</a> and unpack it into your include path to a directory called php-activerecord. Our dependencies are there and we can move on to our first project.</strong></p>
<p><strong>Create Project</strong></p>
<p>Now that everything is installed, we want to test out zf create project.  The following example is a bare bones project ready for us to extend:</p>
<pre># zf create project /var/www/newproject
Creating project at /var/www/newproject
# ls /var/www/newproject/
application  library  public  tests</pre>
<p>That is it, you are ready for the web.  Configure your web server to start serving out pages from the location you set for your new project.  The docroot path should be /var/www/newproject/public.  Load up your web browser and you should see your page.</p>
<p><strong>Loading up ActiveRecord</strong></p>
<p>Inside your application/Bootstrap.php you might notice the code rather bare.  The Zend Bootstrapper will load anything within the bootstrap function starting with _init*.  To tack on ActiveRecord, we simply add the following:</p>
<pre class="php"><span style="color: #000000; font-weight: bold;">class</span> Bootstrap <span style="color: #000000; font-weight: bold;">extends</span> Zend_Application_Bootstrap_Bootstrap
<span style="color: #66cc66;">&#123;</span>
    protected <span style="color: #000000; font-weight: bold;">function</span> _initActiveRecord<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>
    <span style="color: #66cc66;">&#123;</span>
        <span style="color: #b1b100;">require_once</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;php-activerecord/ActiveRecord.php&quot;</span><span style="color: #66cc66;">&#41;</span>;
        ActiveRecord\Config::<span style="color: #006600;">initialize</span><span style="color: #66cc66;">&#40;</span><span style="color: #000000; font-weight: bold;">function</span><span style="color: #66cc66;">&#40;</span><span style="color: #0000ff;">$cfg</span><span style="color: #66cc66;">&#41;</span>
        <span style="color: #66cc66;">&#123;</span>
            <span style="color: #0000ff;">$cfg</span>-&gt;<span style="color: #006600;">set_model_directory</span><span style="color: #66cc66;">&#40;</span>APPLICATION_PATH . <span style="color: #ff0000;">'/models'</span> <span style="color: #66cc66;">&#41;</span>;
            <span style="color: #0000ff;">$cfg</span>-&gt;<span style="color: #006600;">set_connections</span><span style="color: #66cc66;">&#40;</span><a style="text-decoration: none;" href="http://www.php.net/array"><span style="color: #000066;">array</span></a><span style="color: #66cc66;">&#40;</span>
                <span style="color: #ff0000;">'development'</span> =&gt; <span style="color: #ff0000;">'mysql://user:password@localhost/newproject'</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>;
        <span style="color: #66cc66;">&#125;</span><span style="color: #66cc66;">&#41;</span>;
    <span style="color: #66cc66;">&#125;</span>
<span style="color: #66cc66;">&#125;</span></pre>
<p>You will need to adjust your SQL connect strings, and at this point if you want to restructure your model directory differently you may do so.  There are only two configuration points, both of which you could pass from an XML or INI config.  Even though ActiveRecord is loaded, we still need to test out that everything is working. In order to do this we will need to create some test data:</p>
<pre class="sql"><span style="color: #993333; font-weight: bold;">CREATE</span> <span style="color: #993333; font-weight: bold;">DATABASE</span> newproject;
<span style="color: #993333; font-weight: bold;">USE</span> newproject;
<span style="color: #993333; font-weight: bold;">CREATE</span> <span style="color: #993333; font-weight: bold;">TABLE</span> books<span style="color: #66cc66;">&#40;</span>
    id int <span style="color: #993333; font-weight: bold;">NOT</span> <span style="color: #993333; font-weight: bold;">NULL</span> <span style="color: #993333; font-weight: bold;">PRIMARY</span> <span style="color: #993333; font-weight: bold;">KEY</span> <span style="color: #993333; font-weight: bold;">AUTO_INCREMENT</span>,
    name varchar<span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">50</span><span style="color: #66cc66;">&#41;</span>,
    author varchar<span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">50</span><span style="color: #66cc66;">&#41;</span>
<span style="color: #66cc66;">&#41;</span>;
<span style="color: #993333; font-weight: bold;">INSERT</span> <span style="color: #993333; font-weight: bold;">INTO</span> books<span style="color: #66cc66;">&#40;</span>id,name,author<span style="color: #66cc66;">&#41;</span> <span style="color: #993333; font-weight: bold;">VALUES</span><span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">1</span>,<span style="color: #ff0000;">'How to be Angry'</span>,<span style="color: #ff0000;">'Jax'</span><span style="color: #66cc66;">&#41;</span>;</pre>
<p>The next step is to create a model for the books table to be used from within your application. In your model directory that you set within the configuration above, create a Book.php with the following:</p>
<pre class="php"><span style="color: #000000; font-weight: bold;">class</span> Book <span style="color: #000000; font-weight: bold;">extends</span> ActiveRecord\Model <span style="color: #66cc66;">&#123;</span> <span style="color: #66cc66;">&#125;</span></pre>
<p>And finally to test out that everything is working, within your IndexController.php for indexAction add the following:</p>
<pre class="php"><span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> indexAction<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>
<span style="color: #66cc66;">&#123;</span>
    <span style="color: #0000ff;">$this</span>-&gt;<span style="color: #006600;">view</span>-&gt;<span style="color: #006600;">book</span> = Book::<span style="color: #006600;">find</span><span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">1</span><span style="color: #66cc66;">&#41;</span>;
<span style="color: #66cc66;">&#125;</span></pre>
<p>And so we can actually see the output, edit your /views/scripts/index/index.phtml with the following:</p>
<pre class="php">Today I read <span style="color: #000000; font-weight: bold;">&lt;?</span>=book-&gt;<span style="color: #006600;">name</span>?&gt; by <span style="color: #000000; font-weight: bold;">&lt;?</span>=book-&gt;<span style="color: #006600;">author</span>?&gt; and it was terrible.</pre>
<p>As you can see, you can pull the variable set by the action assigned to the view or you can query right from the view itself if you please.  If you get the following output, everything is good to go:</p>
<pre>Today I read How to be Angry by Jax and it was terrible.</pre>
<p><strong>Next Steps</strong></p>
<p>Going forward, there is plenty of room to extend this setup.  Unfortunately, the Zend_Tool_Project abstraction documents are still being completed but there are examples out there on how to add new commands to it.  One new command I am working on is the automated creation of AR models by passing a database connect string to simplify the process even further.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.derivante.com/2009/05/21/php-rapid-application-development-with-zf18-and-ar/feed/</wfw:commentRss>
		<slash:comments>11</slash:comments>
		</item>
		<item>
		<title>PHP ActiveRecord Available for Beta Testing</title>
		<link>http://www.derivante.com/2009/05/19/php-activerecord-available-for-beta-testing/</link>
		<comments>http://www.derivante.com/2009/05/19/php-activerecord-available-for-beta-testing/#comments</comments>
		<pubDate>Wed, 20 May 2009 01:09:27 +0000</pubDate>
		<dc:creator>Kien La</dc:creator>
				<category><![CDATA[PHP]]></category>
		<category><![CDATA[SQL]]></category>
		<category><![CDATA[activerecord]]></category>
		<category><![CDATA[ORM]]></category>
		<category><![CDATA[php 5.3]]></category>

		<guid isPermaLink="false">http://www.derivante.com/?p=548</guid>
		<description><![CDATA[We've been working hard to get this ready for people to start poking around in and we're happy to announce that it's now ready for public beta testing! You can grab it from http://github.com/kla/php-activerecord/. Play with it... break it... and (&#8230;)</p><p><a href="http://www.derivante.com/2009/05/19/php-activerecord-available-for-beta-testing/">Read the rest of this entry &#187;</a></p>]]></description>
			<content:encoded><![CDATA[<p>We've been working hard to get this ready for people to start poking around in and we're happy to announce that it's now ready for public beta testing! You can grab it from <a href="http://github.com/kla/php-activerecord/">http://github.com/kla/php-activerecord/</a>. Play with it... break it... and give us your feedback to help us make a better library for everyone! We want to hear from you.</p>
<p><span id="more-548"></span></p>
<h2>Quick Start</h2>
<p>We'll start first with a bare bones example to show how little you need to get up and running. There's <i>very</i> little to configure. We've adhered to the convention over configuration philosophy so there are no code generators you need to run and no xml/yaml mapping files to maintain.</p>
<pre class="php"><span style="color: #000000; font-weight: bold;">&lt;?</span>
<span style="color: #808080; font-style: italic;">// make sure the ActiveRecord project is in your current directory</span>
<span style="color: #808080; font-style: italic;">// or your include_path</span>
<span style="color: #b1b100;">require_once</span> <span style="color: #ff0000;">'ActiveRecord/ActiveRecord.php'</span>;
&nbsp;
<span style="color: #808080; font-style: italic;">// assumes a table name of &quot;books&quot; with a primay key named &quot;id&quot;</span>
<span style="color: #000000; font-weight: bold;">class</span> Book <span style="color: #000000; font-weight: bold;">extends</span> ActiveRecord\Model <span style="color: #66cc66;">&#123;</span> <span style="color: #66cc66;">&#125;</span>
&nbsp;
<span style="color: #808080; font-style: italic;">// initialize ActiveRecord</span>
ActiveRecord\Config::<span style="color: #006600;">initialize</span><span style="color: #66cc66;">&#40;</span><span style="color: #000000; font-weight: bold;">function</span><span style="color: #66cc66;">&#40;</span><span style="color: #0000ff;">$cfg</span><span style="color: #66cc66;">&#41;</span>
<span style="color: #66cc66;">&#123;</span>
    <span style="color: #0000ff;">$cfg</span>-&gt;<span style="color: #006600;">set_model_directory</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">'.'</span><span style="color: #66cc66;">&#41;</span>;
    <span style="color: #0000ff;">$cfg</span>-&gt;<span style="color: #006600;">set_connections</span><span style="color: #66cc66;">&#40;</span><a style="text-decoration: none;" href="http://www.php.net/array"><span style="color: #000066;">array</span></a><span style="color: #66cc66;">&#40;</span>
        <span style="color: #ff0000;">'development'</span> =&gt; <span style="color: #ff0000;">'mysql://username:password@localhost/database_name'</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>;
<span style="color: #66cc66;">&#125;</span><span style="color: #66cc66;">&#41;</span>;
&nbsp;
<a style="text-decoration: none;" href="http://www.php.net/print_r"><span style="color: #000066;">print_r</span></a><span style="color: #66cc66;">&#40;</span>Book::<span style="color: #006600;">first</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>-&gt;<span style="color: #006600;">attributes</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>;
<span style="color: #000000; font-weight: bold;">?&gt;</span></pre>
<p>That's it! The code for this is located in the examples/simple directory. You can run it with "php examples/simple/simple.php". Make sure to modify the connection string to suit your system and run the simple.sql script appropriately.</p>
<h2>Serious Business Time</h2>
<p>Now for a not so completely trivial example. This example will simulate a very dumb ordering and payment model. See the examples/orders directory for the source. You can run this sample by executing "php examples/orders/orders.php". Again, make sure you modify the connection string to suit your system and be sure the schema in orders.sql has been created in your test database.</p>
<p>First, let's look at the models:</p>
<pre class="php"><span style="color: #000000; font-weight: bold;">class</span> Person <span style="color: #000000; font-weight: bold;">extends</span> ActiveRecord\Model
<span style="color: #66cc66;">&#123;</span>
    <span style="color: #808080; font-style: italic;">// a person can have many orders and payments</span>
    <a style="text-decoration: none;" href="http://www.php.net/static"><span style="color: #000066;">static</span></a> <span style="color: #0000ff;">$has_many</span> = <a style="text-decoration: none;" href="http://www.php.net/array"><span style="color: #000066;">array</span></a><span style="color: #66cc66;">&#40;</span>
        <a style="text-decoration: none;" href="http://www.php.net/array"><span style="color: #000066;">array</span></a><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">'orders'</span><span style="color: #66cc66;">&#41;</span>,
        <a style="text-decoration: none;" href="http://www.php.net/array"><span style="color: #000066;">array</span></a><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">'payments'</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>;
&nbsp;
    <span style="color: #808080; font-style: italic;">// must have a name and a state which has a custom error message</span>
    <a style="text-decoration: none;" href="http://www.php.net/static"><span style="color: #000066;">static</span></a> <span style="color: #0000ff;">$validates_presence_of</span> = <a style="text-decoration: none;" href="http://www.php.net/array"><span style="color: #000066;">array</span></a><span style="color: #66cc66;">&#40;</span>
        <a style="text-decoration: none;" href="http://www.php.net/array"><span style="color: #000066;">array</span></a><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">'name'</span><span style="color: #66cc66;">&#41;</span>, <a style="text-decoration: none;" href="http://www.php.net/array"><span style="color: #000066;">array</span></a><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">'state'</span>, <span style="color: #ff0000;">'message'</span> =&gt; <span style="color: #ff0000;">'Where do you live then?'</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>;
<span style="color: #66cc66;">&#125;</span></pre>
<pre class="php"><span style="color: #000000; font-weight: bold;">class</span> Order <span style="color: #000000; font-weight: bold;">extends</span> ActiveRecord\Model
<span style="color: #66cc66;">&#123;</span>
    <span style="color: #808080; font-style: italic;">// order belongs to a person</span>
    <a style="text-decoration: none;" href="http://www.php.net/static"><span style="color: #000066;">static</span></a> <span style="color: #0000ff;">$belongs_to</span> = <a style="text-decoration: none;" href="http://www.php.net/array"><span style="color: #000066;">array</span></a><span style="color: #66cc66;">&#40;</span>
        <a style="text-decoration: none;" href="http://www.php.net/array"><span style="color: #000066;">array</span></a><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">'person'</span><span style="color: #66cc66;">&#41;</span>,
        <a style="text-decoration: none;" href="http://www.php.net/array"><span style="color: #000066;">array</span></a><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">'order'</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>;
&nbsp;
    <span style="color: #808080; font-style: italic;">// order can have many payments by many people</span>
    <span style="color: #808080; font-style: italic;">// the conditions is just there as an example as it makes no logical sense</span>
    <a style="text-decoration: none;" href="http://www.php.net/static"><span style="color: #000066;">static</span></a> <span style="color: #0000ff;">$has_many</span> = <a style="text-decoration: none;" href="http://www.php.net/array"><span style="color: #000066;">array</span></a><span style="color: #66cc66;">&#40;</span>
        <a style="text-decoration: none;" href="http://www.php.net/array"><span style="color: #000066;">array</span></a><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">'payments'</span><span style="color: #66cc66;">&#41;</span>,
        <a style="text-decoration: none;" href="http://www.php.net/array"><span style="color: #000066;">array</span></a><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">'people'</span>,
            <span style="color: #ff0000;">'through'</span>    =&gt; <span style="color: #ff0000;">'payments'</span>,
            <span style="color: #ff0000;">'select'</span>     =&gt; <span style="color: #ff0000;">'people.*, payments.amount'</span>,
            <span style="color: #ff0000;">'conditions'</span> =&gt; <span style="color: #ff0000;">'payments.amount &lt; 200'</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>;
&nbsp;
    <span style="color: #808080; font-style: italic;">// order must have a price and tax &gt; 0</span>
    <a style="text-decoration: none;" href="http://www.php.net/static"><span style="color: #000066;">static</span></a> <span style="color: #0000ff;">$validates_numericality_of</span> = <a style="text-decoration: none;" href="http://www.php.net/array"><span style="color: #000066;">array</span></a><span style="color: #66cc66;">&#40;</span>
        <a style="text-decoration: none;" href="http://www.php.net/array"><span style="color: #000066;">array</span></a><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">'price'</span>, <span style="color: #ff0000;">'greater_than'</span> =&gt; <span style="color: #cc66cc;">0</span><span style="color: #66cc66;">&#41;</span>,
        <a style="text-decoration: none;" href="http://www.php.net/array"><span style="color: #000066;">array</span></a><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">'tax'</span>,   <span style="color: #ff0000;">'greater_than'</span> =&gt; <span style="color: #cc66cc;">0</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>;
&nbsp;
    <span style="color: #808080; font-style: italic;">// setup a callback to automatically apply a tax</span>
    <a style="text-decoration: none;" href="http://www.php.net/static"><span style="color: #000066;">static</span></a> <span style="color: #0000ff;">$before_validation_on_create</span> = <a style="text-decoration: none;" href="http://www.php.net/array"><span style="color: #000066;">array</span></a><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">'apply_tax'</span><span style="color: #66cc66;">&#41;</span>;
&nbsp;
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> apply_tax<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>
    <span style="color: #66cc66;">&#123;</span>
        <span style="color: #b1b100;">if</span> <span style="color: #66cc66;">&#40;</span><span style="color: #0000ff;">$this</span>-&gt;<span style="color: #006600;">person</span>-&gt;<span style="color: #006600;">state</span> == <span style="color: #ff0000;">'VA'</span><span style="color: #66cc66;">&#41;</span>
            <span style="color: #0000ff;">$tax</span> = <span style="color: #cc66cc;">0.045</span>;
        <span style="color: #b1b100;">elseif</span> <span style="color: #66cc66;">&#40;</span><span style="color: #0000ff;">$this</span>-&gt;<span style="color: #006600;">person</span>-&gt;<span style="color: #006600;">state</span> == <span style="color: #ff0000;">'CA'</span><span style="color: #66cc66;">&#41;</span>
            <span style="color: #0000ff;">$tax</span> = <span style="color: #cc66cc;">0.10</span>;
        <span style="color: #b1b100;">else</span>
            <span style="color: #0000ff;">$tax</span> = <span style="color: #cc66cc;">0.02</span>;
&nbsp;
        <span style="color: #0000ff;">$this</span>-&gt;<span style="color: #006600;">tax</span> = <span style="color: #0000ff;">$this</span>-&gt;<span style="color: #006600;">price</span> * <span style="color: #0000ff;">$tax</span>;
    <span style="color: #66cc66;">&#125;</span>
<span style="color: #66cc66;">&#125;</span></pre>
<pre class="php"><span style="color: #000000; font-weight: bold;">class</span> Payment <span style="color: #000000; font-weight: bold;">extends</span> ActiveRecord\Model
<span style="color: #66cc66;">&#123;</span>
    <span style="color: #808080; font-style: italic;">// payment belongs to a person</span>
    <a style="text-decoration: none;" href="http://www.php.net/static"><span style="color: #000066;">static</span></a> <span style="color: #0000ff;">$belongs_to</span> = <a style="text-decoration: none;" href="http://www.php.net/array"><span style="color: #000066;">array</span></a><span style="color: #66cc66;">&#40;</span>
        <a style="text-decoration: none;" href="http://www.php.net/array"><span style="color: #000066;">array</span></a><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">'person'</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>;
<span style="color: #66cc66;">&#125;</span></pre>
<p>And here's the code that does everything:</p>
<pre class="php"><span style="color: #000000; font-weight: bold;">&lt;?</span>
<span style="color: #b1b100;">require_once</span> <a style="text-decoration: none;" href="http://www.php.net/dirname"><span style="color: #000066;">dirname</span></a><span style="color: #66cc66;">&#40;</span><span style="color: #000000; font-weight: bold;">__FILE__</span><span style="color: #66cc66;">&#41;</span> . <span style="color: #ff0000;">'/../../ActiveRecord.php'</span>;
&nbsp;
<span style="color: #808080; font-style: italic;">// initialize ActiveRecord</span>
ActiveRecord\Config::<span style="color: #006600;">initialize</span><span style="color: #66cc66;">&#40;</span><span style="color: #000000; font-weight: bold;">function</span><span style="color: #66cc66;">&#40;</span><span style="color: #0000ff;">$cfg</span><span style="color: #66cc66;">&#41;</span>
<span style="color: #66cc66;">&#123;</span>
    <span style="color: #0000ff;">$cfg</span>-&gt;<span style="color: #006600;">set_model_directory</span><span style="color: #66cc66;">&#40;</span><a style="text-decoration: none;" href="http://www.php.net/dirname"><span style="color: #000066;">dirname</span></a><span style="color: #66cc66;">&#40;</span><span style="color: #000000; font-weight: bold;">__FILE__</span><span style="color: #66cc66;">&#41;</span> . <span style="color: #ff0000;">'/models'</span><span style="color: #66cc66;">&#41;</span>;
    <span style="color: #0000ff;">$cfg</span>-&gt;<span style="color: #006600;">set_connections</span><span style="color: #66cc66;">&#40;</span><a style="text-decoration: none;" href="http://www.php.net/array"><span style="color: #000066;">array</span></a><span style="color: #66cc66;">&#40;</span>
        <span style="color: #ff0000;">'development'</span> =&gt; <span style="color: #ff0000;">'mysql://test:test@127.0.0.1/orders_test'</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>;
&nbsp;
    <span style="color: #808080; font-style: italic;">// you can change the default connection with the below</span>
    <span style="color: #808080; font-style: italic;">//$cfg-&gt;set_default_connection('production');</span>
<span style="color: #66cc66;">&#125;</span><span style="color: #66cc66;">&#41;</span>;
&nbsp;
<span style="color: #808080; font-style: italic;">// create some people</span>
<span style="color: #0000ff;">$jax</span> = <span style="color: #000000; font-weight: bold;">new</span> Person<span style="color: #66cc66;">&#40;</span><a style="text-decoration: none;" href="http://www.php.net/array"><span style="color: #000066;">array</span></a><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">'name'</span> =&gt; <span style="color: #ff0000;">'Jax'</span>, <span style="color: #ff0000;">'state'</span> =&gt; <span style="color: #ff0000;">'CA'</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>;
<span style="color: #0000ff;">$jax</span>-&gt;<span style="color: #006600;">save</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
&nbsp;
<span style="color: #808080; font-style: italic;">// compact way to create and save a model</span>
<span style="color: #0000ff;">$tito</span> = Person::<span style="color: #006600;">create</span><span style="color: #66cc66;">&#40;</span><a style="text-decoration: none;" href="http://www.php.net/array"><span style="color: #000066;">array</span></a><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">'name'</span> =&gt; <span style="color: #ff0000;">'Tito'</span>, <span style="color: #ff0000;">'state'</span> =&gt; <span style="color: #ff0000;">'VA'</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>;
&nbsp;
<span style="color: #808080; font-style: italic;">// place orders. tax is automatically applied in a callback</span>
<span style="color: #808080; font-style: italic;">// create_orders will automatically place the created model into $tito-&gt;orders</span>
<span style="color: #808080; font-style: italic;">// even if it failed validation</span>
<span style="color: #0000ff;">$pokemon</span> = <span style="color: #0000ff;">$tito</span>-&gt;<span style="color: #006600;">create_orders</span><span style="color: #66cc66;">&#40;</span><a style="text-decoration: none;" href="http://www.php.net/array"><span style="color: #000066;">array</span></a><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">'item_name'</span> =&gt; <span style="color: #ff0000;">'Live Pokemon'</span>, <span style="color: #ff0000;">'price'</span> =&gt; <span style="color: #cc66cc;">6999.99</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>;
<span style="color: #0000ff;">$coal</span>    = <span style="color: #0000ff;">$tito</span>-&gt;<span style="color: #006600;">create_orders</span><span style="color: #66cc66;">&#40;</span><a style="text-decoration: none;" href="http://www.php.net/array"><span style="color: #000066;">array</span></a><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">'item_name'</span> =&gt; <span style="color: #ff0000;">'Lump of Coal'</span>, <span style="color: #ff0000;">'price'</span> =&gt; <span style="color: #cc66cc;">100.00</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>;
<span style="color: #0000ff;">$freebie</span> = <span style="color: #0000ff;">$tito</span>-&gt;<span style="color: #006600;">create_orders</span><span style="color: #66cc66;">&#40;</span><a style="text-decoration: none;" href="http://www.php.net/array"><span style="color: #000066;">array</span></a><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">'item_name'</span> =&gt; <span style="color: #ff0000;">'Freebie'</span>, <span style="color: #ff0000;">'price'</span> =&gt; <span style="color: #cc66cc;">-100.99</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>;
&nbsp;
<span style="color: #b1b100;">if</span> <span style="color: #66cc66;">&#40;</span><a style="text-decoration: none;" href="http://www.php.net/count"><span style="color: #000066;">count</span></a><span style="color: #66cc66;">&#40;</span><span style="color: #0000ff;">$freebie</span>-&gt;<span style="color: #006600;">errors</span><span style="color: #66cc66;">&#41;</span> &gt; <span style="color: #cc66cc;">0</span><span style="color: #66cc66;">&#41;</span>
<span style="color: #66cc66;">&#123;</span>
    <a style="text-decoration: none;" href="http://www.php.net/echo"><span style="color: #000066;">echo</span></a> <span style="color: #ff0000;">&quot;[FAILED] saving order $freebie-&gt;item_name: &quot;</span> .
        <a style="text-decoration: none;" href="http://www.php.net/join"><span style="color: #000066;">join</span></a><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">','</span>,<span style="color: #0000ff;">$freebie</span>-&gt;<span style="color: #006600;">errors</span>-&gt;<span style="color: #006600;">full_messages</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span> . <span style="color: #ff0000;">&quot;<span style="color: #000099; font-weight: bold;">\n</span><span style="color: #000099; font-weight: bold;">\n</span>&quot;</span>;
<span style="color: #66cc66;">&#125;</span>
&nbsp;
<span style="color: #808080; font-style: italic;">// payments</span>
<span style="color: #0000ff;">$pokemon</span>-&gt;<span style="color: #006600;">create_payments</span><span style="color: #66cc66;">&#40;</span><a style="text-decoration: none;" href="http://www.php.net/array"><span style="color: #000066;">array</span></a><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">'amount'</span> =&gt; <span style="color: #cc66cc;">1.99</span>, <span style="color: #ff0000;">'person_id'</span> =&gt; <span style="color: #0000ff;">$tito</span>-&gt;<span style="color: #006600;">id</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>;
<span style="color: #0000ff;">$pokemon</span>-&gt;<span style="color: #006600;">create_payments</span><span style="color: #66cc66;">&#40;</span><a style="text-decoration: none;" href="http://www.php.net/array"><span style="color: #000066;">array</span></a><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">'amount'</span> =&gt; <span style="color: #cc66cc;">4999.50</span>, <span style="color: #ff0000;">'person_id'</span> =&gt; <span style="color: #0000ff;">$tito</span>-&gt;<span style="color: #006600;">id</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>;
<span style="color: #0000ff;">$pokemon</span>-&gt;<span style="color: #006600;">create_payments</span><span style="color: #66cc66;">&#40;</span><a style="text-decoration: none;" href="http://www.php.net/array"><span style="color: #000066;">array</span></a><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">'amount'</span> =&gt; <span style="color: #cc66cc;">2.50</span>, <span style="color: #ff0000;">'person_id'</span> =&gt; <span style="color: #0000ff;">$jax</span>-&gt;<span style="color: #006600;">id</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>;
&nbsp;
<span style="color: #808080; font-style: italic;">// reload since we don't want the freebie to show up (because it failed validation)</span>
<span style="color: #0000ff;">$tito</span>-&gt;<span style="color: #006600;">reload</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
&nbsp;
<a style="text-decoration: none;" href="http://www.php.net/echo"><span style="color: #000066;">echo</span></a> <span style="color: #ff0000;">&quot;$tito-&gt;name has &quot;</span> . <a style="text-decoration: none;" href="http://www.php.net/count"><span style="color: #000066;">count</span></a><span style="color: #66cc66;">&#40;</span><span style="color: #0000ff;">$tito</span>-&gt;<span style="color: #006600;">orders</span><span style="color: #66cc66;">&#41;</span> . <span style="color: #ff0000;">&quot; orders for: &quot;</span> .
    <a style="text-decoration: none;" href="http://www.php.net/join"><span style="color: #000066;">join</span></a><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">', '</span>,ActiveRecord\collect<span style="color: #66cc66;">&#40;</span><span style="color: #0000ff;">$tito</span>-&gt;<span style="color: #006600;">orders</span>,<span style="color: #ff0000;">'item_name'</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span> . <span style="color: #ff0000;">&quot;<span style="color: #000099; font-weight: bold;">\n</span><span style="color: #000099; font-weight: bold;">\n</span>&quot;</span>;
&nbsp;
<span style="color: #808080; font-style: italic;">// get all orders placed by Tito</span>
<span style="color: #b1b100;">foreach</span> <span style="color: #66cc66;">&#40;</span>Order::<span style="color: #006600;">find_all_by_person_id</span><span style="color: #66cc66;">&#40;</span><span style="color: #0000ff;">$tito</span>-&gt;<span style="color: #006600;">id</span><span style="color: #66cc66;">&#41;</span> <span style="color: #b1b100;">as</span> <span style="color: #0000ff;">$order</span><span style="color: #66cc66;">&#41;</span>
<span style="color: #66cc66;">&#123;</span>
    <a style="text-decoration: none;" href="http://www.php.net/echo"><span style="color: #000066;">echo</span></a> <span style="color: #ff0000;">&quot;Order #$order-&gt;id for $order-&gt;item_name &quot;</span> .
        <span style="color: #ff0000;">&quot;($$order-&gt;price + $$order-&gt;tax tax) &quot;</span> .
        <span style="color: #ff0000;">&quot;ordered by &quot;</span> . <span style="color: #0000ff;">$order</span>-&gt;<span style="color: #006600;">person</span>-&gt;<span style="color: #006600;">name</span> . <span style="color: #ff0000;">&quot;<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span>;
&nbsp;
    <span style="color: #b1b100;">if</span> <span style="color: #66cc66;">&#40;</span><a style="text-decoration: none;" href="http://www.php.net/count"><span style="color: #000066;">count</span></a><span style="color: #66cc66;">&#40;</span><span style="color: #0000ff;">$order</span>-&gt;<span style="color: #006600;">payments</span><span style="color: #66cc66;">&#41;</span> &gt; <span style="color: #cc66cc;">0</span><span style="color: #66cc66;">&#41;</span>
    <span style="color: #66cc66;">&#123;</span>
        <span style="color: #808080; font-style: italic;">// display each payment for this order</span>
        <span style="color: #b1b100;">foreach</span> <span style="color: #66cc66;">&#40;</span><span style="color: #0000ff;">$order</span>-&gt;<span style="color: #006600;">payments</span> <span style="color: #b1b100;">as</span> <span style="color: #0000ff;">$payment</span><span style="color: #66cc66;">&#41;</span>
        <span style="color: #66cc66;">&#123;</span>
            <a style="text-decoration: none;" href="http://www.php.net/echo"><span style="color: #000066;">echo</span></a> <span style="color: #ff0000;">&quot;  payment #$payment-&gt;id of $$payment-&gt;amount by &quot;</span> .
            <span style="color: #0000ff;">$payment</span>-&gt;<span style="color: #006600;">person</span>-&gt;<span style="color: #006600;">name</span> . <span style="color: #ff0000;">&quot;<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span>;
        <span style="color: #66cc66;">&#125;</span>
    <span style="color: #66cc66;">&#125;</span>
    <span style="color: #b1b100;">else</span>
        <a style="text-decoration: none;" href="http://www.php.net/echo"><span style="color: #000066;">echo</span></a> <span style="color: #ff0000;">&quot;  no payments<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span>;
&nbsp;
    <a style="text-decoration: none;" href="http://www.php.net/echo"><span style="color: #000066;">echo</span></a> <span style="color: #ff0000;">&quot;<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span>;
<span style="color: #66cc66;">&#125;</span>
&nbsp;
<span style="color: #808080; font-style: italic;">// display summary of all payments made by Tito and Jax</span>
<span style="color: #0000ff;">$conditions</span> = <a style="text-decoration: none;" href="http://www.php.net/array"><span style="color: #000066;">array</span></a><span style="color: #66cc66;">&#40;</span>
    <span style="color: #ff0000;">'conditions'</span> =&gt; <a style="text-decoration: none;" href="http://www.php.net/array"><span style="color: #000066;">array</span></a><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">'id IN(?)'</span>,<a style="text-decoration: none;" href="http://www.php.net/array"><span style="color: #000066;">array</span></a><span style="color: #66cc66;">&#40;</span><span style="color: #0000ff;">$tito</span>-&gt;<span style="color: #006600;">id</span>,<span style="color: #0000ff;">$jax</span>-&gt;<span style="color: #006600;">id</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>,
    <span style="color: #ff0000;">'order'</span>      =&gt; <span style="color: #ff0000;">'name desc'</span><span style="color: #66cc66;">&#41;</span>;
&nbsp;
<span style="color: #b1b100;">foreach</span> <span style="color: #66cc66;">&#40;</span>Person::<span style="color: #006600;">all</span><span style="color: #66cc66;">&#40;</span><span style="color: #0000ff;">$conditions</span><span style="color: #66cc66;">&#41;</span> <span style="color: #b1b100;">as</span> <span style="color: #0000ff;">$person</span><span style="color: #66cc66;">&#41;</span>
<span style="color: #66cc66;">&#123;</span>
    <span style="color: #0000ff;">$n</span> = <a style="text-decoration: none;" href="http://www.php.net/count"><span style="color: #000066;">count</span></a><span style="color: #66cc66;">&#40;</span><span style="color: #0000ff;">$person</span>-&gt;<span style="color: #006600;">payments</span><span style="color: #66cc66;">&#41;</span>;
    <span style="color: #0000ff;">$total</span> = <a style="text-decoration: none;" href="http://www.php.net/array_sum"><span style="color: #000066;">array_sum</span></a><span style="color: #66cc66;">&#40;</span>ActiveRecord\collect<span style="color: #66cc66;">&#40;</span><span style="color: #0000ff;">$person</span>-&gt;<span style="color: #006600;">payments</span>,<span style="color: #ff0000;">'amount'</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>;
    <a style="text-decoration: none;" href="http://www.php.net/echo"><span style="color: #000066;">echo</span></a> <span style="color: #ff0000;">&quot;$person-&gt;name made $n payments for a total of $$total<span style="color: #000099; font-weight: bold;">\n</span><span style="color: #000099; font-weight: bold;">\n</span>&quot;</span>;
<span style="color: #66cc66;">&#125;</span>
&nbsp;
<span style="color: #808080; font-style: italic;">// using order has_many people through payments with options</span>
<span style="color: #808080; font-style: italic;">//</span>
<span style="color: #808080; font-style: italic;">// array('people',</span>
<span style="color: #808080; font-style: italic;">//    'through' =&gt; 'payments',</span>
<span style="color: #808080; font-style: italic;">//    'select' =&gt; 'people.*, payments.amount',</span>
<span style="color: #808080; font-style: italic;">//    'conditions' =&gt; 'payments.amount &lt; 200'));</span>
<span style="color: #808080; font-style: italic;">//</span>
<span style="color: #808080; font-style: italic;">// this means our people in the loop below also has the payment information since</span>
<span style="color: #808080; font-style: italic;">// it is part of an inner join we will only see 2 of the people instead of 3</span>
<span style="color: #808080; font-style: italic;">// because 1 of the payments is greater than 200</span>
<span style="color: #0000ff;">$order</span> = Order::<span style="color: #006600;">find</span><span style="color: #66cc66;">&#40;</span><span style="color: #0000ff;">$pokemon</span>-&gt;<span style="color: #006600;">id</span><span style="color: #66cc66;">&#41;</span>;
<a style="text-decoration: none;" href="http://www.php.net/echo"><span style="color: #000066;">echo</span></a> <span style="color: #ff0000;">&quot;Order #$order-&gt;id for $order-&gt;item_name ($$order-&gt;price + $$order-&gt;tax tax)<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span>;
&nbsp;
<span style="color: #b1b100;">foreach</span> <span style="color: #66cc66;">&#40;</span><span style="color: #0000ff;">$order</span>-&gt;<span style="color: #006600;">people</span> <span style="color: #b1b100;">as</span> <span style="color: #0000ff;">$person</span><span style="color: #66cc66;">&#41;</span>
    <a style="text-decoration: none;" href="http://www.php.net/echo"><span style="color: #000066;">echo</span></a> <span style="color: #ff0000;">&quot;  payment of $$person-&gt;amount by &quot;</span> . <span style="color: #0000ff;">$person</span>-&gt;<span style="color: #006600;">name</span> . <span style="color: #ff0000;">&quot;<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span>;
<span style="color: #000000; font-weight: bold;">?&gt;</span></pre>
<p>The orders example should produce the following output:</p>
<pre>[FAILED] saving order Freebie: Price must be greater than 0, Tax must be greater than 0

Tito has 2 orders for: Live Pokemon, Lump of Coal

Order #3 for Live Pokemon ($6999.99 + $315 tax) ordered by Tito
  payment #4 of $1.99 by Tito
  payment #5 of $4999.5 by Tito
  payment #6 of $2.5 by Jax

Order #4 for Lump of Coal ($100 + $4.5 tax) ordered by Tito
  no payments

Tito made 2 payments for a total of $5001.49

Jax made 1 payments for a total of $2.5

Order #3 for Live Pokemon ($6999.99 + $315 tax)
  payment of $2.50 by Jax
  payment of $1.99 by Tito</pre>
<h2>EXTENDED CONFIGURATION</h2>
<p>Here's one more example that is basically the same as the first, but the point here is to display some of the ways to extend configuration (while remaining close to convention).</p>
<pre class="php"><span style="color: #000000; font-weight: bold;">class</span> Book <span style="color: #000000; font-weight: bold;">extends</span> ActiveRecord\Model
<span style="color: #66cc66;">&#123;</span>
    <span style="color: #808080; font-style: italic;">// explicit table name since our table is not &quot;books&quot;</span>
    <a style="text-decoration: none;" href="http://www.php.net/static"><span style="color: #000066;">static</span></a> <span style="color: #0000ff;">$table_name</span> = <span style="color: #ff0000;">'simple_book'</span>;
&nbsp;
    <span style="color: #808080; font-style: italic;">// explicit pk since our pk is not &quot;id&quot;</span>
    <a style="text-decoration: none;" href="http://www.php.net/static"><span style="color: #000066;">static</span></a> <span style="color: #0000ff;">$primary_key</span> = <span style="color: #ff0000;">'book_id'</span>;
&nbsp;
    <span style="color: #808080; font-style: italic;">// explicit connection name since we always want production with this model</span>
    <a style="text-decoration: none;" href="http://www.php.net/static"><span style="color: #000066;">static</span></a> <span style="color: #0000ff;">$connection</span> = <span style="color: #ff0000;">'production'</span>;
&nbsp;
    <span style="color: #808080; font-style: italic;">// explicit database name will generate sql like so =&gt; db.table_name</span>
    <a style="text-decoration: none;" href="http://www.php.net/static"><span style="color: #000066;">static</span></a> <span style="color: #0000ff;">$db</span> = <span style="color: #ff0000;">'test'</span>;
<span style="color: #66cc66;">&#125;</span>
&nbsp;
<span style="color: #808080; font-style: italic;">// ActiveRecord allows the use of multiple connections</span>
<span style="color: #0000ff;">$connections</span> = <a style="text-decoration: none;" href="http://www.php.net/array"><span style="color: #000066;">array</span></a><span style="color: #66cc66;">&#40;</span>
    <span style="color: #ff0000;">'development'</span> =&gt; <span style="color: #ff0000;">'mysql://test:test@127.0.0.1/development'</span>,
    <span style="color: #ff0000;">'production'</span> =&gt; <span style="color: #ff0000;">'mysql://test:test@127.0.0.1/production'</span>
<span style="color: #66cc66;">&#41;</span>;
&nbsp;
<span style="color: #808080; font-style: italic;">// initialize ActiveRecord</span>
ActiveRecord\Config::<span style="color: #006600;">initialize</span><span style="color: #66cc66;">&#40;</span><span style="color: #000000; font-weight: bold;">function</span><span style="color: #66cc66;">&#40;</span><span style="color: #0000ff;">$cfg</span><span style="color: #66cc66;">&#41;</span> use <span style="color: #66cc66;">&#40;</span><span style="color: #0000ff;">$connections</span><span style="color: #66cc66;">&#41;</span>
<span style="color: #66cc66;">&#123;</span>
    <span style="color: #0000ff;">$cfg</span>-&gt;<span style="color: #006600;">set_model_directory</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">'.'</span><span style="color: #66cc66;">&#41;</span>;
    <span style="color: #0000ff;">$cfg</span>-&gt;<span style="color: #006600;">set_connections</span><span style="color: #66cc66;">&#40;</span><span style="color: #0000ff;">$connections</span><span style="color: #66cc66;">&#41;</span>;
<span style="color: #66cc66;">&#125;</span><span style="color: #66cc66;">&#41;</span>;
&nbsp;
<a style="text-decoration: none;" href="http://www.php.net/print_r"><span style="color: #000066;">print_r</span></a><span style="color: #66cc66;">&#40;</span>Book::<span style="color: #006600;">first</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>-&gt;<span style="color: #006600;">attributes</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>;
&nbsp;</pre>
<h2>Simplicity</h2>
<p>From the configuration, all the way to a <i>serious-business</i> example, ActiveRecord takes care of the heavy-lifting and the gritty details for you. This allows you, the developer, to focus more on business logic and complex code instead of composing custom SQL queries or designing ways to handle your data. Because we have embraced a convention over configuration philosophy, using our library is not painful. The conventions are easy to remember which will also contribute to stream-lining your productivity as a developer.</p>
<h2>Where can you find ActiveRecord?</h2>
<p>Again, the code is hosted on <a href="http://github.com/kla/php-activerecord/">http://github.com/kla/php-activerecord/</a>. We also have a <a href="http://www.phpactiverecord.org/">website</a> which will be available in the near future. We hope to have all the necessary content such as: tutorials, screencasts, and documentation.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.derivante.com/2009/05/19/php-activerecord-available-for-beta-testing/feed/</wfw:commentRss>
		<slash:comments>35</slash:comments>
		</item>
		<item>
		<title>PHP ActiveRecord with PHP 5.3</title>
		<link>http://www.derivante.com/2009/05/14/php-activerecord-with-php-53/</link>
		<comments>http://www.derivante.com/2009/05/14/php-activerecord-with-php-53/#comments</comments>
		<pubDate>Thu, 14 May 2009 04:21:34 +0000</pubDate>
		<dc:creator>Jacques Fuentes</dc:creator>
				<category><![CDATA[PHP]]></category>
		<category><![CDATA[activerecord]]></category>
		<category><![CDATA[ORM]]></category>
		<category><![CDATA[php 5.3]]></category>

		<guid isPermaLink="false">http://www.derivante.com/?p=391</guid>
		<description><![CDATA[Update! Find the latest here. PHP 5.3 gets ActiveRecord! A quick search to find an implementation of active record for php on google is discouraging when one considers the state of ActiveRecord for Ruby on Rails. The reader will notice (&#8230;)</p><p><a href="http://www.derivante.com/2009/05/14/php-activerecord-with-php-53/">Read the rest of this entry &#187;</a></p>]]></description>
			<content:encoded><![CDATA[<h3>Update!</h3>
<p>Find the latest <a href="http://www.derivante.com/2009/05/19/php-activerecord-available-for-beta-testing/">here</a>. </p>
<h2>PHP 5.3 gets ActiveRecord!</h2>
<p><img height="51" width="95" alt="php-med-trans-light" src="http://www.derivante.com/wp-content/uploads/2009/05/php-med-trans-light.gif" title="php-med-trans-light" style="margin: 15px;" class="alignleft size-full wp-image-425"/>A quick search to find an implementation of active record for php on <a title="google" href="http://www.google.com/search?q=php+activerecord" target="_blank">google</a> is discouraging when one considers the state of ActiveRecord for Ruby on Rails. The reader will notice that the top results are from very old posts and the rest of the results preview minimial implementations. Of course, eventually, PHP will see a robust active record similar to RoR. Fortunately, that time is now, thanks to <a href="http://downloads.php.net/johannes/php-5.3.0RC2.tar.gz">PHP 5.3</a> and the beneficial <a href="http://wiki.php.net/doc/scratchpad/upgrade/53">new</a> <a href="http://www.sitepoint.com/article/whats-new-php-5-3/">features</a>: closures, late static binding, and <a href="http://us2.php.net/manual/en/language.namespaces.rationale.php">namespaces</a>.</p>
<p>My friend Kien and I have improved upon an earlier version of an ORM that he had written prior to PHP 5.3. The ActiveRecord we have created is inspired by Ruby on Rails and we have tried to maintain their conventions while deviating mainly because of convenience or necessity. Our main goal for this project has been to allow PHP developers tackle larger projects with greater agility. However, we also hope that use of this resource will push the PHP community further by learning the wonderful benefits of the Ruby on Rails stack. Enough with the rambling, let's get to the interesting piece!</p>
<p><span id="more-391"></span></p>
<h2>Overview</h2>
<p>Allow me to reiterate the fact that we have tried to maintain similarity between our implementation and rails' ActiveRecord to avoid headaches and increase programmer bandwidth. Keeping this similarity in mind, we have tried to re-produce many of the features. Here is a list of those features:</p>
<ul>
<li><a href="#finder_methods">Finder methods</a></li>
<li><a href="#dynamic_methods">Dynamic finder methods</a></li>
<li><a href="#writer_methods">Writer methods</a></li>
<li><a href="#relationships">Relationships</a></li>
<li><a href="#validations">Validations</a></li>
<li><a href="#callbacks">Callbacks</a></li>
<li><a href="#serializations">Serializations</a></li>
<li><a href="#adapters">Support for multiple adapters</a></li>
<li><a href="#misc_options">Miscellaneous options</a></li>
</ul>
<p><br/><br />
There are other features such as named scopes, additional adapters, <strong>transactions</strong> (something we want sooner than later), and a few others we hope to add in the future, but we believe this is a great start. We are hoping to have the code hosted on launchpad or the like within a week or two and a website with documentation. Be sure to check back here shortly for updates!</p>
<h2> Configuration </h2>
<p>Setup is very easy and straight-forward. There are essentially only two configuration points you must concern yourself with:</p>
<ul>
<li>Setting the model auto_load directory.</li>
<li>Configuring your database connections</li>
</ul>
<p>Examples:<br />
<code></p>
<pre class="php">&nbsp;
ActiveRecord\Config::<span style="color: #006600;">initialize</span><span style="color: #66cc66;">&#40;</span><span style="color: #000000; font-weight: bold;">function</span><span style="color: #66cc66;">&#40;</span><span style="color: #0000ff;">$cfg</span><span style="color: #66cc66;">&#41;</span>
<span style="color: #66cc66;">&#123;</span>
    <span style="color: #0000ff;">$cfg</span>-&gt;<span style="color: #006600;">set_model_directory</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">'/path/to/your/model_directory'</span><span style="color: #66cc66;">&#41;</span>;
    <span style="color: #0000ff;">$cfg</span>-&gt;<span style="color: #006600;">set_connections</span><span style="color: #66cc66;">&#40;</span><a style="text-decoration: none;" href="http://www.php.net/array"><span style="color: #000066;">array</span></a><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">'development'</span> =&gt;
       <span style="color: #ff0000;">'mysql://username:password@localhost/database_name'</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>;
<span style="color: #66cc66;">&#125;</span><span style="color: #66cc66;">&#41;</span>;
&nbsp;
<span style="color: #808080; font-style: italic;">#Alternatively (w/o the 5.3 closure):</span>
<span style="color: #0000ff;">$cfg</span> = ActiveRecord\Config::<span style="color: #006600;">instance</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
<span style="color: #0000ff;">$cfg</span>-&gt;<span style="color: #006600;">set_model_directory</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">'/path/to/your/model_directory'</span><span style="color: #66cc66;">&#41;</span>;
<span style="color: #0000ff;">$cfg</span>-&gt;<span style="color: #006600;">set_connections</span><span style="color: #66cc66;">&#40;</span><a style="text-decoration: none;" href="http://www.php.net/array"><span style="color: #000066;">array</span></a><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">'development'</span> =&gt;
    <span style="color: #ff0000;">'mysql://username:password@localhost/database_name'</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>;
&nbsp;</pre>
<p></code></p>
<p>Once you have configured these two settings you are done. ActiveRecord takes care of the rest for you. It does not require that you map your table schema to yaml/xml files. It will query the database for this information and cache it so that it does not make multiple calls to the database for a single schema.</p>
<h2><a name="finder_methods">Finder methods</a></h2>
<p>ActiveRecord supports a number of methods by which you can find records either by primary key or you can construct your own complex conditions array with other options such as:  order, limit, select, group.<br />
<code></p>
<pre class="php">&nbsp;
<span style="color: #808080; font-style: italic;">#find by primary key</span>
Author::<span style="color: #006600;">find</span><span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">3</span><span style="color: #66cc66;">&#41;</span>;
&nbsp;
<span style="color: #808080; font-style: italic;">#same as above but expecting multiple results</span>
Author::<span style="color: #006600;">find</span><span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">1</span>,<span style="color: #cc66cc;">2</span>,<span style="color: #cc66cc;">3</span><span style="color: #66cc66;">&#41;</span>;
&nbsp;
<span style="color: #808080; font-style: italic;">#find the first record with limit</span>
Author::<span style="color: #006600;">first</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>; 
&nbsp;
<span style="color: #808080; font-style: italic;">#find last record by order and limit</span>
Author::<span style="color: #006600;">last</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>; 
&nbsp;
Author::<span style="color: #006600;">all</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>; 
&nbsp;
<span style="color: #808080; font-style: italic;">#this may be evil - but you can pass your raw sql to this method</span>
Author::<span style="color: #006600;">find_by_sql</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
&nbsp;
<span style="color: #808080; font-style: italic;">## you can also pass many options to finder methods</span>
&nbsp;
<span style="color: #808080; font-style: italic;">#sql =&gt; ORDER BY name</span>
Author::<span style="color: #006600;">find</span><span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">3</span>,<a style="text-decoration: none;" href="http://www.php.net/array"><span style="color: #000066;">array</span></a><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">'order'</span> =&gt; <span style="color: #ff0000;">'name'</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>;
&nbsp;
<span style="color: #808080; font-style: italic;">#sql =&gt; WHERE author_id IN (1,2,3)</span>
<span style="color: #808080; font-style: italic;">#find all with conditions as array</span>
Author::<span style="color: #006600;">find</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">'all'</span>,
  <a style="text-decoration: none;" href="http://www.php.net/array"><span style="color: #000066;">array</span></a><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">'conditions'</span> =&gt; <a style="text-decoration: none;" href="http://www.php.net/array"><span style="color: #000066;">array</span></a><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">'author_id IN(?)'</span>,<a style="text-decoration: none;" href="http://www.php.net/array"><span style="color: #000066;">array</span></a><span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">1</span>,<span style="color: #cc66cc;">2</span>,<span style="color: #cc66cc;">3</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>;
&nbsp;
<span style="color: #808080; font-style: italic;">#sql =&gt; WHERE author_id = 3</span>
<span style="color: #808080; font-style: italic;">#find with conditions as string</span>
Author::<span style="color: #006600;">find</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">'first'</span>,<a style="text-decoration: none;" href="http://www.php.net/array"><span style="color: #000066;">array</span></a><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">'conditions'</span> =&gt; <span style="color: #ff0000;">'author_id=3'</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>;
&nbsp;
<span style="color: #808080; font-style: italic;">#sql =&gt; GROUP BY name</span>
Author::<span style="color: #006600;">find</span><span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">3</span>, <a style="text-decoration: none;" href="http://www.php.net/array"><span style="color: #000066;">array</span></a><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">'group'</span> =&gt; <span style="color: #ff0000;">'name'</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>;
&nbsp;
<span style="color: #808080; font-style: italic;">#sql =&gt; LIMIT 0,3</span>
Author::<span style="color: #006600;">find</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">'all'</span>, <a style="text-decoration: none;" href="http://www.php.net/array"><span style="color: #000066;">array</span></a><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">'limit'</span> =&gt; <span style="color: #cc66cc;">3</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>;
&nbsp;
<span style="color: #808080; font-style: italic;">#sql =&gt; select * from `author` INNER JOIN etc...</span>
Author::<span style="color: #006600;">find</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">'all'</span>, <a style="text-decoration: none;" href="http://www.php.net/array"><span style="color: #000066;">array</span></a><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">'joins'</span> =&gt;
  <span style="color: #ff0000;">'INNER JOIN book on (book.author_id = author.id)'</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>;
&nbsp;
<span style="color: #808080; font-style: italic;">#sql =&gt; SELECT name FROM table</span>
Author::<span style="color: #006600;">first</span><span style="color: #66cc66;">&#40;</span><a style="text-decoration: none;" href="http://www.php.net/array"><span style="color: #000066;">array</span></a><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">'select'</span> =&gt; <span style="color: #ff0000;">'name'</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>;
&nbsp;
<span style="color: #808080; font-style: italic;">#these methods do not return records</span>
<span style="color: #808080; font-style: italic;">#return true/false</span>
Author::<span style="color: #006600;">exists</span><span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">1</span><span style="color: #66cc66;">&#41;</span>;
&nbsp;
<span style="color: #808080; font-style: italic;">#return integer</span>
Author::<a style="text-decoration: none;" href="http://www.php.net/count"><span style="color: #000066;">count</span></a><span style="color: #66cc66;">&#40;</span><a style="text-decoration: none;" href="http://www.php.net/array"><span style="color: #000066;">array</span></a><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">'conditions'</span> =&gt; <a style="text-decoration: none;" href="http://www.php.net/array"><span style="color: #000066;">array</span></a><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">'name = ?'</span>, <span style="color: #ff0000;">'John'</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>;
&nbsp;
<span style="color: #808080; font-style: italic;"># access is self-evident</span>
<span style="color: #0000ff;">$book</span> = Book::<span style="color: #006600;">first</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
<a style="text-decoration: none;" href="http://www.php.net/echo"><span style="color: #000066;">echo</span></a> <span style="color: #0000ff;">$book</span>-&gt;<span style="color: #006600;">title</span>;
<a style="text-decoration: none;" href="http://www.php.net/echo"><span style="color: #000066;">echo</span></a> <span style="color: #0000ff;">$book</span>-&gt;<span style="color: #006600;">author_id</span>;
&nbsp;
<span style="color: #808080; font-style: italic;">#returns an array</span>
<span style="color: #0000ff;">$books</span> = Book::<span style="color: #006600;">all</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
<a style="text-decoration: none;" href="http://www.php.net/echo"><span style="color: #000066;">echo</span></a> <span style="color: #0000ff;">$books</span><span style="color: #66cc66;">&#91;</span><span style="color: #cc66cc;">0</span><span style="color: #66cc66;">&#93;</span>-&gt;<span style="color: #006600;">title</span>;
&nbsp;</pre>
<p></code></p>
<h2><a name="dynamic_methods">Dynamic finder methods</a></h2>
<p>ActiveRecord within rails makes clever use of finders by allowing you to dynamically create methods based on the attribute names. This means you can easily make a "find_by_attribute_name" query without having to explicitly define it in the class. We also make use of this feature by using a new PHP 5.3 magic method: __callStatic().<br />
<code></p>
<pre class="php">&nbsp;
Author::<span style="color: #006600;">find_by_name</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">'George Bush'</span><span style="color: #66cc66;">&#41;</span>;
<span style="color: #808080; font-style: italic;">#you can make use of and/or</span>
Author::<span style="color: #006600;">find_by_name_or_author_id</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">'George Bush'</span>, <span style="color: #cc66cc;">2</span><span style="color: #66cc66;">&#41;</span>;
Person::<span style="color: #006600;">find_by_first_name_and_last_name</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">'Obama'</span>, <span style="color: #ff0000;">'Mama'</span><span style="color: #66cc66;">&#41;</span>;
<span style="color: #808080; font-style: italic;">#also have find_all_by</span>
Author::<span style="color: #006600;">find_all_by_name</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">'Tito'</span><span style="color: #66cc66;">&#41;</span>;
Author::<span style="color: #006600;">find_all_by_name</span><span style="color: #66cc66;">&#40;</span><a style="text-decoration: none;" href="http://www.php.net/array"><span style="color: #000066;">array</span></a><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">'Tito'</span>,<span style="color: #ff0000;">'Bill Clinton'</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>;
&nbsp;</pre>
<p></code></p>
<h2><a name="writer_methods">Writer methods</a></h2>
<p>What good is it to have an object that encapsulates a record from the database if you can't do anything with it?<br />
<code></p>
<pre class="php">&nbsp;
<span style="color: #808080; font-style: italic;">#call to SQL insert since it knows that it is a new record</span>
<span style="color: #0000ff;">$book</span> = <span style="color: #000000; font-weight: bold;">new</span> Artist<span style="color: #66cc66;">&#40;</span><a style="text-decoration: none;" href="http://www.php.net/array"><span style="color: #000066;">array</span></a><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">'name'</span> =&gt; <span style="color: #ff0000;">'Tito'</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>;
<span style="color: #0000ff;">$book</span>-&gt;<span style="color: #006600;">save</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
&nbsp;
<span style="color: #808080; font-style: italic;">## updates</span>
<span style="color: #808080; font-style: italic;">#only update 'dirty' attributes meaning the sql would only update</span>
<span style="color: #808080; font-style: italic;">#fields that have been changed</span>
<span style="color: #0000ff;">$book</span> = Book::<span style="color: #006600;">find</span><span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">1</span><span style="color: #66cc66;">&#41;</span>;
<span style="color: #0000ff;">$book</span>-&gt;<span style="color: #006600;">title</span> = <span style="color: #ff0000;">'new title!'</span>;
<span style="color: #0000ff;">$book</span>-&gt;<span style="color: #006600;">save</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
&nbsp;
<span style="color: #808080; font-style: italic;">#this will automatically call save</span>
<span style="color: #0000ff;">$book</span> = Book::<span style="color: #006600;">find</span><span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">1</span><span style="color: #66cc66;">&#41;</span>;
<span style="color: #0000ff;">$book</span>-&gt;<span style="color: #006600;">update_attributes</span><span style="color: #66cc66;">&#40;</span><a style="text-decoration: none;" href="http://www.php.net/array"><span style="color: #000066;">array</span></a><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">'title'</span> =&gt; <span style="color: #ff0000;">'new title!'</span>,
  <span style="color: #ff0000;">'price'</span> =&gt; <span style="color: #cc66cc;">5.00</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>;
&nbsp;
<span style="color: #808080; font-style: italic;">#will also make call to save</span>
<span style="color: #0000ff;">$book</span> = Book::<span style="color: #006600;">find</span><span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">1</span><span style="color: #66cc66;">&#41;</span>;
<span style="color: #0000ff;">$book</span>-&gt;<span style="color: #006600;">update_attribute</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">'title'</span>, <span style="color: #ff0000;">'some new title'</span><span style="color: #66cc66;">&#41;</span>;
&nbsp;
<span style="color: #0000ff;">$book</span> = <span style="color: #000000; font-weight: bold;">new</span> Book;
<span style="color: #0000ff;">$book</span>-&gt;<span style="color: #006600;">title</span> = <span style="color: #ff0000;">'new title!'</span>;
<span style="color: #0000ff;">$book</span>-&gt;<span style="color: #006600;">author_id</span> = <span style="color: #cc66cc;">5</span>;
<span style="color: #0000ff;">$book</span>-&gt;<span style="color: #006600;">save</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
<span style="color: #0000ff;">$book</span>-&gt;<span style="color: #006600;">created_at</span> <span style="color: #808080; font-style: italic;"># we also support created_at/updated_at timestamps where applicable</span>
<a style="text-decoration: none;" href="http://www.php.net/echo"><span style="color: #000066;">echo</span></a> <span style="color: #0000ff;">$book</span>-&gt;<span style="color: #006600;">id</span>; <span style="color: #808080; font-style: italic;">#id is also set to the auto increment value from the db</span>
&nbsp;
<span style="color: #808080; font-style: italic;">#delete</span>
<span style="color: #0000ff;">$author</span> = Author::<span style="color: #006600;">find</span><span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">4</span><span style="color: #66cc66;">&#41;</span>;
<span style="color: #0000ff;">$author</span>-&gt;<span style="color: #006600;">delete</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
&nbsp;
<span style="color: #808080; font-style: italic;">#you can pass readonly on the find so that you cannot save a model</span>
<span style="color: #0000ff;">$book</span> = Book::<span style="color: #006600;">first</span><span style="color: #66cc66;">&#40;</span><a style="text-decoration: none;" href="http://www.php.net/array"><span style="color: #000066;">array</span></a><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">'readonly'</span> =&gt; <span style="color: #000000; font-weight: bold;">true</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>;
<span style="color: #0000ff;">$book</span>-&gt;<span style="color: #006600;">title</span> = <span style="color: #ff0000;">'new'</span>; <span style="color: #808080; font-style: italic;"># or you could set it here by doing $book-&gt;readonly(true);</span>
<span style="color: #808080; font-style: italic;">#this will throw an ActiveRecord\ReadonlyException</span>
<span style="color: #0000ff;">$book</span>-&gt;<span style="color: #006600;">save</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
&nbsp;</pre>
<p></code></p>
<h2><a name="relationships">Relationships</a></h2>
<p>Associations are the complex piece of ActiveRecord. They accept many of the same options as with rails.<br />
<code></p>
<pre class="php">&nbsp;
<span style="color: #808080; font-style: italic;">#relationships are declared with a static var</span>
<span style="color: #000000; font-weight: bold;">class</span> Book <span style="color: #000000; font-weight: bold;">extends</span> ActiveRecord\Model <span style="color: #66cc66;">&#123;</span>
  <a style="text-decoration: none;" href="http://www.php.net/static"><span style="color: #000066;">static</span></a> <span style="color: #0000ff;">$belongs_to</span> = <a style="text-decoration: none;" href="http://www.php.net/array"><span style="color: #000066;">array</span></a><span style="color: #66cc66;">&#40;</span>
    <a style="text-decoration: none;" href="http://www.php.net/array"><span style="color: #000066;">array</span></a><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">'publisher'</span><span style="color: #66cc66;">&#41;</span>,
    <a style="text-decoration: none;" href="http://www.php.net/array"><span style="color: #000066;">array</span></a><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">'author'</span>, <span style="color: #ff0000;">'readonly'</span> =&gt; <span style="color: #000000; font-weight: bold;">true</span>, <span style="color: #ff0000;">'select'</span> =&gt; <span style="color: #ff0000;">'name'</span>, <span style="color: #ff0000;">'conditions'</span> =&gt; <span style="color: #ff0000;">&quot;name != 'jax'&quot;</span><span style="color: #66cc66;">&#41;</span>,
    <a style="text-decoration: none;" href="http://www.php.net/array"><span style="color: #000066;">array</span></a><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">'another'</span>, <span style="color: #ff0000;">'class_name'</span> =&gt; <span style="color: #ff0000;">'SomeModel'</span><span style="color: #66cc66;">&#41;</span>
  <span style="color: #66cc66;">&#41;</span>;
  <span style="color: #808080; font-style: italic;">#has_many accepts select/conditions/limit/readonly/group/primary_key</span>
  <span style="color: #808080; font-style: italic;">#has_many also takes a through option which you can use with source</span>
  <span style="color: #808080; font-style: italic;">#to clarify the class</span>
  <a style="text-decoration: none;" href="http://www.php.net/static"><span style="color: #000066;">static</span></a> <span style="color: #0000ff;">$has_many</span> = <a style="text-decoration: none;" href="http://www.php.net/array"><span style="color: #000066;">array</span></a><span style="color: #66cc66;">&#40;</span>
    <a style="text-decoration: none;" href="http://www.php.net/array"><span style="color: #000066;">array</span></a><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">'revisions'</span><span style="color: #66cc66;">&#41;</span>,
    <a style="text-decoration: none;" href="http://www.php.net/array"><span style="color: #000066;">array</span></a><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">'editors'</span>, <span style="color: #ff0000;">'through'</span> =&gt; <span style="color: #ff0000;">'revisions'</span><span style="color: #66cc66;">&#41;</span>
  <span style="color: #66cc66;">&#41;</span>;
&nbsp;
  <a style="text-decoration: none;" href="http://www.php.net/static"><span style="color: #000066;">static</span></a> <span style="color: #0000ff;">$has_one</span> = <a style="text-decoration: none;" href="http://www.php.net/array"><span style="color: #000066;">array</span></a><span style="color: #66cc66;">&#40;</span>
    <a style="text-decoration: none;" href="http://www.php.net/array"><span style="color: #000066;">array</span></a><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">'something'</span><span style="color: #66cc66;">&#41;</span>
  <span style="color: #66cc66;">&#41;</span>;
<span style="color: #66cc66;">&#125;</span>
&nbsp;
<span style="color: #808080; font-style: italic;">#the 0 index declaration array of each association is the &quot;attribute_name&quot;</span>
<span style="color: #808080; font-style: italic;">#which you use to access on the model like so:</span>
<span style="color: #0000ff;">$book</span> = Book::<span style="color: #006600;">first</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
<a style="text-decoration: none;" href="http://www.php.net/echo"><span style="color: #000066;">echo</span></a> <span style="color: #0000ff;">$book</span>-&gt;<span style="color: #006600;">publisher</span>-&gt;<span style="color: #006600;">name</span>;
<a style="text-decoration: none;" href="http://www.php.net/echo"><span style="color: #000066;">echo</span></a> <span style="color: #0000ff;">$book</span>-&gt;<span style="color: #006600;">author</span>-&gt;<span style="color: #006600;">name</span>; <span style="color: #808080; font-style: italic;"># we only have name as an attribute b/c of the select opt</span>
<span style="color: #808080; font-style: italic;">#below will throw a readonlyException due to the option -- see the writer</span>
<span style="color: #808080; font-style: italic;">#methods section</span>
<span style="color: #0000ff;">$book</span>-&gt;<span style="color: #006600;">author</span>-&gt;<span style="color: #006600;">save</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
&nbsp;
<span style="color: #808080; font-style: italic;">#has_many</span>
<a style="text-decoration: none;" href="http://www.php.net/echo"><span style="color: #000066;">echo</span></a> <span style="color: #0000ff;">$book</span>-&gt;<span style="color: #006600;">revisions</span><span style="color: #66cc66;">&#91;</span><span style="color: #cc66cc;">0</span><span style="color: #66cc66;">&#93;</span>-&gt;<span style="color: #006600;">id</span>;
<span style="color: #808080; font-style: italic;">#has_many through</span>
<a style="text-decoration: none;" href="http://www.php.net/echo"><span style="color: #000066;">echo</span></a> <span style="color: #0000ff;">$book</span>-&gt;<span style="color: #006600;">editors</span><span style="color: #66cc66;">&#91;</span><span style="color: #cc66cc;">0</span><span style="color: #66cc66;">&#93;</span>-&gt;<span style="color: #006600;">name</span>;
&nbsp;</pre>
<p></code></p>
<h2><a name="validations">Validations</a></h2>
<p>This is rather self-explanatory. Before save/update/insert, your validations will be called for each declaration you have made and will save the model if all validations have passed. Otherwise, you will have access to an errors attribute which you can use to get back validation error messages.</p>
<pre class="php">&nbsp;
<span style="color: #000000; font-weight: bold;">class</span> Book <span style="color: #000000; font-weight: bold;">extends</span> ActiveRecord\Model
<span style="color: #66cc66;">&#123;</span>
  <a style="text-decoration: none;" href="http://www.php.net/static"><span style="color: #000066;">static</span></a> <span style="color: #0000ff;">$validates_format_of</span> = <a style="text-decoration: none;" href="http://www.php.net/array"><span style="color: #000066;">array</span></a><span style="color: #66cc66;">&#40;</span>
    <a style="text-decoration: none;" href="http://www.php.net/array"><span style="color: #000066;">array</span></a><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">'title'</span>, <span style="color: #ff0000;">'with'</span> =&gt; <span style="color: #ff0000;">'/^[a-zW]*$/'</span>, <span style="color: #ff0000;">'allow_blank'</span> =&gt; <span style="color: #000000; font-weight: bold;">true</span><span style="color: #66cc66;">&#41;</span>
  <span style="color: #66cc66;">&#41;</span>;
  <a style="text-decoration: none;" href="http://www.php.net/static"><span style="color: #000066;">static</span></a> <span style="color: #0000ff;">$validates_exclusion_of</span> = <a style="text-decoration: none;" href="http://www.php.net/array"><span style="color: #000066;">array</span></a><span style="color: #66cc66;">&#40;</span>
    <a style="text-decoration: none;" href="http://www.php.net/array"><span style="color: #000066;">array</span></a><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">'title'</span>, <span style="color: #ff0000;">'in'</span> =&gt; <a style="text-decoration: none;" href="http://www.php.net/array"><span style="color: #000066;">array</span></a><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">'blah'</span>, <span style="color: #ff0000;">'alpha'</span>, <span style="color: #ff0000;">'bravo'</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>
  <span style="color: #66cc66;">&#41;</span>;
  <a style="text-decoration: none;" href="http://www.php.net/static"><span style="color: #000066;">static</span></a> <span style="color: #0000ff;">$validates_inclusion_of</span> = <a style="text-decoration: none;" href="http://www.php.net/array"><span style="color: #000066;">array</span></a><span style="color: #66cc66;">&#40;</span>
    <a style="text-decoration: none;" href="http://www.php.net/array"><span style="color: #000066;">array</span></a><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">'title'</span>, <span style="color: #ff0000;">'within'</span> =&gt; <a style="text-decoration: none;" href="http://www.php.net/array"><span style="color: #000066;">array</span></a><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">'tragedy of dubya'</span>, <span style="color: #ff0000;">'sharks wit laserz'</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>
  <span style="color: #66cc66;">&#41;</span>;
  <a style="text-decoration: none;" href="http://www.php.net/static"><span style="color: #000066;">static</span></a> <span style="color: #0000ff;">$validates_length_of</span> = <a style="text-decoration: none;" href="http://www.php.net/array"><span style="color: #000066;">array</span></a><span style="color: #66cc66;">&#40;</span>
    <a style="text-decoration: none;" href="http://www.php.net/array"><span style="color: #000066;">array</span></a><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">'title'</span>, <span style="color: #ff0000;">'within'</span> =&gt; <a style="text-decoration: none;" href="http://www.php.net/array"><span style="color: #000066;">array</span></a><span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">1</span>, <span style="color: #cc66cc;">5</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>,
    <a style="text-decoration: none;" href="http://www.php.net/array"><span style="color: #000066;">array</span></a><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">'attribute2'</span>, <span style="color: #ff0000;">'in'</span> =&gt; <a style="text-decoration: none;" href="http://www.php.net/array"><span style="color: #000066;">array</span></a><span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">1</span>,<span style="color: #cc66cc;">2</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>,
    <a style="text-decoration: none;" href="http://www.php.net/array"><span style="color: #000066;">array</span></a><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">'attribute3'</span>, <span style="color: #ff0000;">'is'</span> =&gt; <span style="color: #cc66cc;">4</span>, <span style="color: #ff0000;">'allow_null'</span> =&gt; <span style="color: #000000; font-weight: bold;">true</span><span style="color: #66cc66;">&#41;</span>
  <span style="color: #66cc66;">&#41;</span>;
  <span style="color: #808080; font-style: italic;"># same as above since it is just an alias</span>
  <a style="text-decoration: none;" href="http://www.php.net/static"><span style="color: #000066;">static</span></a> <span style="color: #0000ff;">$validates_size_of</span> = <a style="text-decoration: none;" href="http://www.php.net/array"><span style="color: #000066;">array</span></a><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
  <a style="text-decoration: none;" href="http://www.php.net/static"><span style="color: #000066;">static</span></a> <span style="color: #0000ff;">$validates_numericality_of</span> = <a style="text-decoration: none;" href="http://www.php.net/array"><span style="color: #000066;">array</span></a><span style="color: #66cc66;">&#40;</span>
    <a style="text-decoration: none;" href="http://www.php.net/array"><span style="color: #000066;">array</span></a><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">'title'</span><span style="color: #66cc66;">&#41;</span>
  <span style="color: #66cc66;">&#41;</span>;
  <a style="text-decoration: none;" href="http://www.php.net/static"><span style="color: #000066;">static</span></a> <span style="color: #0000ff;">$validates_presence_of</span> = <a style="text-decoration: none;" href="http://www.php.net/array"><span style="color: #000066;">array</span></a><span style="color: #66cc66;">&#40;</span>
    <a style="text-decoration: none;" href="http://www.php.net/array"><span style="color: #000066;">array</span></a><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">'title'</span><span style="color: #66cc66;">&#41;</span>
  <span style="color: #66cc66;">&#41;</span>;
<span style="color: #66cc66;">&#125;</span>;
&nbsp;
<span style="color: #0000ff;">$book</span> = <span style="color: #000000; font-weight: bold;">new</span> Book;
<span style="color: #0000ff;">$book</span>-&gt;<span style="color: #006600;">title</span> = <span style="color: #ff0000;">'this is not part of the inclusion'</span>;
<span style="color: #b1b100;">if</span> <span style="color: #66cc66;">&#40;</span>!<span style="color: #0000ff;">$book</span>-&gt;<span style="color: #006600;">save</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>
  <a style="text-decoration: none;" href="http://www.php.net/print_r"><span style="color: #000066;">print_r</span></a><span style="color: #66cc66;">&#40;</span><span style="color: #0000ff;">$book</span>-&gt;<span style="color: #006600;">errors</span>-&gt;<span style="color: #006600;">on</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">'title'</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>;
&nbsp;</pre>
<h2><a name="callbacks">Callbacks</a></h2>
<p>Callbacks allow you to take command of your model before/after certain events during its lifecycle. You can define methods in your model that will occur as callbacks before or after other methods are invoked on the model. Unfortunately, even though PHP 5.3 has closures, you cannot use them in a static var declaration so you must define/use methods.<br />
<code></p>
<pre class="php">&nbsp;
<span style="color: #808080; font-style: italic;">#below are the possible declarations that you can make</span>
<span style="color: #808080; font-style: italic;">#if your callback returns false for a before_* then it will cancel the</span>
<span style="color: #808080; font-style: italic;">#action and the rest of the callbacks</span>
<span style="color: #000000; font-weight: bold;">class</span> Book <span style="color: #000000; font-weight: bold;">extends</span> ActiveRecordModel<span style="color: #66cc66;">&#123;</span>
  <a style="text-decoration: none;" href="http://www.php.net/static"><span style="color: #000066;">static</span></a> <span style="color: #0000ff;">$after_construct</span>;
  <a style="text-decoration: none;" href="http://www.php.net/static"><span style="color: #000066;">static</span></a> <span style="color: #0000ff;">$before_save</span> = <a style="text-decoration: none;" href="http://www.php.net/array"><span style="color: #000066;">array</span></a><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">'do_something_before_save'</span><span style="color: #66cc66;">&#41;</span>;
  <a style="text-decoration: none;" href="http://www.php.net/static"><span style="color: #000066;">static</span></a> <span style="color: #0000ff;">$after_save</span>;
  <a style="text-decoration: none;" href="http://www.php.net/static"><span style="color: #000066;">static</span></a> <span style="color: #0000ff;">$before_create</span>;
  <a style="text-decoration: none;" href="http://www.php.net/static"><span style="color: #000066;">static</span></a> <span style="color: #0000ff;">$after_create</span>;
  <a style="text-decoration: none;" href="http://www.php.net/static"><span style="color: #000066;">static</span></a> <span style="color: #0000ff;">$before_update</span>;
  <a style="text-decoration: none;" href="http://www.php.net/static"><span style="color: #000066;">static</span></a> <span style="color: #0000ff;">$after_update</span>;
  <a style="text-decoration: none;" href="http://www.php.net/static"><span style="color: #000066;">static</span></a> <span style="color: #0000ff;">$before_validation</span>;
  <a style="text-decoration: none;" href="http://www.php.net/static"><span style="color: #000066;">static</span></a> <span style="color: #0000ff;">$after_validation</span>;
  <a style="text-decoration: none;" href="http://www.php.net/static"><span style="color: #000066;">static</span></a> <span style="color: #0000ff;">$before_validation_on_create</span>;
  <a style="text-decoration: none;" href="http://www.php.net/static"><span style="color: #000066;">static</span></a> <span style="color: #0000ff;">$after_validation_on_create</span>;
  <a style="text-decoration: none;" href="http://www.php.net/static"><span style="color: #000066;">static</span></a> <span style="color: #0000ff;">$before_validation_on_update</span>;
  <a style="text-decoration: none;" href="http://www.php.net/static"><span style="color: #000066;">static</span></a> <span style="color: #0000ff;">$after_validation_on_update</span>;
  <a style="text-decoration: none;" href="http://www.php.net/static"><span style="color: #000066;">static</span></a> <span style="color: #0000ff;">$before_destroy</span>;
  <a style="text-decoration: none;" href="http://www.php.net/static"><span style="color: #000066;">static</span></a> <span style="color: #0000ff;">$after_destroy</span>;
&nbsp;
  <span style="color: #808080; font-style: italic;">#this will be called directly before save()</span>
  <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> do_something_before_save<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span><span style="color: #66cc66;">&#125;</span>
<span style="color: #66cc66;">&#125;</span>
&nbsp;</pre>
<p></code></p>
<h2><a name="serializations">Serializations</a></h2>
<p><code></p>
<pre class="php">&nbsp;
<span style="color: #000000; font-weight: bold;">class</span> Book <span style="color: #000000; font-weight: bold;">extends</span> ActiveRecord\Model<span style="color: #66cc66;">&#123;</span>
  <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> upper_title<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#123;</span>
    <span style="color: #b1b100;">return</span> <a style="text-decoration: none;" href="http://www.php.net/strtoupper"><span style="color: #000066;">strtoupper</span></a><span style="color: #66cc66;">&#40;</span><span style="color: #0000ff;">$this</span>-&gt;<span style="color: #006600;">title</span><span style="color: #66cc66;">&#41;</span>;
  <span style="color: #66cc66;">&#125;</span>
<span style="color: #66cc66;">&#125;</span>
&nbsp;
<span style="color: #808080; font-style: italic;">#produces: {title: 'sharks wit lazers', author_id: 2}</span>
<span style="color: #0000ff;">$book</span> = Book::<span style="color: #006600;">find</span><span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">1</span><span style="color: #66cc66;">&#41;</span>;
<span style="color: #0000ff;">$book</span>-&gt;<span style="color: #006600;">to_json</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>; 
&nbsp;
<span style="color: #808080; font-style: italic;">#produces: {title: 'sharks wit lazers'}</span>
<span style="color: #0000ff;">$book</span>-&gt;<span style="color: #006600;">to_json</span><span style="color: #66cc66;">&#40;</span><a style="text-decoration: none;" href="http://www.php.net/array"><span style="color: #000066;">array</span></a><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">'except'</span> =&gt; <span style="color: #ff0000;">'author_id'</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>;
&nbsp;
<span style="color: #808080; font-style: italic;">#produces:  {upper_title: 'SHARKS WIT LAZERS'}</span>
<span style="color: #808080; font-style: italic;">#make methods an array of methods and it will call them all</span>
<span style="color: #0000ff;">$book</span>-&gt;<span style="color: #006600;">to_json</span><span style="color: #66cc66;">&#40;</span><a style="text-decoration: none;" href="http://www.php.net/array"><span style="color: #000066;">array</span></a><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">'methods'</span> =&gt; <span style="color: #ff0000;">'upper_title'</span>, <span style="color: #ff0000;">'only'</span> =&gt; <span style="color: #ff0000;">'upper_title'</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>; 
&nbsp;
<span style="color: #808080; font-style: italic;">#produces {title: 'sharks wit lazers', author_id: 2}</span>
<span style="color: #0000ff;">$book</span>-&gt;<span style="color: #006600;">to_json</span><span style="color: #66cc66;">&#40;</span><a style="text-decoration: none;" href="http://www.php.net/array"><span style="color: #000066;">array</span></a><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">'include'</span> =&gt; <a style="text-decoration: none;" href="http://www.php.net/array"><span style="color: #000066;">array</span></a><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">'author'</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>;
&nbsp;
<span style="color: #808080; font-style: italic;">#also support xml w/ the same options but need more tests =)</span>
<span style="color: #0000ff;">$book</span>-&gt;<span style="color: #006600;">to_xml</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
&nbsp;</pre>
<p></code></p>
<h2><a name="adapters">Support for multiple adapters</a></h2>
<p>Currently, there exists support only for <strong>MySQL</strong> (also through mysqli) and <strong>sqlite3</strong>. Right now there are only two contributors to the code base; however, we hope to attract more coders to the project that can help support additional adapters such as postgres and oracle. The connection/adapter piece of the code has been sufficiently abstracted so that it should not be difficult to create more adapters when the time comes.</p>
<h2><a name="misc_options">Miscellaneous Options</a></h2>
<p>When declaring a model you can also specify the primary_key and table_name. Protected/accessible declarations are available so that you can avoid mass assignment problems. Attributes can be aliased so that you may access them via different names.<br />
<code></p>
<pre class="php">&nbsp;
<span style="color: #000000; font-weight: bold;">class</span> Book <span style="color: #000000; font-weight: bold;">extends</span> ActiveRecord\Model<span style="color: #66cc66;">&#123;</span>
  <a style="text-decoration: none;" href="http://www.php.net/static"><span style="color: #000066;">static</span></a> <span style="color: #0000ff;">$primary_key</span> = <span style="color: #ff0000;">'book_id'</span>;
  <a style="text-decoration: none;" href="http://www.php.net/static"><span style="color: #000066;">static</span></a> <span style="color: #0000ff;">$table_name</span> = <span style="color: #ff0000;">'book_table'</span>;
  <a style="text-decoration: none;" href="http://www.php.net/static"><span style="color: #000066;">static</span></a> <span style="color: #0000ff;">$attr_accessible</span> = <a style="text-decoration: none;" href="http://www.php.net/array"><span style="color: #000066;">array</span></a><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">'author_id'</span><span style="color: #66cc66;">&#41;</span>;
  <a style="text-decoration: none;" href="http://www.php.net/static"><span style="color: #000066;">static</span></a> <span style="color: #0000ff;">$attr_protected</span> = <a style="text-decoration: none;" href="http://www.php.net/array"><span style="color: #000066;">array</span></a><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">'book_id'</span><span style="color: #66cc66;">&#41;</span>;
  <a style="text-decoration: none;" href="http://www.php.net/static"><span style="color: #000066;">static</span></a> <span style="color: #0000ff;">$alias_attribute</span> = <a style="text-decoration: none;" href="http://www.php.net/array"><span style="color: #000066;">array</span></a><span style="color: #66cc66;">&#40;</span>
    <span style="color: #ff0000;">'new_alias'</span> =&gt; <span style="color: #ff0000;">'actual_attribute'</span>,
    <span style="color: #ff0000;">'new_alias_two'</span> =&gt; <span style="color: #ff0000;">'other_actual_attribute'</span>
  <span style="color: #66cc66;">&#41;</span>;
<span style="color: #66cc66;">&#125;</span>
&nbsp;</pre>
<p></code></p>
<h2>The Future</h2>
<p>As I stated previously, very shortly the code will be available on launchpad. We are also in the works of putting up a website to host tutorials and documentation for the code. I will make additional posts once those milestones have been reached. Thanks for reading.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.derivante.com/2009/05/14/php-activerecord-with-php-53/feed/</wfw:commentRss>
		<slash:comments>20</slash:comments>
		</item>
	</channel>
</rss>
<!-- WP Super Cache is installed but broken. The path to wp-cache-phase1.php in wp-content/advanced-cache.php must be fixed! -->
