<?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; Kien La</title>
	<atom:link href="http://www.derivante.com/author/kla/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.derivante.com</link>
	<description>to obtain or receive from a source</description>
	<lastBuildDate>Mon, 26 Apr 2010 18:44:42 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.2.1</generator>
<xhtml:meta xmlns:xhtml="http://www.w3.org/1999/xhtml" name="robots" content="noindex" />
		<item>
		<title>PHP ActiveRecord 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>
	</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! -->
