<?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</title>
	<atom:link href="http://www.derivante.com/category/php/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>Extensible PHP Caching Library</title>
		<link>http://www.derivante.com/2010/04/23/extensible-php-caching-library/</link>
		<comments>http://www.derivante.com/2010/04/23/extensible-php-caching-library/#comments</comments>
		<pubDate>Fri, 23 Apr 2010 17:31:43 +0000</pubDate>
		<dc:creator>Justin Leider</dc:creator>
				<category><![CDATA[PHP]]></category>
		<category><![CDATA[architecture]]></category>
		<category><![CDATA[Cache]]></category>
		<category><![CDATA[Class]]></category>
		<category><![CDATA[Drupal]]></category>
		<category><![CDATA[Flexibility]]></category>
		<category><![CDATA[Library]]></category>
		<category><![CDATA[memcached]]></category>
		<category><![CDATA[mysql]]></category>

		<guid isPermaLink="false">http://www.derivante.com/?p=765</guid>
		<description><![CDATA[Everyone has probably already seen every caching class there ever was and ever will be. However, when I was searching for a class that could easily be switched from one data store to the next I couldn't find a thing. (&#8230;)</p><p><a href="http://www.derivante.com/2010/04/23/extensible-php-caching-library/">Read the rest of this entry &#187;</a></p>]]></description>
			<content:encoded><![CDATA[<p>Everyone has probably already seen every caching class there ever was and ever will be. However, when I was searching for a class that could easily be switched from one data store to the next I couldn't find a thing. Every caching class I seemed to come by was written specifically for a single back end and with their predefined static keys/column names. Well, those silly restrictions have come to an end!</p>
<p>I present to you the <a title="PHP Caching Library" href="http://github.com/jleider/extensible-php-caching-library" target="_blank">Extensible PHP Caching Library</a> (Hosted at GitHub) - This collection of classes makes it easy to customize key or column names to your needs as well as switch from one data store to another. This extensibility is baked into the core of the library via an abstract class. This abstract Cache class takes an associative array of keys/columns and determines how to use those keys based on the type of cache back end you are using. For example: Suppose you are using a RDBMS such as MySQL. In this case the associative array will be parsed and the query built such that the key is the column name and the value is what you want to query on. However, if you chose to use a NoSQL key/value store such as Memcache then the associative array is sorted and imploded to create a single string as the key.</p>
<p>Since we always specify the key as an associative array, switching between different data stores is as simple as changing the Class name from say MCache to SQLCache in your code. Nothing else is required to change data stores. Keys, expiration dates, and data processing all stay the same and all functions are called with the same arguments. This functionality is attributed to the abstract base Cache class which guides and regulates the inheriting classes.</p>
<p>Lets get to some examples:</p>
<p>In the following example we create a new SQLCache object and pass it our associative array of keys and values. Check if there is cached data, if there isnt then do something to generate the data and then cache it. Notice how you dont have to pass the key in again when setting the cache. The key is stored within the object so you can get and set as many times as you need without having to set the key every time. Lastly we delete the cache.</p>
<pre class="php"><span style="color: #808080; font-style: italic;">// Get Cache</span>
<span style="color: #0000ff;">$cache</span> = <span style="color: #000000; font-weight: bold;">new</span> SQLCache<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;">'column1'</span> =&gt; <span style="color: #cc66cc;">123</span>, <span style="color: #ff0000;">'column2'</span> =&gt; <span style="color: #ff0000;">'blah'</span>, … <span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>;
<span style="color: #0000ff;">$output</span> = <span style="color: #0000ff;">$cache</span>-&gt;<span style="color: #006600;">getCache</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
&nbsp;
<span style="color: #b1b100;">if</span><span style="color: #66cc66;">&#40;</span>!<span style="color: #0000ff;">$output</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span>
  <span style="color: #808080; font-style: italic;">// Do something to generate data</span>
  <span style="color: #0000ff;">$output</span> = <span style="color: #ff0000;">'some datas'</span>;
  <span style="color: #808080; font-style: italic;">// Set Cache</span>
  <span style="color: #b1b100;">if</span><span style="color: #66cc66;">&#40;</span><span style="color: #0000ff;">$cacheNow</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span>
    <span style="color: #808080; font-style: italic;">// Force a write to cache now</span>
    <span style="color: #0000ff;">$cache</span>-&gt;<span style="color: #006600;">setCache</span><span style="color: #66cc66;">&#40;</span><span style="color: #0000ff;">$output</span>, <span style="color: #ff0000;">'+1 day'</span>, <span style="color: #000000; font-weight: bold;">true</span><span style="color: #66cc66;">&#41;</span>;
  <span style="color: #66cc66;">&#125;</span> <span style="color: #b1b100;">else</span> <span style="color: #66cc66;">&#123;</span>
    <span style="color: #808080; font-style: italic;">// Setting no time defaults to time() + 86400, one day from now.</span>
    <span style="color: #0000ff;">$cache</span>-&gt;<span style="color: #006600;">setCache</span><span style="color: #66cc66;">&#40;</span><span style="color: #0000ff;">$output</span><span style="color: #66cc66;">&#41;</span>;
  <span style="color: #66cc66;">&#125;</span>
<span style="color: #66cc66;">&#125;</span>
&nbsp;
<a style="text-decoration: none;" href="http://www.php.net/print"><span style="color: #000066;">print</span></a> <span style="color: #0000ff;">$output</span>;
&nbsp;
<span style="color: #808080; font-style: italic;">// Delete the cache</span>
<span style="color: #0000ff;">$cache</span>-&gt;<span style="color: #006600;">deleteCache</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
&nbsp;</pre>
<p>If ever you needed to update this caching strategy to include Memcached the only change would be to change SQLCache to Mcache:</p>
<pre class="php"><span style="color: #0000ff;">$cache</span> = <span style="color: #000000; font-weight: bold;">new</span> SQLCache<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;">'column1'</span> =&gt; <span style="color: #cc66cc;">123</span>, <span style="color: #ff0000;">'column2'</span> =&gt; <span style="color: #ff0000;">'blah'</span>, … <span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>;
to
<span style="color: #0000ff;">$cache</span> = <span style="color: #000000; font-weight: bold;">new</span> MCache<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;">'key1'</span> =&gt; <span style="color: #cc66cc;">123</span>, <span style="color: #ff0000;">'key2'</span> =&gt; <span style="color: #ff0000;">'blah'</span>, … <span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>;
&nbsp;</pre>
<p>The column and key names are arbitrary and may be set to anything you want to name it. For SQL caches make sure you create a <a title="Example Cache Table Schema" href="http://github.com/jleider/extensible-php-caching-library" target="_blank">cache table</a> that has the corresponding column names and that they are indexed optimally.</p>
<p>While I have only created classes for SQL (MySQL - since there is a LIMIT 1), Memcached and File based caching, the base class can be extended to include any key/value store or any database with columns (MongoDB, CouchDB, Tokyo, Postgress, Oracle, etc). Just update the back end calls and you are good to go.</p>
<p>For anyone who updates or adds functionality please let me know so I can give credit where credit is due. This library is available under the <a title="GNU Lesser General Public License" href="http://www.gnu.org/licenses/lgpl.html" target="_blank">LGPLv3</a>.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.derivante.com/2010/04/23/extensible-php-caching-library/feed/</wfw:commentRss>
		<slash:comments>10</slash:comments>
		</item>
		<item>
		<title>PHP Content Rating / Confidence</title>
		<link>http://www.derivante.com/2009/09/01/php-content-rating-confidence/</link>
		<comments>http://www.derivante.com/2009/09/01/php-content-rating-confidence/#comments</comments>
		<pubDate>Tue, 01 Sep 2009 21:15:51 +0000</pubDate>
		<dc:creator>Clay vanSchalkwijk</dc:creator>
				<category><![CDATA[PHP]]></category>
		<category><![CDATA[Web Technology]]></category>
		<category><![CDATA[content analysis]]></category>
		<category><![CDATA[rating]]></category>

		<guid isPermaLink="false">http://www.derivante.com/?p=717</guid>
		<description><![CDATA[For those web masters dealing with user feedback looking to weight content finding the right algorithm can be challenging. From experience, there is going to be no out of the box solution since each site and the requirements will be (&#8230;)</p><p><a href="http://www.derivante.com/2009/09/01/php-content-rating-confidence/">Read the rest of this entry &#187;</a></p>]]></description>
			<content:encoded><![CDATA[<p>For those web masters dealing with user feedback looking to weight content finding the right algorithm can be challenging. From experience, there is going to be no out of the box solution since each site and the requirements will be unique.  Getting started and putting a solid foundation is the first step and of course, refining over time to get just the right recipe. The following is a binomial proportion confidence interval (what?).  It is a PHP implementation using the Wilson Score Interval to weight the feedback.</p>
<p><span id="more-717"></span></p>
<pre class="php"><span style="color: #000000; font-weight: bold;">class</span> Rating
<span style="color: #66cc66;">&#123;</span>
  <span style="color: #000000; font-weight: bold;">public</span> <a style="text-decoration: none;" href="http://www.php.net/static"><span style="color: #000066;">static</span></a> <span style="color: #000000; font-weight: bold;">function</span> ratingAverage<span style="color: #66cc66;">&#40;</span><span style="color: #0000ff;">$positive</span>, <span style="color: #0000ff;">$total</span>, <span style="color: #0000ff;">$power</span> = <span style="color: #ff0000;">'0.05'</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;">$total</span> == <span style="color: #cc66cc;">0</span><span style="color: #66cc66;">&#41;</span>
      <span style="color: #b1b100;">return</span> <span style="color: #cc66cc;">0</span>;
&nbsp;
    <span style="color: #0000ff;">$z</span> = Rating::<span style="color: #006600;">pnormaldist</span><span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">1</span>-<span style="color: #0000ff;">$power</span>/<span style="color: #cc66cc;">2</span>,<span style="color: #cc66cc;">0</span>,<span style="color: #cc66cc;">1</span><span style="color: #66cc66;">&#41;</span>;
    <span style="color: #0000ff;">$p</span> = <span style="color: #cc66cc;">1.0</span> * <span style="color: #0000ff;">$positive</span> / <span style="color: #0000ff;">$total</span>;
    <span style="color: #0000ff;">$s</span> = <span style="color: #66cc66;">&#40;</span><span style="color: #0000ff;">$p</span> + <span style="color: #0000ff;">$z</span>*<span style="color: #0000ff;">$z</span>/<span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">2</span>*<span style="color: #0000ff;">$total</span><span style="color: #66cc66;">&#41;</span> - <span style="color: #0000ff;">$z</span> * <a style="text-decoration: none;" href="http://www.php.net/sqrt"><span style="color: #000066;">sqrt</span></a><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#40;</span><span style="color: #0000ff;">$p</span>*<span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">1</span>-<span style="color: #0000ff;">$p</span><span style="color: #66cc66;">&#41;</span>+<span style="color: #0000ff;">$z</span>*<span style="color: #0000ff;">$z</span>/<span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">4</span>*<span style="color: #0000ff;">$total</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>/<span style="color: #0000ff;">$total</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>/<span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">1</span>+<span style="color: #0000ff;">$z</span>*<span style="color: #0000ff;">$z</span>/<span style="color: #0000ff;">$total</span><span style="color: #66cc66;">&#41;</span>;
    <span style="color: #b1b100;">return</span> <span style="color: #0000ff;">$s</span>;
  <span style="color: #66cc66;">&#125;</span> 
&nbsp;
  <span style="color: #000000; font-weight: bold;">public</span> <a style="text-decoration: none;" href="http://www.php.net/static"><span style="color: #000066;">static</span></a> <span style="color: #000000; font-weight: bold;">function</span> pnormaldist<span style="color: #66cc66;">&#40;</span><span style="color: #0000ff;">$qn</span><span style="color: #66cc66;">&#41;</span>
  <span style="color: #66cc66;">&#123;</span>
    <span style="color: #0000ff;">$b</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.570796288</span>, <span style="color: #cc66cc;">0.03706987906</span>, <span style="color: #cc66cc;">-0</span>.8364353589e<span style="color: #cc66cc;">-3</span>,
      <span style="color: #cc66cc;">-0</span>.2250947176e<span style="color: #cc66cc;">-3</span>, <span style="color: #cc66cc;">0</span>.6841218299e<span style="color: #cc66cc;">-5</span>, <span style="color: #cc66cc;">0</span>.5824238515e<span style="color: #cc66cc;">-5</span>,
      <span style="color: #cc66cc;">-0</span>.104527497e<span style="color: #cc66cc;">-5</span>, <span style="color: #cc66cc;">0</span>.8360937017e<span style="color: #cc66cc;">-7</span>, <span style="color: #cc66cc;">-0</span>.3231081277e<span style="color: #cc66cc;">-8</span>,
      <span style="color: #cc66cc;">0</span>.3657763036e<span style="color: #cc66cc;">-10</span>, <span style="color: #cc66cc;">0</span>.6936233982e<span style="color: #cc66cc;">-12</span><span style="color: #66cc66;">&#41;</span>;
&nbsp;
    <span style="color: #b1b100;">if</span> <span style="color: #66cc66;">&#40;</span><span style="color: #0000ff;">$qn</span> &lt; <span style="color: #cc66cc;">0.0</span> || <span style="color: #cc66cc;">1.0</span> &lt; <span style="color: #0000ff;">$qn</span><span style="color: #66cc66;">&#41;</span>
      <span style="color: #b1b100;">return</span> <span style="color: #cc66cc;">0.0</span>;
&nbsp;
    <span style="color: #b1b100;">if</span> <span style="color: #66cc66;">&#40;</span><span style="color: #0000ff;">$qn</span> == <span style="color: #cc66cc;">0.5</span><span style="color: #66cc66;">&#41;</span>
      <span style="color: #b1b100;">return</span> <span style="color: #cc66cc;">0.0</span>;
&nbsp;
    <span style="color: #0000ff;">$w1</span> = <span style="color: #0000ff;">$qn</span>;
&nbsp;
    <span style="color: #b1b100;">if</span> <span style="color: #66cc66;">&#40;</span><span style="color: #0000ff;">$qn</span> &gt; <span style="color: #cc66cc;">0.5</span><span style="color: #66cc66;">&#41;</span>
      <span style="color: #0000ff;">$w1</span> = <span style="color: #cc66cc;">1.0</span> - <span style="color: #0000ff;">$w1</span>;
&nbsp;
    <span style="color: #0000ff;">$w3</span> = - <a style="text-decoration: none;" href="http://www.php.net/log"><span style="color: #000066;">log</span></a><span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">4.0</span> * <span style="color: #0000ff;">$w1</span> * <span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">1.0</span> - <span style="color: #0000ff;">$w1</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>;
    <span style="color: #0000ff;">$w1</span> = <span style="color: #0000ff;">$b</span><span style="color: #66cc66;">&#91;</span><span style="color: #cc66cc;">0</span><span style="color: #66cc66;">&#93;</span>;
&nbsp;
    <span style="color: #b1b100;">for</span> <span style="color: #66cc66;">&#40;</span><span style="color: #0000ff;">$i</span> = <span style="color: #cc66cc;">1</span>;<span style="color: #0000ff;">$i</span> &lt;= <span style="color: #cc66cc;">10</span>; <span style="color: #0000ff;">$i</span>++<span style="color: #66cc66;">&#41;</span>
      <span style="color: #0000ff;">$w1</span> += <span style="color: #0000ff;">$b</span><span style="color: #66cc66;">&#91;</span><span style="color: #0000ff;">$i</span><span style="color: #66cc66;">&#93;</span> * <a style="text-decoration: none;" href="http://www.php.net/pow"><span style="color: #000066;">pow</span></a><span style="color: #66cc66;">&#40;</span><span style="color: #0000ff;">$w3</span>,<span style="color: #0000ff;">$i</span><span style="color: #66cc66;">&#41;</span>;
&nbsp;
    <span style="color: #b1b100;">if</span> <span style="color: #66cc66;">&#40;</span><span style="color: #0000ff;">$qn</span> &gt; <span style="color: #cc66cc;">0.5</span><span style="color: #66cc66;">&#41;</span>
      <span style="color: #b1b100;">return</span> <a style="text-decoration: none;" href="http://www.php.net/sqrt"><span style="color: #000066;">sqrt</span></a><span style="color: #66cc66;">&#40;</span><span style="color: #0000ff;">$w1</span> * <span style="color: #0000ff;">$w3</span><span style="color: #66cc66;">&#41;</span>;
&nbsp;
    <span style="color: #b1b100;">return</span> - <a style="text-decoration: none;" href="http://www.php.net/sqrt"><span style="color: #000066;">sqrt</span></a><span style="color: #66cc66;">&#40;</span><span style="color: #0000ff;">$w1</span> * <span style="color: #0000ff;">$w3</span><span style="color: #66cc66;">&#41;</span>;
  <span style="color: #66cc66;">&#125;</span>
<span style="color: #66cc66;">&#125;</span>
&nbsp;</pre>
<p>The function takes 3 parameters: the positive votes, total votes, and the power. The power can be adjusted, 0.10 to have a 95% chance that your lower bound is correct, 0.05 to have a 97.5% chance, etc.  Sample usage:</p>
<pre class="php">sample<span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">1</span>,<span style="color: #cc66cc;">0</span><span style="color: #66cc66;">&#41;</span>;
sample<span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">100</span>,<span style="color: #cc66cc;">50</span><span style="color: #66cc66;">&#41;</span>;
sample<span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">250</span>,<span style="color: #cc66cc;">100</span><span style="color: #66cc66;">&#41;</span>;
sample<span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">1000</span>,<span style="color: #cc66cc;">500</span><span style="color: #66cc66;">&#41;</span>;
&nbsp;
<span style="color: #000000; font-weight: bold;">function</span> sample<span style="color: #66cc66;">&#40;</span><span style="color: #0000ff;">$p</span>,<span style="color: #0000ff;">$n</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> Rating::<span style="color: #006600;">ratingAverage</span><span style="color: #66cc66;">&#40;</span><span style="color: #0000ff;">$p</span>,<span style="color: #0000ff;">$p</span>+<span style="color: #0000ff;">$n</span><span style="color: #66cc66;">&#41;</span>;
<span style="color: #66cc66;">&#125;</span>
&nbsp;</pre>
<p>Output:</p>
<table border="0" cellpadding="2" cellspacing="0" border="1">
<tbody>
<tr>
<td>Positive</td>
<td>Negative</td>
<td>Score</td>
</tr>
<tr>
<td>1</td>
<td>0</td>
<td>0.20654931654388</td>
</tr>
<tr>
<td>100</td>
<td>50</td>
<td>0.58789756740385</td>
</tr>
<tr>
<td>250</td>
<td>100</td>
<td>0.6648317184611</td>
</tr>
<tr>
<td>1000</td>
<td>500</td>
<td>0.6424116916199</td>
</tr>
</tbody>
</table>
<p>When dealing with sites like Reddit, Digg, and the like  you have a certain "freshness" element.  The above solution might be a working model for the entire span of the site, but for that front page element you will need to implement some form of "gravity".  This can be done by taking the raw score and decaying it over time, like so:</p>
<pre class="php">&nbsp;
<span style="color: #000000; font-weight: bold;">class</span> Rating
<span style="color: #66cc66;">&#123;</span>
  ...
  <span style="color: #000000; font-weight: bold;">public</span> <a style="text-decoration: none;" href="http://www.php.net/static"><span style="color: #000066;">static</span></a> <span style="color: #000000; font-weight: bold;">function</span> gravityRating<span style="color: #66cc66;">&#40;</span><span style="color: #0000ff;">$positive</span>, <span style="color: #0000ff;">$total</span>, <span style="color: #0000ff;">$time</span>, <span style="color: #0000ff;">$power</span> = <span style="color: #ff0000;">'0.05'</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;">$total</span> == <span style="color: #cc66cc;">0</span><span style="color: #66cc66;">&#41;</span>
      <span style="color: #b1b100;">return</span> <span style="color: #cc66cc;">0</span>;
    <span style="color: #b1b100;">return</span> <span style="color: #66cc66;">&#40;</span>Rating::<span style="color: #006600;">ratingAverage</span><span style="color: #66cc66;">&#40;</span><span style="color: #0000ff;">$positive</span>, <span style="color: #0000ff;">$total</span>, <span style="color: #0000ff;">$power</span><span style="color: #66cc66;">&#41;</span> / <a style="text-decoration: none;" href="http://www.php.net/pow"><span style="color: #000066;">pow</span></a><span style="color: #66cc66;">&#40;</span><span style="color: #0000ff;">$time</span>,<span style="color: #cc66cc;">0.5</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>
&nbsp;
sample<span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">100</span>,<span style="color: #cc66cc;">50</span>,<span style="color: #ff0000;">'0.5'</span><span style="color: #66cc66;">&#41;</span>;
sample<span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">100</span>,<span style="color: #cc66cc;">50</span>,<span style="color: #ff0000;">'1'</span><span style="color: #66cc66;">&#41;</span>;
sample<span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">100</span>,<span style="color: #cc66cc;">50</span>,<span style="color: #ff0000;">'4'</span><span style="color: #66cc66;">&#41;</span>;
sample<span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">100</span>,<span style="color: #cc66cc;">50</span>,<span style="color: #ff0000;">'8'</span><span style="color: #66cc66;">&#41;</span>;
sample<span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">100</span>,<span style="color: #cc66cc;">50</span>,<span style="color: #ff0000;">'24'</span><span style="color: #66cc66;">&#41;</span>;
&nbsp;
<span style="color: #000000; font-weight: bold;">function</span> sample<span style="color: #66cc66;">&#40;</span><span style="color: #0000ff;">$p</span>,<span style="color: #0000ff;">$n</span>,<span style="color: #0000ff;">$time</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> Rating::<span style="color: #006600;">gravityRating</span><span style="color: #66cc66;">&#40;</span><span style="color: #0000ff;">$p</span>,<span style="color: #0000ff;">$p</span>+<span style="color: #0000ff;">$n</span>,<span style="color: #0000ff;">$time</span><span style="color: #66cc66;">&#41;</span>.<span style="color: #ff0000;">&quot;<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span>;
<span style="color: #66cc66;">&#125;</span>
&nbsp;</pre>
<p>In the example above, $time represents the age (in hours) and you can see the decay in the output:</p>
<p>0.83141271310867<br />
0.58789756740385<br />
0.29394878370192<br />
0.20785317827717<br />
0.12000408843024</p>
<p>My recommendation would be to "cap" the time to stop decay after a fixed period such as 12 or 24 hours to stop the initial boost of fresh content and let it normalize quickly.  The rate of decay of course, can be adjusted as fast or as slow as you want and again the individual weighting you want to apply will vary from site to site.  Depending on the volatility of your content, a front page "freshness" that will encompass a week would not merit a 12 hour decay, but rather a week long decay.   Hopefully the above code is enough to get started with content rating and making better use of user feedback and can help lead web masters to making a more intelligent calculation of their content beyond the traditional "5-Star Rating".</p>
<p>[ c9maji2tvz ]</p>
]]></content:encoded>
			<wfw:commentRss>http://www.derivante.com/2009/09/01/php-content-rating-confidence/feed/</wfw:commentRss>
		<slash:comments>7</slash:comments>
		</item>
		<item>
		<title>PHP Google Analytics API</title>
		<link>http://www.derivante.com/2009/07/08/php-google-analytics-api/</link>
		<comments>http://www.derivante.com/2009/07/08/php-google-analytics-api/#comments</comments>
		<pubDate>Wed, 08 Jul 2009 15:19:50 +0000</pubDate>
		<dc:creator>Clay vanSchalkwijk</dc:creator>
				<category><![CDATA[PHP]]></category>
		<category><![CDATA[Web Technology]]></category>
		<category><![CDATA[Analytics]]></category>
		<category><![CDATA[API]]></category>
		<category><![CDATA[Google]]></category>

		<guid isPermaLink="false">http://www.derivante.com/?p=707</guid>
		<description><![CDATA[GAPI 1.3 released this past month. First, read the Google Analytics Data API Reference and then read up on dimensions, metrics, and valid combinations of the two.  The quotas apply to a single web property, so each analytics profile (site1, (&#8230;)</p><p><a href="http://www.derivante.com/2009/07/08/php-google-analytics-api/">Read the rest of this entry &#187;</a></p>]]></description>
			<content:encoded><![CDATA[<p><a href="http://code.google.com/p/gapi-google-analytics-php-interface/">GAPI 1.3</a> released this past month.  First, read the <a href="http://code.google.com/apis/analytics/docs/gdata/gdataReference.html">Google Analytics Data API Reference</a> and then read up on <a href="http://code.google.com/apis/analytics/docs/gdata/gdataReferenceDimensionsMetrics.html">dimensions, metrics, and valid combinations</a> of the two.  The quotas apply to a single web property, so each analytics profile (site1, site2, etc) are subject to their own individual quotas.   There is no per user account limit for accessing the API.    If you plan on making any kind of on demand application I suggest you query in bulk and cache the results locally (analytics is not real time anyways).</p>
<p><span id="more-707"></span></p>
<p><strong>API Features</strong></p>
<ul>
<li>Supports CURL and fopen HTTP access methods, with autodetection </li>
<li>PHP arrays for Google Analytics metrics and dimensions </li>
<li>Account data object mapping - get methods for parameters </li>
<li>Report data object mapping - get methods for metrics and parameters </li>
<li>Easy filtering, use a GAPI query language for Google Analytics filters </li>
<li>Full PHP5 Object Oriented code, ready for use in your PHP application </li>
</ul>
<p><br></p>
<p><strong>Google Quotas</strong></p>
<ul>
<li>The quota applies to a <em>single web property</em></li>
<li>10,000 requests per 24 hours</li>
<li>100 requests in any given 10-second period</li>
<li>A query is also limited to pagination limits of 10,000 entries per feed,   with a default response of 1,000 entries</li>
</ul>
<p><br></p>
<p>While the broad set of data they have is extremely useful to pull down to do your own processing on, I find the custom visitor segments with the API a lot more interesting.    If you have a user based site with registration and collect information on your users, you can pull this data back out to determine a lot of information about your user.   A lot of tools are already built into Analytics for reporting but the ability to mash this data up to provide custom reporting for your users (customers) without giving them access to analytics is key to delivering metrics and performance</p>
]]></content:encoded>
			<wfw:commentRss>http://www.derivante.com/2009/07/08/php-google-analytics-api/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<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>
		<item>
		<title>PHP/KML Polyline Simplification with Douglas-Peucker</title>
		<link>http://www.derivante.com/2009/04/20/phpkml-polyline-simplification-with-douglas-peucker/</link>
		<comments>http://www.derivante.com/2009/04/20/phpkml-polyline-simplification-with-douglas-peucker/#comments</comments>
		<pubDate>Tue, 21 Apr 2009 02:41:16 +0000</pubDate>
		<dc:creator>Clay vanSchalkwijk</dc:creator>
				<category><![CDATA[PHP]]></category>
		<category><![CDATA[Web Technology]]></category>

		<guid isPermaLink="false">http://www.derivante.com/?p=331</guid>
		<description><![CDATA[Quality GIS data sometimes comes with a lot more precision than what is usable for Google Maps (or other mapping software). The problem lies in the number of points representing a polygon that you want to overlay. A county representation (&#8230;)</p><p><a href="http://www.derivante.com/2009/04/20/phpkml-polyline-simplification-with-douglas-peucker/">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" />Quality GIS data sometimes comes with a lot more precision than what is usable for Google Maps (or other mapping software). The problem lies in the number of points representing a polygon that you want to overlay. A county representation for a state might include 100,000 points that is not usable without some form of reduction. Luckily there is an algorithm that solves that problem, Douglas-Peucker.</p>
<p>The algorithm simplifies a polyline by removing vertices that do not contribute (sufficiently) to the overall shape. It is a recursive process which finds the most important vertices for every given reduction. First, the most basic reduction is assumed. A single segment connecting the beginning and end of the original polyline. This is when the recursion starts, the most significant vertex (the most distant) for this segment is found and, when the distance from this vertex to the segment exceeds the reduction tolerance, the segment is split into two sub-segments, each inheriting a subset of the original vertex list. Each segment continues to subdivide until none of the vertices in the local list are further away than the tolerance value.</p>
<p>There is a PHP class that does just this: <a href="http://www.fonant.com/demos/douglas_peucker/algorithm">Douglas-Peucker Polyline Simplification in PHP</a> by <a href="http://www.fonant.com/">Anthony Cartmell</a>. Based on the original quality of the data and tolerance level, I was able to achieve a 90-93% reduction in size. This reduction allows me to represent significantly more data at a reasonable performance level to clients. Keep in mind, that this reduction is removing data out of the coordinate array so the quality of your representation will go down with the tolerance and reduction being applied. I highly suggest that you play around with the tolerance until you find a good balance between data size and image quality.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.derivante.com/2009/04/20/phpkml-polyline-simplification-with-douglas-peucker/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>PHP GIS Functions</title>
		<link>http://www.derivante.com/2009/04/14/php-gis-functions/</link>
		<comments>http://www.derivante.com/2009/04/14/php-gis-functions/#comments</comments>
		<pubDate>Tue, 14 Apr 2009 17:35:53 +0000</pubDate>
		<dc:creator>Clay vanSchalkwijk</dc:creator>
				<category><![CDATA[PHP]]></category>
		<category><![CDATA[SQL]]></category>

		<guid isPermaLink="false">http://www.derivante.com/?p=313</guid>
		<description><![CDATA[I have been working a lot of with PHP and GIS consulting for CitySquares and the History Engine. I found searching for everything I needed to do basic processing &#38; Google Integration tedious and painful. So here is a collection (&#8230;)</p><p><a href="http://www.derivante.com/2009/04/14/php-gis-functions/">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" />I have been working a lot of with PHP and GIS consulting for <a href="http://citysquares.com">CitySquares </a>and the <a href="http://historyengine.richmond.edu/">History Engine</a>.  I found searching for everything I needed to do basic processing &amp; Google Integration tedious and painful.  So here is a collection of common functions that helped me get through the <em>massaging</em> of the data and ready for integration.</p>
<ul style="padding-left: 20px;padding-bottom: 10px;"> <strong><a href="http://www.derivante.com/files/phpgis.txt">pnPoly</a></strong> - Used to determine if a coordinate falls inside a polygon.<br />
<strong><a href="http://www.derivante.com/files/phpgis.txt">Centroid </a></strong>- Find the center of a polygon..<br />
<strong><a href="http://www.derivante.com/files/phpgis.txt">Area</a></strong> - Calculate the area of a polygon.<br />
<strong><a href="http://www.derivante.com/files/geocoder.txt">googleGeoCoder </a></strong> - Extracts GIS information from Google Maps from an address.<br />
<strong><a href="http://www.derivante.com/files/polyline.txt">PolylineEncoder</a></strong> - Takes a set of coordinates and encodes it for Google Maps.</ul>
<p>If you ran into the problem I did, which is that a lot of the data is coming in the form of shp/dbf files and needs to be parsed out to something friendlier either KML or CSV, there are a couple of solutions for that.  You can parse out the data with <a href="http://www.obviously.com/gis/shp2text/">shp2text</a> if your source coordinate format is already in lat/lng or if you have different coordinate system and use ArcGIS, you can try the plugin <a href="http://arcscripts.esri.com/details.asp?dbid=14273">Export to KML 2.5.3</a> to help with the exporting of data with the ESRI suite of products.</p>
<p>Once your data is in SQL, the following query is an example of distance sorting with SQL. You can grab a copy of the zip_codes database <a href="http://www.derivante.com/files/zip_codes.sql.gz">here</a> and play around with it.</p>
<pre style="padding-left: 20px;padding-bottom: 10px;" lang="sql">SELECT *,
sqrt((69.1 * ("37.6" - latitude)) * (69.1 * ("37.6" - latitude)) +
(53.0 * ("-77.6" - longitude)) * (53.0 * ("-77.6" - longitude)))
AS distance
FROM `zip_codes`
HAVING distance &lt; 10
ORDER BY distance ASC</pre>
]]></content:encoded>
			<wfw:commentRss>http://www.derivante.com/2009/04/14/php-gis-functions/feed/</wfw:commentRss>
		<slash:comments>5</slash:comments>
		</item>
		<item>
		<title>Drupal Scaling, not so much</title>
		<link>http://www.derivante.com/2009/02/25/drupal-scaling-not-so-much/</link>
		<comments>http://www.derivante.com/2009/02/25/drupal-scaling-not-so-much/#comments</comments>
		<pubDate>Wed, 25 Feb 2009 16:23:45 +0000</pubDate>
		<dc:creator>Clay vanSchalkwijk</dc:creator>
				<category><![CDATA[Framework]]></category>
		<category><![CDATA[PHP]]></category>

		<guid isPermaLink="false">http://www.derivante.com/?p=237</guid>
		<description><![CDATA[After working on the migration this week and moving CitySquares.com over to a new environment we ended the week with a load test from Soasta. Starting at several hundred concurrent users to a cap of 2500 simulated users on at (&#8230;)</p><p><a href="http://www.derivante.com/2009/02/25/drupal-scaling-not-so-much/">Read the rest of this entry &#187;</a></p>]]></description>
			<content:encoded><![CDATA[<p>After working on the migration this week and moving CitySquares.com over to a new environment we ended the week with a load test from <a href="http://www.soasta.com/" target="_blank">Soasta</a>.  Starting at several hundred concurrent users to a cap of 2500 simulated users on at once we were able to really pick apart the infrastructure.  Couple this with our own internal testing and tweaking with ab over the past few days I've come to the conclusion that Drupal does not scale so well when you're talking about traffic in the tens of millions of page views a month.  It's just not possible to get requests in and out fast enough without continually adding more boxes to the front end.  I tried every setup from mod_php to php-fcgi, worker, prefork, eaccelerator, xcache, and every configuration therein.</p>
<p>I realized that I was over-thinking the problem so I did a little test to just see how much overhead Drupal is adding.  I can execute ten times the number of queries natively through PHP and mysql_connect than if I were to execute those queries from within Drupal with just bootstrap included.  Apache answered requests and processed these several million round trips to the database much faster.  I understand that as a framework there is a lot being loaded, but a whole decimal point off when dealing with requests/sec is a huge overhead.</p>
<p>The better part of the last few days was spent gutting the inner workings of Drupal and removing as much of it as possible to lover overhead, reduce response time, and hopefully let us scale a little further on the hardware that we have.  As it is, traffic is growing a steady 40-50% month over month for a while now and we are already dealing with several million unique visitors a month.  That number is growing rapidly with no ceiling in the near future and it's unfortunate that at this point the only way to make this work is to break the upgrade path of Drupal.</p>
<p>Going through the code base, it's easy to see where so much of the bloat comes from.  With comments like: "TODO: remove this when we require at least PHP 4.4.0" there is a lot of backwards compatibility that is required to support such a large community.   I am seeing more and more Frameworks replace legacy CMS systems that provide a bare bone set of tools.   The agility, performance, and maintenance of a ground up site using frameworks like Symfony or Rails leads to longer development cycles and increased costs but there is a point where you stop putting a round peg in a square hole and realize that the cost of maintenance and development down the road will far exceed the up front investment.</p>
<p>I'm glad Drupal has been able to take us this far and I'm very grateful for the community behind it.  It's a pleasant surprise that everything is still working and hopefully we can get a few more miles out of it before we are ready to do the next iteration of the site.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.derivante.com/2009/02/25/drupal-scaling-not-so-much/feed/</wfw:commentRss>
		<slash:comments>1</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! -->
