


<?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>Learn DooPHP &#187; DooModel</title>
	<atom:link href="http://learn.doophp.com/tag/doomodel/feed/" rel="self" type="application/rss+xml" />
	<link>http://learn.doophp.com</link>
	<description>Learn DooPHP - a high performance MVC based PHP framework</description>
	<lastBuildDate>Thu, 18 Aug 2011 19:47:45 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.8.4</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Using DooModel for Database Operations</title>
		<link>http://learn.doophp.com/2009/09/using-doomodel-for-database-operations/</link>
		<comments>http://learn.doophp.com/2009/09/using-doomodel-for-database-operations/#comments</comments>
		<pubDate>Wed, 23 Sep 2009 18:30:31 +0000</pubDate>
		<dc:creator>Leng</dc:creator>
				<category><![CDATA[Tutorials]]></category>
		<category><![CDATA[database]]></category>
		<category><![CDATA[DooModel]]></category>
		<category><![CDATA[easy]]></category>
		<category><![CDATA[model]]></category>
		<category><![CDATA[ORM]]></category>

		<guid isPermaLink="false">http://learn.doophp.com/?p=86</guid>
		<description><![CDATA[<p>If you have ever read DooPHP <a href="http://doophp.com/doc/guide/basic/model">guide</a> on model, you will see that a basic Model class does not have to extend any superclass.</p>
<p>A basic Model class looks like this:</p>
<pre class="brush: php;">
class User{
    public $id;
    public $uname;
    public $pwd;
    public $group;
    public $vip;

    public $_table = 'user';
    public $_primarykey = 'id';
    public $_fields = array('id', 'uname', 'pwd', 'group', 'vip');
}</pre>
<p>With the basic Model class, you can search for a database record by:</p>
<pre class="brush: php;">
//$this-&#62;db()-&#62;find('User');  is the same
Doo::db()-&#62;find('User');

//search for one record
Doo::db()-&#62;find('User', array('limit'=&#62;1) );

//search for a user named 'david'
Doo::db()-&#62;find('User', array('where'=&#62;&#34;uname='david'&#34;, 'limit'=&#62;1) );

//using prepared statement to avoid sql injection
Doo::db()-&#62;find('User', array(
                    'where' =&#62; 'user=?',
                    'param' =&#62; array($_GET['name']),
                    'limit' =&#62; 1
                )
           );

//Or simply use this for shorter code.
Doo::loadModel('User');
$u = new User;
$u-&#62;uname = $_GET['name'];
$result = Doo::db()-&#62;find($u, array('limit'=&#62;1));
</pre>
<p>Although the above code is pretty straightforward, we can make it even shorter and cleaner. First of all, we would need to have our Models to extend the DooModel class. We will have our Model class as the code below, notice the constructor:</p>
<pre class="brush: php; highlight: [14];">Doo::loadCore('db/DooModel');
class User extends DooModel {
    public $id;
    public $uname;
    public $pwd;
    public $group;
    public $vip;

    public $_table = 'user';
    public $_primarykey = 'id';
    public $_fields = array('id', 'uname', 'pwd', 'group', 'vip');

    function __construct(){
         parent::$className = __CLASS__;
    }
}
</pre>
<p>By</p>]]></description>
		<wfw:commentRss>http://learn.doophp.com/2009/09/using-doomodel-for-database-operations/feed/</wfw:commentRss>
		<slash:comments>17</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! -->
