<?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>Håkon Nessjøen &#187; php</title>
	<atom:link href="http://lunatic.no/category/php/feed/" rel="self" type="application/rss+xml" />
	<link>http://lunatic.no</link>
	<description>Just another life hacker</description>
	<lastBuildDate>Tue, 06 Dec 2011 22:39:25 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
<xhtml:meta xmlns:xhtml="http://www.w3.org/1999/xhtml" name="robots" content="noindex" />
		<item>
		<title>Simple ORM php-library for mySQL</title>
		<link>http://lunatic.no/2010/08/simple-orm-php-library-for-mysql/</link>
		<comments>http://lunatic.no/2010/08/simple-orm-php-library-for-mysql/#comments</comments>
		<pubDate>Mon, 23 Aug 2010 17:51:54 +0000</pubDate>
		<dc:creator>Håkon Nessjøen</dc:creator>
				<category><![CDATA[hobby]]></category>
		<category><![CDATA[php]]></category>
		<category><![CDATA[ActiveRecord]]></category>
		<category><![CDATA[ORM]]></category>

		<guid isPermaLink="false">http://lunatic.no/?p=132</guid>
		<description><![CDATA[<div style="display:inline;float:right;margin-left:1em"><g:plusone href="http://lunatic.no/2010/08/simple-orm-php-library-for-mysql/"></g:plusone></div>
I have been using Akelos both for professional and personal uses for a long time now. But sometimes I only need the ORM part of Akelos, and not the whole MVC shebang. So for a while ago, I made a ~40 lines php script that abstracted mysql queries in a easy to use class, much [...]]]></description>
			<content:encoded><![CDATA[<div style="display:inline;float:right;margin-left:1em"><g:plusone href="http://lunatic.no/2010/08/simple-orm-php-library-for-mysql/"></g:plusone></div>
<p>I have been using Akelos both for professional and personal uses for a long time now. But sometimes I only need the ORM part of Akelos, and not the whole MVC shebang. So for a while ago, I made a ~40 lines php script that abstracted mysql queries in a easy to use class, much alike how Akelos&#8217; AKActiveRecord works. But just much much simpler.</p>
<p>Then for a few days ago I thought, why not extend (rewrite) it, and add support for relations and serialization, etc; but still keep it simple enough to be one single small include for my small projects, with only two lines of configuration needed?</p>
<p>So here is initial version under LGPL license: <a href='http://lunatic.no/wp-content/uploads/2010/08/SimpleActiveRecord-v0.1.tar.gz'>SimpleActiveRecord-v0.1.tar.gz</a>.</p>
<p>In the tarball above, you will find everything you need in the file &#8220;simpleactiverecord.php&#8221;, but included is also a test suite using the simpletest library.</p>
<p>To use the orm functions, all you need is the simpleactiverecord.php. But if you are going to extend it, or check that all the functionality is intact, you can use the included tests to check that the all the desired functionality is intact after your changes. To run the tests, just invoke ./test.sh from the command line. (if you are using debian/ubuntu, you need to have the php-cli package installed to do this)</p>
<p>To begin; I will add some phpDoc in the future! It&#8217;s not a finished project.</p>
<p><strong>Examples</strong></p>
<p>Here are some examples and explanations of how you can use this simple ORM:</p>
<p>First the mysql database I use for these examples:</p>

<div class="wp_syntax"><div class="code"><pre class="sql" style="font-family:monospace;"><span style="color: #993333; font-weight: bold;">CREATE</span> <span style="color: #993333; font-weight: bold;">TABLE</span> <span style="color: #ff0000;">`users`</span> <span style="color: #66cc66;">&#40;</span>
  <span style="color: #ff0000;">`id`</span> <span style="color: #993333; font-weight: bold;">BIGINT</span><span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">20</span><span style="color: #66cc66;">&#41;</span> <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;">AUTO_INCREMENT</span><span style="color: #66cc66;">,</span>
  <span style="color: #ff0000;">`customer_id`</span> <span style="color: #993333; font-weight: bold;">INT</span><span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">11</span><span style="color: #66cc66;">&#41;</span> <span style="color: #993333; font-weight: bold;">NOT</span> <span style="color: #993333; font-weight: bold;">NULL</span><span style="color: #66cc66;">,</span>
  <span style="color: #ff0000;">`username`</span> <span style="color: #993333; font-weight: bold;">VARCHAR</span><span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">50</span><span style="color: #66cc66;">&#41;</span> <span style="color: #993333; font-weight: bold;">DEFAULT</span> <span style="color: #993333; font-weight: bold;">NULL</span><span style="color: #66cc66;">,</span>
  <span style="color: #ff0000;">`meta`</span> text<span style="color: #66cc66;">,</span>
  <span style="color: #993333; font-weight: bold;">PRIMARY</span> <span style="color: #993333; font-weight: bold;">KEY</span> <span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">`id`</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">,</span>
  <span style="color: #993333; font-weight: bold;">KEY</span> <span style="color: #ff0000;">`customer_id`</span> <span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">`customer_id`</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> <span style="color: #ff0000;">`users`</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: #66cc66;">,</span><span style="color: #cc66cc;">1</span><span style="color: #66cc66;">,</span><span style="color: #ff0000;">'someuser'</span><span style="color: #66cc66;">,</span><span style="color: #ff0000;">'N;'</span><span style="color: #66cc66;">&#41;</span>;
&nbsp;
<span style="color: #993333; font-weight: bold;">CREATE</span> <span style="color: #993333; font-weight: bold;">TABLE</span> <span style="color: #ff0000;">`customers`</span> <span style="color: #66cc66;">&#40;</span>
  <span style="color: #ff0000;">`id`</span> <span style="color: #993333; font-weight: bold;">BIGINT</span><span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">20</span><span style="color: #66cc66;">&#41;</span> <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;">AUTO_INCREMENT</span><span style="color: #66cc66;">,</span>
  <span style="color: #ff0000;">`name`</span> <span style="color: #993333; font-weight: bold;">VARCHAR</span><span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">100</span><span style="color: #66cc66;">&#41;</span> <span style="color: #993333; font-weight: bold;">DEFAULT</span> <span style="color: #993333; font-weight: bold;">NULL</span><span style="color: #66cc66;">,</span>
  <span style="color: #993333; font-weight: bold;">PRIMARY</span> <span style="color: #993333; font-weight: bold;">KEY</span> <span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">`id`</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> <span style="color: #ff0000;">`customers`</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: #66cc66;">,</span><span style="color: #ff0000;">'CustomerName 1'</span><span style="color: #66cc66;">&#41;</span>;
&nbsp;
<span style="color: #993333; font-weight: bold;">CREATE</span> <span style="color: #993333; font-weight: bold;">TABLE</span> <span style="color: #ff0000;">`blogposts`</span> <span style="color: #66cc66;">&#40;</span>
  <span style="color: #ff0000;">`id`</span> <span style="color: #993333; font-weight: bold;">BIGINT</span><span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">20</span><span style="color: #66cc66;">&#41;</span> <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;">AUTO_INCREMENT</span><span style="color: #66cc66;">,</span>
  <span style="color: #ff0000;">`user_id`</span> <span style="color: #993333; font-weight: bold;">BIGINT</span><span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">20</span><span style="color: #66cc66;">&#41;</span> <span style="color: #993333; font-weight: bold;">DEFAULT</span> <span style="color: #993333; font-weight: bold;">NULL</span><span style="color: #66cc66;">,</span>
  <span style="color: #ff0000;">`title`</span> <span style="color: #993333; font-weight: bold;">VARCHAR</span><span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">255</span><span style="color: #66cc66;">&#41;</span> <span style="color: #993333; font-weight: bold;">DEFAULT</span> <span style="color: #993333; font-weight: bold;">NULL</span><span style="color: #66cc66;">,</span>
  <span style="color: #ff0000;">`body`</span> text<span style="color: #66cc66;">,</span>
  <span style="color: #ff0000;">`created_at`</span> datetime <span style="color: #993333; font-weight: bold;">DEFAULT</span> <span style="color: #993333; font-weight: bold;">NULL</span><span style="color: #66cc66;">,</span>
  <span style="color: #ff0000;">`updated_at`</span> <span style="color: #993333; font-weight: bold;">TIMESTAMP</span> <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;">DEFAULT</span> <span style="color: #993333; font-weight: bold;">CURRENT_TIMESTAMP</span> <span style="color: #993333; font-weight: bold;">ON</span> <span style="color: #993333; font-weight: bold;">UPDATE</span> <span style="color: #993333; font-weight: bold;">CURRENT_TIMESTAMP</span><span style="color: #66cc66;">,</span>
  <span style="color: #993333; font-weight: bold;">PRIMARY</span> <span style="color: #993333; font-weight: bold;">KEY</span> <span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">`id`</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">,</span>
  <span style="color: #993333; font-weight: bold;">KEY</span> <span style="color: #ff0000;">`user_id`</span> <span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">`user_id`</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> <span style="color: #ff0000;">`blogposts`</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: #66cc66;">,</span><span style="color: #cc66cc;">1</span><span style="color: #66cc66;">,</span><span style="color: #ff0000;">'Blogpost 1'</span><span style="color: #66cc66;">,</span><span style="color: #ff0000;">'This is my first blog'</span><span style="color: #66cc66;">,</span><span style="color: #993333; font-weight: bold;">NULL</span><span style="color: #66cc66;">,</span><span style="color: #ff0000;">'2010-08-20 12:54:02'</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">,</span><span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">2</span><span style="color: #66cc66;">,</span><span style="color: #cc66cc;">1</span><span style="color: #66cc66;">,</span><span style="color: #ff0000;">'Blogpost 2'</span><span style="color: #66cc66;">,</span><span style="color: #ff0000;">'This is my second blog'</span><span style="color: #66cc66;">,</span><span style="color: #993333; font-weight: bold;">NULL</span><span style="color: #66cc66;">,</span><span style="color: #ff0000;">'2010-08-20 12:54:09'</span><span style="color: #66cc66;">&#41;</span>;</pre></div></div>

<p>And then some code to test the functionality:</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">&lt;?</span>
&nbsp;
<span style="color: #b1b100;">require_once</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'simpleactiverecord.php'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">class</span> User <span style="color: #000000; font-weight: bold;">extends</span> SimpleActiveRecord <span style="color: #009900;">&#123;</span>
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000088;">$has_many</span> <span style="color: #339933;">=</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span> <span style="color: #0000ff;">'blogposts'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'Blogpost'</span> <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000088;">$belongs_to</span> <span style="color: #339933;">=</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span> <span style="color: #0000ff;">'customer'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'Customer'</span> <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000088;">$serialize</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">'meta'</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">class</span> Customer <span style="color: #000000; font-weight: bold;">extends</span> SimpleActiveRecord <span style="color: #009900;">&#123;</span>
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000088;">$has_many</span> <span style="color: #339933;">=</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span> <span style="color: #0000ff;">'users'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'User'</span> <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">class</span> Blogpost <span style="color: #000000; font-weight: bold;">extends</span> SimpleActiveRecord <span style="color: #009900;">&#123;</span>
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000088;">$belongs_to</span> <span style="color: #339933;">=</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span> <span style="color: #0000ff;">'user'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'User'</span> <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
SimpleDbAdapterWrapper<span style="color: #339933;">::</span><span style="color: #004000;">setAdapter</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'mysqlAdapter'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
SimpleDbAdapterWrapper<span style="color: #339933;">::</span><span style="color: #004000;">connect</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'127.0.0.1'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'ormtest'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'passord'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'ormtestdb'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000088;">$user</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> User<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'username'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'someuser'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #666666; font-style: italic;">/* or $user-&gt;findFirstBy('field', 'value'); */</span>
&nbsp;
<span style="color: #b1b100;">print</span> <span style="color: #0000ff;">&quot;Username: &quot;</span> <span style="color: #339933;">.</span> <span style="color: #000088;">$user</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">username</span> <span style="color: #339933;">.</span> <span style="color: #0000ff;">&quot;<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #339933;">;</span>
<span style="color: #b1b100;">print</span> <span style="color: #0000ff;">&quot;Customer: &quot;</span> <span style="color: #339933;">.</span> <span style="color: #000088;">$user</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">customer</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">name</span> <span style="color: #339933;">.</span> <span style="color: #0000ff;">&quot;<span style="color: #000099; font-weight: bold;">\n</span><span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #b1b100;">foreach</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$user</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">blogposts</span> <span style="color: #b1b100;">as</span> <span style="color: #000088;">$blogpost</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
	<span style="color: #666666; font-style: italic;">// Dumps the whole blogpost object</span>
	<span style="color: #b1b100;">print</span> <span style="color: #000088;">$blogpost</span><span style="color: #339933;">;</span>
&nbsp;
	<span style="color: #666666; font-style: italic;">// Or fetch data from it manually:</span>
	<span style="color: #b1b100;">print</span> <span style="color: #0000ff;">&quot;Blog title: &quot;</span> <span style="color: #339933;">.</span> <span style="color: #000088;">$blogpost</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">title</span> <span style="color: #339933;">.</span> <span style="color: #0000ff;">&quot;<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #339933;">;</span>
	<span style="color: #b1b100;">print</span> <span style="color: #0000ff;">&quot;Blog written by: &quot;</span> <span style="color: #339933;">.</span> <span style="color: #000088;">$blogpost</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">user</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">username</span> <span style="color: #339933;">.</span> <span style="color: #0000ff;">&quot;<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #339933;">;</span>
	<span style="color: #b1b100;">print</span> <span style="color: #0000ff;">&quot;---<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">?&gt;</span></pre></div></div>

<p>This gave me the follwoing output:</p>
<p>
<pre>Username: someuser
Customer: CustomerName 1

Blogpost(1)
	Id: 1
	User_id: 1
	Title: Blogpost 1
	Body: This is my first blog
	Created_at:
	Updated_at: 2010-08-20 14:54:02
	User: (reference to a User object)
Blog title: Blogpost 1
Blog written by: someuser
---
Blogpost(2)
	Id: 2
	User_id: 1
	Title: Blogpost 2
	Body: This is my second blog
	Created_at:
	Updated_at: 2010-08-20 14:54:09
	User: (reference to a User object)
Blog title: Blogpost 2
Blog written by: someuser
---</pre>
</p>
<p><strong>Saving / Updating</strong></p>
<p>If I want to change something in for user with id 1 I can simply write:</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000088;">$user</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> User<span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">1</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$user</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">username</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">'newusername'</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$user</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">save</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>This will update sql with the new values of your user object.</p>
<p><strong>Relations</strong></p>
<p>As you can see in the example; we have three databases with relations to eachother. To relate the models to eachother, all you have to do is to define the relationship in the &#8216;magic&#8217; variables called $belongs_to and $has_many. In the example, I have related the Customer model to the User model, by saying &#8220;$has_many = array(&#8216;users&#8217; => &#8216;User&#8217;);&#8221; which means that the User table has a column called customer_id, which points to the &#8220;Customer&#8221; model. This means that if you have a Customer object, you can access an array of all the users to this customer, by using the $customerobject->users property. This will automatically &#8220;lazy load&#8221; all the users for your selected customer.</p>
<p><strong>Custom table names or field names</strong></p>
<p>If your users table haven&#8217;t defined the customer id field as &#8220;customer_id&#8221; which was automatically guessed, you can define it by saying: &#8220;var $has_many = array(&#8216;users:custom_customer_id&#8217; => &#8216;User&#8217;);&#8221;. Now instead of searching for customer_id in the Users table, it will search for the correct customer id in the custom_customer_id field of the users table. The same goes to the $belongs_to relations.</p>
<p>Also, if you do not define the primary key of your tables as &#8216;id&#8217;. Maybe you have your table&#8217;s primary key named &#8216;user_id&#8217;, you can define this in your model definition. The same goes if you define for example a User class, but the table is names &#8216;my_users&#8217;; you can explicitly define both like this:</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">class</span> User <span style="color: #000000; font-weight: bold;">extends</span> SimpleActiveRecord <span style="color: #009900;">&#123;</span>
  <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000088;">$tableName</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">'my_users'</span><span style="color: #339933;">;</span>
  <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000088;">$primaryKey</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">'user_id'</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p><strong>Serialization</strong></p>
<p>A nifty feature of this library is also automatic serialization. As you can see in the definition of the User class in the first example, i have told the library that the field &#8216;meta&#8217; is a serialized field. This means that it wil automatically be serialized with php&#8217;s &#8220;serialize()&#8221; function before it is saved to SQL. This enables you to add php variables and objects to your object for later use when you load the object from sql again. Let me give an example:</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000088;">$user</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> User<span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">1</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$user</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">meta</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'is_logged_in'</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #cc66cc;">1</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$user</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">meta</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'something_useful'</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'this'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'is'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'useful'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'information'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'about'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'someuser'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$user</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">save</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>This will then save the serialized content of meta to sql. The following SQL query happened behind the curtains:</p>

<div class="wp_syntax"><div class="code"><pre class="sql" style="font-family:monospace;"><span style="color: #993333; font-weight: bold;">UPDATE</span> <span style="color: #ff0000;">`users`</span> <span style="color: #993333; font-weight: bold;">SET</span> <span style="color: #ff0000;">`customer_id`</span> <span style="color: #66cc66;">=</span> <span style="color: #cc66cc;">1</span><span style="color: #66cc66;">,</span> <span style="color: #ff0000;">`username`</span> <span style="color: #66cc66;">=</span> <span style="color: #ff0000;">'someuser'</span><span style="color: #66cc66;">,</span> <span style="color: #ff0000;">`meta`</span> <span style="color: #66cc66;">=</span> <span style="color: #ff0000;">'a:2:{s:12:<span style="color: #000099; font-weight: bold;">\&quot;</span>is_logged_in<span style="color: #000099; font-weight: bold;">\&quot;</span>;i:1;s:16:<span style="color: #000099; font-weight: bold;">\&quot;</span>something_useful<span style="color: #000099; font-weight: bold;">\&quot;</span>;a:6:{i:0;s:4:<span style="color: #000099; font-weight: bold;">\&quot;</span>this<span style="color: #000099; font-weight: bold;">\&quot;</span>;i:1;s:2:<span style="color: #000099; font-weight: bold;">\&quot;</span>is<span style="color: #000099; font-weight: bold;">\&quot;</span>;i:2;s:6:<span style="color: #000099; font-weight: bold;">\&quot;</span>useful<span style="color: #000099; font-weight: bold;">\&quot;</span>;i:3;s:11:<span style="color: #000099; font-weight: bold;">\&quot;</span>information<span style="color: #000099; font-weight: bold;">\&quot;</span>;i:4;s:5:<span style="color: #000099; font-weight: bold;">\&quot;</span>about<span style="color: #000099; font-weight: bold;">\&quot;</span>;i:5;s:8:<span style="color: #000099; font-weight: bold;">\&quot;</span>someuser<span style="color: #000099; font-weight: bold;">\&quot;</span>;}}'</span> <span style="color: #993333; font-weight: bold;">WHERE</span> <span style="color: #ff0000;">`id`</span> <span style="color: #66cc66;">=</span> <span style="color: #cc66cc;">1</span> <span style="color: #993333; font-weight: bold;">LIMIT</span> <span style="color: #cc66cc;">1</span></pre></div></div>

<p>If we load user from sql again now and print the meta field:</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000088;">$user</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> User<span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">1</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #990000;">print_r</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$user</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">meta</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>We can see that it has correctly remembered the structure of our meta variable.</p>
<pre>Array
(
    [is_logged_in] => 1
    [something_useful] => Array
        (
            [0] => this
            [1] => is
            [2] => useful
            [3] => information
            [4] => about
            [5] => someuser
        )

)</pre>
<p>You can also have more than one serialized field by specifying it in the class definition:</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000088;">$serialize</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">'meta,metafield2'</span><span style="color: #339933;">;</span></pre></div></div>

<p>or</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000088;">$serialize</span> <span style="color: #339933;">=</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'meta'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'metafield2'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p><strong>Setting / getting multiple keys at once</strong></p>
<p>To set a whole bunch of variables in the object at once, you can use setAttributes(). This function sets the corresponding fields to the values in the associated array given as argument:</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000088;">$user</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">setAttributes</span><span style="color: #009900;">&#40;</span><span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span> <span style="color: #0000ff;">'name'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'newname'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'customer_id'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #cc66cc;">2</span> <span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #666666; font-style: italic;">// or fetch all data from a model as an array:</span>
<span style="color: #990000;">print_r</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$user</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">getAttributes</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p><strong>Searches</strong></p>
<p>You can also search for specific fields, or specify a WHERE statement manually. For example. If you are searching for all customers with a specific customer name, you can do the following:<br />
$customer = new Customer();<br />
$customers = $customer->findBy(&#8216;name&#8217;, &#8216;CustomerName&#8217;);<br />
print_r($customers);</p>
<p>This would give you an array of Customer objects. You probably notice that I have defined $customer as an empty instance of the Customer object, before using it to find the customers and wonered why.. The reason for this is that PHP &lt; 5.3.0 doesn&#8217;t support <a href="http://php.net/manual/en/language.oop5.late-static-bindings.php">late static bindings</a>. And I would like to support &lt; 5.3.0 since a lot of distributions haven&#8217;t moved to 5.3.x yet.</p>
<p>There is also a function called findFirstBy(), which works in a similar matter, but gives you only 1 object.</p>
<p>The find() and findFirst() functions allow you to define the full WHERE clause yourself. But remember to escape your data when you use these two functions. These are the only two functions that does not automatically escape your input.</p>
<p><strong>Model code</strong></p>
<p>Inside your model (the class definition of your table), you should add functions to handle tasks for your model that is more advanced than setting a variable and saving. For example in a User model, you might want to encrypt the users password just before the object is saved to sql.</p>
<p>Things like this can be done by defining beforeSave(), afterLoad() and beforeDestroy() functions in your model. They do as you think they do. And if your beforeSave() or beforeDestroy() functions return a false boolean, the save- or delete-operation will be aborted!</p>
<p><strong>Exceptions</strong></p>
<p>The class only has two custom exceptions, SqlErrorException and InvalidDbAdapterException. I think the names describes themselves pretty good.<br />
Remember to catch them. InvalidDbAdapterException usually only arrives if your dbAdapter is uncomplete. You should not be able to get this error with for the included mysqlAdapter.</p>
<p><strong>Thats all folks!</strong></p>
<p>Until I add phpDoc documentation, there&#8217;s only one way to learn about all the features included. There&#8217;s a lot of functions that is not mentioned here. But you can easily find about them by reading the source code. It shouldn&#8217;t be all too cryptic to understand.</p>
]]></content:encoded>
			<wfw:commentRss>http://lunatic.no/2010/08/simple-orm-php-library-for-mysql/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>Programming in Akelos</title>
		<link>http://lunatic.no/2010/01/programming-in-akelos/</link>
		<comments>http://lunatic.no/2010/01/programming-in-akelos/#comments</comments>
		<pubDate>Sun, 24 Jan 2010 16:31:41 +0000</pubDate>
		<dc:creator>Håkon Nessjøen</dc:creator>
				<category><![CDATA[hobby]]></category>
		<category><![CDATA[php]]></category>

		<guid isPermaLink="false">http://lunatic.no/?p=81</guid>
		<description><![CDATA[<div style="display:inline;float:right;margin-left:1em"><g:plusone href="http://lunatic.no/2010/01/programming-in-akelos/"></g:plusone></div>
For some while I have been using the Akelos library for MVC development within PHP. The reason I like this library over other well-known libraries like Zend Framework, CakePHP etc, is that this library is very much coded with &#8220;Convension over configuration&#8221; which means that you don&#8217;t have to over-configure everything. And it&#8217;s easy to [...]]]></description>
			<content:encoded><![CDATA[<div style="display:inline;float:right;margin-left:1em"><g:plusone href="http://lunatic.no/2010/01/programming-in-akelos/"></g:plusone></div>
<p>For some while I have been using the <a href="http://akelos.org/" target="_blank">Akelos</a> library for <a href="http://en.wikipedia.org/wiki/Model%E2%80%93view%E2%80%93controller" target="_blank">MVC</a> development within PHP. The reason I like this library over other well-known libraries like <a href="http://framework.zend.com/" target="_blank">Zend Framework</a>, <a href="http://cakephp.org/" target="_blank">CakePHP</a> etc, is that this library is very much coded with &#8220;Convension over configuration&#8221; which means that you don&#8217;t have to over-configure everything. And it&#8217;s easy to learn new people how to use it.<span id="more-81"></span></p>
<p>For example, when you create a database, you fill inn the fields you like, in a &#8216;installer&#8217;. Each time you need to add fields, you add new up() revisions in the installer, so versioning systems like SVN or git can see that &#8220;in revision x, a new fields was added to the sql table&#8221;. This means that the versioning system can keep track of the database as well as the files.</p>
<p>Something I also love about Akelos is the ease of translation. In the Views you can write so called &#8220;sintags&#8221;. Which is basically the template language in Akelos. To write out the contents of a variable, you write {variable name} if you write text that needs translating, you write _{translate this text}. Thats variables and simple translations. Then you have functions, like a form:</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;"><span style="color:#006600; font-weight:bold;">&lt;%</span>= form_tag <span style="color:#ff3333; font-weight:bold;">:id</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#996600;">'myid'</span> <span style="color:#006600; font-weight:bold;">%&gt;</span>
<span style="color:#006600; font-weight:bold;">&lt;%</span>= text_field <span style="color:#996600;">'blog'</span>, <span style="color:#996600;">'name'</span>, :<span style="color:#9966CC; font-weight:bold;">class</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#996600;">'myclass'</span> <span style="color:#006600; font-weight:bold;">%&gt;</span>
&lt;/form&gt;</pre></div></div>

<p>Now I wanted to be able to translate inside function tags, so I made a little <a href="http://trac.akelos.org/ticket/229#comment:2" target="_blank">adjustment</a> to the Akelos source which made it possible to do:</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;"> <span style="color:#006600; font-weight:bold;">&lt;%</span>= link_to _<span style="color:#996600;">'translated text'</span>, <span style="color:#ff3333; font-weight:bold;">:action</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#996600;">'about'</span> <span style="color:#006600; font-weight:bold;">%&gt;</span>
    or
 <span style="color:#006600; font-weight:bold;">&lt;%</span>= something <span style="color:#996600;">'name'</span>, <span style="color:#ff3333; font-weight:bold;">:vals</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#006600; font-weight:bold;">&#123;</span> <span style="color:#996600;">'plain text'</span>, _<span style="color:#996600;">'translated text'</span>, _<span style="color:#996600;">'translated text with %variable'</span> <span style="color:#006600; font-weight:bold;">&#125;</span> <span style="color:#006600; font-weight:bold;">%&gt;</span></pre></div></div>

<p>Here you also see that translated text also can contain variables from the view scope, which is pretty nifty in regards to dynamic translating. This &#8220;adjustment&#8221; was added to trunk in October 2009.</p>
]]></content:encoded>
			<wfw:commentRss>http://lunatic.no/2010/01/programming-in-akelos/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

