<?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; Jacques Fuentes</title>
	<atom:link href="http://www.derivante.com/author/jpfuentes2/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 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>
		<item>
		<title>PHP on Rails &#8211; The Flash</title>
		<link>http://www.derivante.com/2008/12/15/php-on-rails-the-flash/</link>
		<comments>http://www.derivante.com/2008/12/15/php-on-rails-the-flash/#comments</comments>
		<pubDate>Mon, 15 Dec 2008 14:15:44 +0000</pubDate>
		<dc:creator>Jacques Fuentes</dc:creator>
				<category><![CDATA[PHP]]></category>
		<category><![CDATA[PHP on Rails]]></category>

		<guid isPermaLink="false">http://www.derivante.com/?p=162</guid>
		<description><![CDATA[Last week I wrote an article about extending my framework (Pho framework courtsey of Kien La) to DRY your code and to be more similar to Rails. Today, I would like to present to you another class that will help (&#8230;)</p><p><a href="http://www.derivante.com/2008/12/15/php-on-rails-the-flash/">Read the rest of this entry &#187;</a></p>]]></description>
			<content:encoded><![CDATA[<p><img class="alignleft size-full wp-image-425" style="margin: 15px;" title="php-med-trans-light" src="http://www.derivante.com/wp-content/uploads/2009/05/php-med-trans-light.gif" alt="php-med-trans-light" width="95" height="51" />Last week I wrote an article about extending my framework (Pho framework courtsey of Kien La) to DRY your code and to be more similar to Rails.</p>
<p>Today, I would like to present to you another class that will help extend your framework in similar fashion.</p>
<h3>The Flash</h3>
<p>If you aren't familiar with the way RoR uses "flash" messaging, you can take a look at the <a title="RoR API Flash" href="http://api.rubyonrails.org/classes/ActionController/Flash.html" target="_blank">RoR API</a>, or you can read this explanation:</p>
<blockquote><p>The flash provides a way to pass temporary objects between actions. Anything you place in the flash will be exposed to the very next action and then cleared out.</p></blockquote>
<p>The flash allows you to place messages in a user's session that can be viewed when navigating (or redirecting) to different pages or even the current page. If you read further into the API, you will see that the RoR flash offers you the following behavior:</p>
<ul>
<li>Set a regular message (viewable upon next http request)</li>
<li>Create a "now" message (viewable now and <strong>not</strong> upon next http request)</li>
<li>"Keep" a message or all messages (is a message is viewable now, it will now also be viewable upon the next http request)</li>
<li>Discard/unset a message or all messages</li>
</ul>
<p>If you're a good little coder, you would first attempt to steal this code from somewhere else. I have found a few sites that offer 'some' of the functionality of the RoR flash; however, I would like to have the flash in its full capacity. Therefore, I have created my own class.</p>
<p>The behavior of the flash is rather simple and so is my class. If you think about the lifetime of the messages, it should hit you that we have essentially two types of flash messages: now and later (next http request). Even though we have two types of messages, we actually only have one corresponding message. This means that a certain flash message "error" could have a now <em>and</em> a later message. The reason behind this is beceause we want to use discard() and keep() which we should be able to pass a 'key' to when invoked so that discard would remove all traces of our "error" message (or all messages if no key is passed). My class requires php &gt;= version 5. Keep in mind that you will only want one instance of this class, so you could make it a singleton if you'd like. The constructor does all of the work by removing old messages and such, so you only have to worry about setting messages. Okay, let's see some code.</p>
<div><a href="http://www.derivante.com/wp-content/uploads/2008/12/flash.zip">Download UserSessionFlash </a></div>
<pre class="php">&lt;!--p
<span style="color: #808080; font-style: italic;"># Instantiate flash which will remove old messages;</span>
<span style="color: #0000ff;">$flash</span> = <span style="color: #000000; font-weight: bold;">new</span> UserSessionFlash;
<span style="color: #0000ff;">$flash</span>-&gt;<span style="color: #006600;">error</span> = <span style="color: #ff0000;">'This is an error message for the next page request.'</span>;
<span style="color: #0000ff;">$flash</span>-&amp;gt;now<span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">'notice'</span>, <span style="color: #ff0000;">'Message will be available for the current page request.'</span><span style="color: #66cc66;">&#41;</span>;
&nbsp;
<span style="color: #808080; font-style: italic;"># Allows you to use the 'notice' for the next page request.</span>
<span style="color: #808080; font-style: italic;"># If you do not pass a key, then it will keep ALL messages.</span>
<span style="color: #0000ff;">$flash</span>-&amp;gt;keep<span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">'notice'</span><span style="color: #66cc66;">&#41;</span>; 
&nbsp;
<span style="color: #808080; font-style: italic;"># The $flash-&amp;gt;error above has now been erased.</span>
<span style="color: #808080; font-style: italic;"># Just like keep, if you do not pass a key, then ALL messages will be erased.</span>
<span style="color: #0000ff;">$flash</span>-&amp;gt;discard<span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">'error'</span><span style="color: #66cc66;">&#41;</span>;
<span style="color: #808080; font-style: italic;"># Delete all messages.</span>
<span style="color: #0000ff;">$flash</span>-&amp;gt;discard<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
?&amp;gt;</pre>
<p>As you can see this is all fairly basic. Simply add the flash instance to your action controller, and you access it via your page controller. What I have done is included a function in my action controller called flash() that returns my instance of the flash class. So my page controller would call</a></p>
<pre class="php"><span style="color: #0000ff;">$this</span>-&amp;gt;flash<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>-&amp;gt;error = <span style="color: #ff0000;">'This is an error msg.'</span>;
<span style="color: #0000ff;">$this</span>-&amp;gt;flash<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>-&amp;gt;now<span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">'notice'</span>, <span style="color: #ff0000;">'Show me the money.'</span><span style="color: #66cc66;">&#41;</span>;</pre>
<p>If you'd like, you can check out some other implementations below. Again, none of the other classes I've found offer the full rails flash behavior.</p>
<p><a href="http://poorbuthappy.com/ease/archives/2007/04/22/3589/rails-flash-in-php">http://poorbuthappy.com/ease/archives/2007/04/22/3589/rails-flash-in-php</a><br />
<a href="http://www.phpclasses.org/browse/package/3668.html">http://www.phpclasses.org/browse/package/3668.html</a><br />
<a href="http://shabadeehoob.com/2007/03/17/rails-like-flash-messages-in-cakephp/">http://shabadeehoob.com/2007/03/17/rails-like-flash-messages-in-cakephp/</a><br />
<a href="http://api.phpontrax.com/__filesource/fsource_PHPonTrax__vendortraxsession.php.html">http://api.phpontrax.com/__filesource/fsource_PHPonTrax__vendortraxsession.php.html</a></p>
]]></content:encoded>
			<wfw:commentRss>http://www.derivante.com/2008/12/15/php-on-rails-the-flash/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
		<item>
		<title>PHP on Rails</title>
		<link>http://www.derivante.com/2008/12/08/php-on-rails/</link>
		<comments>http://www.derivante.com/2008/12/08/php-on-rails/#comments</comments>
		<pubDate>Mon, 08 Dec 2008 18:23:07 +0000</pubDate>
		<dc:creator>Jacques Fuentes</dc:creator>
				<category><![CDATA[PHP]]></category>
		<category><![CDATA[PHP on Rails]]></category>

		<guid isPermaLink="false">http://www.derivante.com/?p=128</guid>
		<description><![CDATA[Adding conventions to DRY our code This article will provide a few snippets of code that I have recently plugged into the custom PHP framework that I use. However, those of you whom use and are more familiar with popular (&#8230;)</p><p><a href="http://www.derivante.com/2008/12/08/php-on-rails/">Read the rest of this entry &#187;</a></p>]]></description>
			<content:encoded><![CDATA[<p><img class="alignleft size-full wp-image-425" style="margin: 15px;" title="php-med-trans-light" src="http://www.derivante.com/wp-content/uploads/2009/05/php-med-trans-light.gif" alt="php-med-trans-light" width="95" height="51" /></p>
<h3>Adding conventions to DRY our code</h3>
<p>This article will provide a few snippets of code that I have recently plugged into the custom PHP framework that I use. However, those of you whom use and are more familiar with popular frameworks may be able to use this as well. I decided a few days ago to alter this custom framework in the following ways:</p>
<ul>
<li>an "application controller" which has access to the base and current page controller</li>
<li>filter capabilities in page controllers</li>
</ul>
<p>First, I will outline some basics about my framework so that you can understand my implementations.</p>
<p>The custom framework that I use is <strong>very</strong> similar to rails' conventions. It is an MVC framework and has almost a 1:1 folder/file structure to rails. One glaring difference is that it uses camelCase throughout the framework in opposition to the rails convetion. Anyway, let me start with the app folder (since this is the center of our attention).</p>
<p><img class="alignnone size-medium wp-image-56" title="app_layout" src="http://www.derivante.com/wp-content/uploads/2008/12/app_layout-211x300.png" alt="" width="211" height="300" /></p>
<p>You can see above that my app folder is almost exactly like rails except for the camelCase and the fact that my views have .tpls (go smarty!). You'll also notice that I have 3 page controllers plus the application controller. We are going to start with how I have implemented the application controller.</p>
<h3>The Application Controller</h3>
<p>You should already know that my page controllers will extend the "base" controller (which is simply Controller for me). Thus, my page controllers have access to all of the essentials such as redirections, session access, IoC, flash messaging, etc. So, we want to make another controller which will allow those 3 page controllers (and any future page controller) to access shared functions so that we can maintain DRY code. Keep in mind that this application controller should also have access to the parent controller.</p>
<p><strong>The problem:</strong> How do we easily give our application controller shared communication between the parent controllers and child controller, but also disallowing it from being a page controller itself?</p>
<p>First let's take a look at my page controller:</p>
<p> </p>
<p><code></p>
<pre class="php">&lt;!--p
<span style="color: #000000; font-weight: bold;">class</span> HomeController <span style="color: #000000; font-weight: bold;">extends</span> Controller
<span style="color: #66cc66;">&#123;</span>
  <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> actionIndex<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>
  <span style="color: #66cc66;">&#123;</span>
    <span style="color: #0000ff;">$thi</span>--&gt;<span style="color: #006600;">requireUser</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
  <span style="color: #66cc66;">&#125;</span>
<span style="color: #66cc66;">&#125;</span>;
?&amp;gt;</pre>
<p>You may have guessed that this "action" will respond to http://somewhere/home/index/ and render app/view/home/index.tpl (and you are correct!). Inside the function we are trying to access the application controller's "requireUser" method. Here's what the application controller looks like:</p>
<p> </p>
<p><code></p>
<pre class="php">&lt;!--p
<span style="color: #000000; font-weight: bold;">class</span> ApplicationController <span style="color: #000000; font-weight: bold;">extends</span> AbstractApplicationController
<span style="color: #66cc66;">&#123;</span>
   <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> requireUser<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;">$thi</span>--&gt;<span style="color: #006600;">session</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>-&amp;gt;hasRole<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;">'User'</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>
     <span style="color: #66cc66;">&#123;</span>
        <span style="color: #0000ff;">$this</span>-&amp;gt;flash<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>-&amp;gt;error = <span style="color: #ff0000;">'You must be logged in to do that.'</span>;
	<span style="color: #0000ff;">$this</span>-&amp;gt;redirect<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;">'controller'</span> =&amp;gt; <span style="color: #ff0000;">'home'</span>, <span style="color: #ff0000;">'action'</span> =&amp;gt; <span style="color: #ff0000;">'index'</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>;
     <span style="color: #66cc66;">&#125;</span>
   <span style="color: #66cc66;">&#125;</span>
<span style="color: #66cc66;">&#125;</span>;
?&amp;gt;</pre>
<p> </p>
<p>As you can see my application controller doesn't extend "Controller", but I'm still calling upon its methods. By having it extend from another class I get the easy benefit of not allowing the controller to be a "page" controller which means I cannot place action functions inside of it and render pages. Instead, it is simply there to DRY up some code and allow my other page controllers to access its methods. So how do we accomplish this and what does AbstractApplicationController look like? We'll get to that in a second. First, let me show you the two parts of the parent controller which we will need. Basically, directly before our method actionIndex in HomeController is invoked, we will create a new ApplicationController object in our parent controller so we can start communication.</p>
<p></code> </p>
<p><code></p>
<pre class="php"><span style="color: #0000ff;">$this</span>-&amp;gt;applicationController = <span style="color: #000000; font-weight: bold;">new</span> ApplicationController<span style="color: #66cc66;">&#40;</span><span style="color: #0000ff;">$this</span><span style="color: #66cc66;">&#41;</span>;</pre>
<p> </p>
<p>We pass $this to its constructor because the AbstractionApplicationController is basically a placeholder (or proxy) for the controller object.</p>
<p></code> </p>
<p><code></p>
<pre class="php">&lt;!--p
abstract <span style="color: #000000; font-weight: bold;">class</span> AbstractApplicationController
<span style="color: #66cc66;">&#123;</span>
   <span style="color: #000000; font-weight: bold;">private</span> <span style="color: #0000ff;">$controller</span>;
&nbsp;
   <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> _construct<span style="color: #66cc66;">&#40;</span>&amp;<span style="color: #808080; font-style: italic;">#038;$controller)</span>
   <span style="color: #66cc66;">&#123;</span>
      <span style="color: #b1b100;">if</span> <span style="color: #66cc66;">&#40;</span><a style="text-decoration: none;" href="http://www.php.net/is_subclass_of"><span style="color: #000066;">is_subclass_of</span></a><span style="color: #66cc66;">&#40;</span><span style="color: #0000ff;">$controller</span>, <span style="color: #ff0000;">'Controller'</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>
         <span style="color: #0000ff;">$thi</span>--&gt;<span style="color: #006600;">controller</span> =&amp;amp; <span style="color: #0000ff;">$controller</span>;
      <span style="color: #b1b100;">else</span>
         throw <span style="color: #000000; font-weight: bold;">new</span> Exception<span style="color: #66cc66;">&#40;</span><a style="text-decoration: none;" href="http://www.php.net/get_class"><span style="color: #000066;">get_class</span></a><span style="color: #66cc66;">&#40;</span><span style="color: #0000ff;">$controller</span><span style="color: #66cc66;">&#41;</span> .<span style="color: #ff0000;">' must be a subclass of Controller'</span><span style="color: #66cc66;">&#41;</span>;
   <span style="color: #66cc66;">&#125;</span>
&nbsp;
   <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> getController<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>
   <span style="color: #66cc66;">&#123;</span>
     <span style="color: #b1b100;">return</span> <span style="color: #0000ff;">$this</span>-&amp;gt;controller;
   <span style="color: #66cc66;">&#125;</span>
&nbsp;
   <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> __call<span style="color: #66cc66;">&#40;</span><span style="color: #0000ff;">$meth</span>, <span style="color: #0000ff;">$args</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/call_user_func_array"><span style="color: #000066;">call_user_func_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: #0000ff;">$this</span>-&amp;gt;controller, <span style="color: #0000ff;">$meth</span><span style="color: #66cc66;">&#41;</span>, <span style="color: #0000ff;">$args</span><span style="color: #66cc66;">&#41;</span>;
   <span style="color: #66cc66;">&#125;</span>
<span style="color: #66cc66;">&#125;</span>;
?&amp;gt;</pre>
<p> </p>
<p><span style="font-size: 15px; font-weight: bold; color: #570E00; font-family:Georgia,">The beauty is in the magic method __call()</span><br />
As you can see any method not found in your ApplicationController will find the magic __call() method and attempt to execute the method on the cached controller. This means when we redirect or add messages to the flash, we can use the same syntax as we would in our page controllers. This gives us one-way communication. What about accessing the "requireUser" method from the page controller? Again, there is beauty in __call(). We place the same thing in the base controller so that any method invoked from our page controller that isn't part of the base/page controller should be redirected to the ApplicationController object. Let's take a look.</p>
<p></code> </p>
<p><code></p>
<pre class="php">&lt;!--p
<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> __call<span style="color: #66cc66;">&#40;</span><span style="color: #0000ff;">$meth</span>, <span style="color: #0000ff;">$args</span><span style="color: #66cc66;">&#41;</span>
<span style="color: #66cc66;">&#123;</span>
   <span style="color: #b1b100;">return</span> <span style="color: #0000ff;">$thi</span>--&gt;<span style="color: #006600;">applicationController</span>-&amp;gt;<span style="color: #0000ff;">$meth</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
<span style="color: #66cc66;">&#125;</span>
?&amp;gt;</pre>
<p> </p>
<p>This allows us to call $this-&gt;requireUser() in our HomeController' actionIndex() method and it will invoke through base controller's __call() on the ApplicationController object. Now we have two-way communication between our page controllers and our application controller. Keep in mind that since the ApplicationController is not a child of Controller, we can only call upon public methods inside Controller and our page controllers.</p>
<p></code></p>
<h3>The Filter</h3>
<p>Rails has a great option for allowing the developer to invoke certain methods before or after the current page's action is invoked. I wanted this bonus also.</p>
<p> </p>
<p><code></p>
<pre class="php">&lt;!--p
<span style="color: #000000; font-weight: bold;">class</span> GamesController <span style="color: #000000; font-weight: bold;">extends</span> Controller
<span style="color: #66cc66;">&#123;</span>
   protected <span style="color: #0000ff;">$BEFORE_FILTER</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;">'requireUser'</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;">'except'</span> =&amp;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;">'myExceptionAction'</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</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> actionIndex<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>
   <span style="color: #66cc66;">&#123;</span>
      <span style="color: #808080; font-style: italic;">//before filter method will be invoked before this method is invoked</span>
   <span style="color: #66cc66;">&#125;</span>
&nbsp;
   <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> actionMyExceptionAction<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>
   <span style="color: #66cc66;">&#123;</span>
      <span style="color: #808080; font-style: italic;">//before filter method will not be invoked for this</span>
   <span style="color: #66cc66;">&#125;</span>
<span style="color: #66cc66;">&#125;</span>;
?&amp;gt;</pre>
<p> </p>
<p>If you aren't familiar with rails, don't fret, the logic is simple. I'm using the protected $BEFORE_FILTER to tell my parent controller to execute the requireUser() method before it executes any of the action methods <strong>except</strong> actionMyExceptionAction(). You can change the "except" key to "only" to change from a black-list to a white-list. Where is the requireUser() method? Well, If you haven't fallen asleep yet, you should know that requireUser() resides in our ApplicationController. How does this code work?</p>
<p></code><span style="font-size: 15px; font-weight: bold; color: #570E00; font-family:Georgia,">The beauty is in PHP's reflection class.</span></p>
<p> </p>
<p><code></p>
<pre class="php">&lt;!--p
<span style="color: #808080; font-style: italic;">//instaniate our ApplicationController before</span>
<span style="color: #808080; font-style: italic;">//the current page's action is invoked</span>
<span style="color: #0000ff;">$thi</span>--&gt;<span style="color: #006600;">applicationController</span> = <span style="color: #000000; font-weight: bold;">new</span> ApplicationController<span style="color: #66cc66;">&#40;</span><span style="color: #0000ff;">$this</span><span style="color: #66cc66;">&#41;</span>;
<span style="color: #808080; font-style: italic;">//run before filter</span>
<span style="color: #0000ff;">$this</span>-&amp;gt;filter<span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">'before'</span><span style="color: #66cc66;">&#41;</span>;
&nbsp;
<span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000000; font-weight: bold;">function</span> filter<span style="color: #66cc66;">&#40;</span><span style="color: #0000ff;">$temporality</span><span style="color: #66cc66;">&#41;</span>
<span style="color: #66cc66;">&#123;</span>
  <span style="color: #0000ff;">$filter</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;">$temporality</span>.<span style="color: #ff0000;">'_FILTER'</span><span style="color: #66cc66;">&#41;</span>;
  <span style="color: #0000ff;">$controllerName</span> = <span style="color: #0000ff;">$this</span>-&amp;gt;getControllerName<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>.<span style="color: #ff0000;">'Controller'</span>;
  <span style="color: #0000ff;">$controller</span> = <span style="color: #000000; font-weight: bold;">new</span> ReflectionClass<span style="color: #66cc66;">&#40;</span><span style="color: #0000ff;">$controllerName</span><span style="color: #66cc66;">&#41;</span>;
&nbsp;
  <span style="color: #b1b100;">foreach</span> <span style="color: #66cc66;">&#40;</span><span style="color: #0000ff;">$controller</span>-&amp;gt;getProperties<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span> <span style="color: #b1b100;">as</span> <span style="color: #0000ff;">$property</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;">$property</span>-&amp;gt;name === <span style="color: #0000ff;">$filter</span><span style="color: #66cc66;">&#41;</span>
    <span style="color: #66cc66;">&#123;</span>
      <span style="color: #0000ff;">$filter</span> = <span style="color: #0000ff;">$this</span>-&amp;gt;<span style="color: #66cc66;">&#123;</span><span style="color: #0000ff;">$property</span>-&amp;gt;name<span style="color: #66cc66;">&#125;</span>;
      <span style="color: #b1b100;">if</span> <span style="color: #66cc66;">&#40;</span>!<a style="text-decoration: none;" href="http://www.php.net/is_array"><span style="color: #000066;">is_array</span></a><span style="color: #66cc66;">&#40;</span><span style="color: #0000ff;">$filter</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>
      <span style="color: #66cc66;">&#123;</span>
 	<a style="text-decoration: none;" href="http://www.php.net/settype"><span style="color: #000066;">settype</span></a><span style="color: #66cc66;">&#40;</span><span style="color: #0000ff;">$filter</span>, <span style="color: #ff0000;">'array'</span><span style="color: #66cc66;">&#41;</span>;
 	<span style="color: #0000ff;">$filter</span><span style="color: #66cc66;">&#91;</span><span style="color: #0000ff;">$filter</span><span style="color: #66cc66;">&#91;</span><span style="color: #cc66cc;">0</span><span style="color: #66cc66;">&#93;</span><span style="color: #66cc66;">&#93;</span> = <span style="color: #0000ff;">$filter</span><span style="color: #66cc66;">&#91;</span><span style="color: #cc66cc;">0</span><span style="color: #66cc66;">&#93;</span>;
 	<a style="text-decoration: none;" href="http://www.php.net/unset"><span style="color: #000066;">unset</span></a><span style="color: #66cc66;">&#40;</span><span style="color: #0000ff;">$filter</span><span style="color: #66cc66;">&#91;</span><span style="color: #cc66cc;">0</span><span style="color: #66cc66;">&#93;</span><span style="color: #66cc66;">&#41;</span>;
      <span style="color: #66cc66;">&#125;</span>
&nbsp;
      <span style="color: #b1b100;">foreach</span> <span style="color: #66cc66;">&#40;</span><span style="color: #0000ff;">$filter</span> <span style="color: #b1b100;">as</span> <span style="color: #0000ff;">$method</span> =&amp;gt; <span style="color: #0000ff;">$options</span><span style="color: #66cc66;">&#41;</span>
      <span style="color: #66cc66;">&#123;</span>
 	<span style="color: #0000ff;">$action</span> = <span style="color: #0000ff;">$this</span>-&amp;gt;currentAction<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
 	<span style="color: #b1b100;">if</span><span style="color: #66cc66;">&#40;</span>!<a style="text-decoration: none;" href="http://www.php.net/is_array"><span style="color: #000066;">is_array</span></a><span style="color: #66cc66;">&#40;</span><span style="color: #0000ff;">$options</span><span style="color: #66cc66;">&#41;</span> || <span style="color: #66cc66;">&#40;</span>!<a style="text-decoration: none;" href="http://www.php.net/isset"><span style="color: #000066;">isset</span></a><span style="color: #66cc66;">&#40;</span><span style="color: #0000ff;">$options</span><span style="color: #66cc66;">&#91;</span><span style="color: #ff0000;">'only'</span><span style="color: #66cc66;">&#93;</span><span style="color: #66cc66;">&#41;</span>
          &amp;amp;&amp;amp; !<a style="text-decoration: none;" href="http://www.php.net/isset"><span style="color: #000066;">isset</span></a><span style="color: #66cc66;">&#40;</span><span style="color: #0000ff;">$options</span><span style="color: #66cc66;">&#91;</span><span style="color: #ff0000;">'except'</span><span style="color: #66cc66;">&#93;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>
	      <span style="color: #0000ff;">$this</span>-&amp;gt;<span style="color: #0000ff;">$method</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
	<span style="color: #b1b100;">elseif</span> <span style="color: #66cc66;">&#40;</span><a style="text-decoration: none;" href="http://www.php.net/isset"><span style="color: #000066;">isset</span></a><span style="color: #66cc66;">&#40;</span><span style="color: #0000ff;">$options</span><span style="color: #66cc66;">&#91;</span><span style="color: #ff0000;">'only'</span><span style="color: #66cc66;">&#93;</span><span style="color: #66cc66;">&#41;</span>
          &amp;amp;&amp;amp; <a style="text-decoration: none;" href="http://www.php.net/in_array"><span style="color: #000066;">in_array</span></a><span style="color: #66cc66;">&#40;</span><span style="color: #0000ff;">$action</span>, <span style="color: #0000ff;">$options</span><span style="color: #66cc66;">&#91;</span><span style="color: #ff0000;">'only'</span><span style="color: #66cc66;">&#93;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>
	      <span style="color: #0000ff;">$this</span>-&amp;gt;<span style="color: #0000ff;">$method</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
	<span style="color: #b1b100;">elseif</span> <span style="color: #66cc66;">&#40;</span><a style="text-decoration: none;" href="http://www.php.net/isset"><span style="color: #000066;">isset</span></a><span style="color: #66cc66;">&#40;</span><span style="color: #0000ff;">$options</span><span style="color: #66cc66;">&#91;</span><span style="color: #ff0000;">'except'</span><span style="color: #66cc66;">&#93;</span><span style="color: #66cc66;">&#41;</span>
          &amp;amp;&amp;amp; !<a style="text-decoration: none;" href="http://www.php.net/in_array"><span style="color: #000066;">in_array</span></a><span style="color: #66cc66;">&#40;</span><span style="color: #0000ff;">$action</span>, <span style="color: #0000ff;">$options</span><span style="color: #66cc66;">&#91;</span><span style="color: #ff0000;">'except'</span><span style="color: #66cc66;">&#93;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>
	      <span style="color: #0000ff;">$this</span>-&amp;gt;<span style="color: #0000ff;">$method</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
      <span style="color: #66cc66;">&#125;</span>
    <span style="color: #66cc66;">&#125;</span>
  <span style="color: #66cc66;">&#125;</span>
<span style="color: #66cc66;">&#125;</span>
?&amp;gt;</pre>
<p> </p>
<p>Basically, we use reflection on our GamesController and then find any properties that match our filter name (BEFORE_FILTER). We make sure to turn it into an array if it isn't (which means we can do BEFORE_FILTER = 'requireUser';), and then invoke the method based on whether or not it fits the description. If "only" and "except" do not exist then it should be invoked. Otherwise, it should not be invoked if the current page's action is not in the "only" array or if it is in the "except" array. As you can see, you simply need to add protected $AFTER_FILTER = 'someMethodHere'; to have an after filter invoke some method. There you have it!</p>
<p></code>This concludes our crazy talk of making your framework more like rails (or at least adding bits and pieces to make your life easier).</p>
<p> </p>
<p></code></p>
]]></content:encoded>
			<wfw:commentRss>http://www.derivante.com/2008/12/08/php-on-rails/feed/</wfw:commentRss>
		<slash:comments>2</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! -->
