<?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>Webscriptz.be &#187; php</title>
	<atom:link href="http://webscriptz.be/tags/php/feed" rel="self" type="application/rss+xml" />
	<link>http://webscriptz.be</link>
	<description></description>
	<lastBuildDate>Thu, 13 Oct 2011 18:16:08 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3</generator>
		<item>
		<title>Using Our Models To Do Basic Queries On The DB</title>
		<link>http://webscriptz.be/2011/06/23/using-our-models-to-do-basic-queries-on-the-db/409</link>
		<comments>http://webscriptz.be/2011/06/23/using-our-models-to-do-basic-queries-on-the-db/409#comments</comments>
		<pubDate>Thu, 23 Jun 2011 21:37:39 +0000</pubDate>
		<dc:creator>webscriptz</dc:creator>
				<category><![CDATA[tutorials]]></category>
		<category><![CDATA[AR]]></category>
		<category><![CDATA[data]]></category>
		<category><![CDATA[database]]></category>
		<category><![CDATA[MVC]]></category>
		<category><![CDATA[php]]></category>
		<category><![CDATA[queries]]></category>
		<category><![CDATA[query]]></category>
		<category><![CDATA[yii]]></category>
		<category><![CDATA[yii framework]]></category>
		<category><![CDATA[yiiframework]]></category>

		<guid isPermaLink="false">http://webscriptz.be/?p=409</guid>
		<description><![CDATA[The application has an auto-generated model, Gii kindly provides the model once the database is created. &#60;?phpclass Product extends CActiveRecord&#123;&#160;&#160;&#160;&#160;/**&#160;&#160;&#160;&#160;&#160;* The followings are the available columns in table 'Product':&#160;&#160;&#160;&#160;&#160;* @var integer $id&#160;&#160;&#160;&#160;&#160;* @var integer $brandId&#160;&#160;&#160;&#160;&#160;* @var string $name&#160;&#160;&#160;&#160;&#160;* @var string $price&#160;&#160;&#160;&#160;&#160;*/&#160;&#160;&#160;&#160;&#160;/**&#160;&#160;&#160;&#160;&#160;* Returns the static model of the specified AR class.&#160;&#160;&#160;&#160;&#160;* @return CActiveRecord the static model [...]]]></description>
			<content:encoded><![CDATA[<p>The application has an auto-generated model, Gii kindly provides the model once the database is created.</p>
<p><!--DEVFMTCODE--><pre class="devcodeblock" title="PHP"><div class="devcodeoverflow"><ol><li><span style="color: #000000; font-weight: bold;">&lt;?php</span></li><li><span style="color: #000000; font-weight: bold;">class</span> Product <span style="color: #000000; font-weight: bold;">extends</span> CActiveRecord</li><li><span style="color: #009900;">&#123;</span></li><li>&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #009933; font-style: italic;">/**</span></li><li><span style="color: #009933; font-style: italic;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;* The followings are the available columns in table 'Product':</span></li><li><span style="color: #009933; font-style: italic;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;* @var integer $id</span></li><li><span style="color: #009933; font-style: italic;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;* @var integer $brandId</span></li><li><span style="color: #009933; font-style: italic;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;* @var string $name</span></li><li><span style="color: #009933; font-style: italic;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;* @var string $price</span></li><li><span style="color: #009933; font-style: italic;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;*/</span></li><li>&nbsp;</li><li>&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #009933; font-style: italic;">/**</span></li><li><span style="color: #009933; font-style: italic;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;* Returns the static model of the specified AR class.</span></li><li><span style="color: #009933; font-style: italic;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;* @return CActiveRecord the static model class</span></li><li><span style="color: #009933; font-style: italic;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;*/</span></li><li>&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #000000; font-weight: bold;">public</span> static <span style="color: #000000; font-weight: bold;">function</span> model<span style="color: #009900;">&#40;</span><span style="color: #000088;">$className</span><span style="color: #339933;">=</span><span style="color: #009900; font-weight: bold;">__CLASS__</span><span style="color: #009900;">&#41;</span></li><li>&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #009900;">&#123;</span></li><li>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #b1b100;">return</span> parent<span style="color: #339933;">::</span><span style="color: #004000;">model</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$className</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></li><li>&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #009900;">&#125;</span></li><li>&nbsp;</li><li>&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #009933; font-style: italic;">/**</span></li><li><span style="color: #009933; font-style: italic;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;* @return string the associated database table name</span></li><li><span style="color: #009933; font-style: italic;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;*/</span></li><li>&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> tableName<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span></li><li>&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #009900;">&#123;</span></li><li>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #b1b100;">return</span> <span style="color: #0000ff;">'Product'</span><span style="color: #339933;">;</span></li><li>&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #009900;">&#125;</span></li><li>&nbsp;</li><li>&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #009933; font-style: italic;">/**</span></li><li><span style="color: #009933; font-style: italic;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;* @return array validation rules for model attributes.</span></li><li><span style="color: #009933; font-style: italic;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;*/</span></li><li>&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> rules<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span></li><li>&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #009900;">&#123;</span></li><li>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #b1b100;">return</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span></li><li>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'name'</span><span style="color: #339933;">,</span><span style="color: #0000ff;">'length'</span><span style="color: #339933;">,</span><span style="color: #0000ff;">'max'</span><span style="color: #339933;">=&gt;</span><span style="color: #cc66cc;">255</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span></li><li>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'price'</span><span style="color: #339933;">,</span><span style="color: #0000ff;">'length'</span><span style="color: #339933;">,</span><span style="color: #0000ff;">'max'</span><span style="color: #339933;">=&gt;</span><span style="color: #cc66cc;">8</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span></li><li>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'name, price'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'required'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span></li><li>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></li><li>&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #009900;">&#125;</span></li><li>&nbsp;</li><li>&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #009933; font-style: italic;">/**</span></li><li><span style="color: #009933; font-style: italic;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;* @return array relational rules.</span></li><li><span style="color: #009933; font-style: italic;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;*/</span></li><li>&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> relations<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span></li><li>&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #009900;">&#123;</span></li><li>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #666666; font-style: italic;">// NOTE: you may need to adjust the relation name and the related</span></li><li>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #666666; font-style: italic;">// class name for the relations automatically generated below.</span></li><li>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #b1b100;">return</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span></li><li>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #0000ff;">'brand'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #000000; font-weight: bold;">self</span><span style="color: #339933;">::</span><span style="color: #004000;">BELONGS_TO</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'Brand'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'brandId'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span></li><li>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #0000ff;">'categories'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #000000; font-weight: bold;">self</span><span style="color: #339933;">::</span><span style="color: #004000;">MANY_MANY</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'Category'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'ProductCategory(productId, categoryId)'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span></li><li>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></li><li>&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #009900;">&#125;</span></li><li>&nbsp;</li><li>&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #009933; font-style: italic;">/**</span></li><li><span style="color: #009933; font-style: italic;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;* @return array customized attribute labels (name=&gt;label)</span></li><li><span style="color: #009933; font-style: italic;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;*/</span></li><li>&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> attributeLabels<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span></li><li>&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #009900;">&#123;</span></li><li>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #b1b100;">return</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span></li><li>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #0000ff;">'id'</span><span style="color: #339933;">=&gt;</span><span style="color: #0000ff;">'Id'</span><span style="color: #339933;">,</span></li><li>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #0000ff;">'brandId'</span><span style="color: #339933;">=&gt;</span><span style="color: #0000ff;">'Brand'</span><span style="color: #339933;">,</span></li><li>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #0000ff;">'name'</span><span style="color: #339933;">=&gt;</span><span style="color: #0000ff;">'Name'</span><span style="color: #339933;">,</span></li><li>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #0000ff;">'price'</span><span style="color: #339933;">=&gt;</span><span style="color: #0000ff;">'Price'</span><span style="color: #339933;">,</span></li><li>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></li><li>&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #009900;">&#125;</span></li><li><span style="color: #009900;">&#125;</span></li></ol></div></pre><!--END_DEVFMTCODE--></p>
<p>Enable your weblogger: this you will find in: /yourProject/protected/config/main.php </p>
<p>By default your log array looks something like this: (Tip: IF you haven&#8217;t done this, and you find it hard to find you way in the file, comment everything you deem meaningful, use spaces and limit blocks of codes with comment of your choice)</p>
<p><!--DEVFMTCODE--><pre class="devcodeblock" title="PHP"><div class="devcodeoverflow"><ol><li>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #0000ff;">'log'</span><span style="color: #339933;">=&gt;</span><span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span></li><li>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #0000ff;">'class'</span><span style="color: #339933;">=&gt;</span><span style="color: #0000ff;">'CLogRouter'</span><span style="color: #339933;">,</span></li><li>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #0000ff;">'routes'</span><span style="color: #339933;">=&gt;</span><span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span></li><li>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span></li><li>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #0000ff;">'class'</span><span style="color: #339933;">=&gt;</span><span style="color: #0000ff;">'CFileLogRoute'</span><span style="color: #339933;">,</span></li><li>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #0000ff;">'levels'</span><span style="color: #339933;">=&gt;</span><span style="color: #0000ff;">'error, warning'</span><span style="color: #339933;">,</span></li><li>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span></li><li>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span></li><li>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span></li></ol></div></pre><!--END_DEVFMTCODE--></p>
<p>Let&#8217;s add in the CWebLogRoute so it looks like this.</p>
<p>      <!--DEVFMTCODE--><pre class="devcodeblock" title="PHP"><div class="devcodeoverflow"><ol><li>&nbsp;&nbsp;<span style="color: #0000ff;">'log'</span><span style="color: #339933;">=&gt;</span><span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span></li><li>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #0000ff;">'class'</span><span style="color: #339933;">=&gt;</span><span style="color: #0000ff;">'CLogRouter'</span><span style="color: #339933;">,</span></li><li>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #0000ff;">'routes'</span><span style="color: #339933;">=&gt;</span><span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span></li><li>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span></li><li>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #0000ff;">'class'</span><span style="color: #339933;">=&gt;</span><span style="color: #0000ff;">'CFileLogRoute'</span><span style="color: #339933;">,</span></li><li>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #0000ff;">'levels'</span><span style="color: #339933;">=&gt;</span><span style="color: #0000ff;">'error, warning'</span><span style="color: #339933;">,</span></li><li>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span></li><li>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span></li><li>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #0000ff;">'class'</span><span style="color: #339933;">=&gt;</span><span style="color: #0000ff;">'CWebLogRoute'</span><span style="color: #339933;">,</span></li><li>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #0000ff;">'levels'</span><span style="color: #339933;">=&gt;</span><span style="color: #0000ff;">'trace,info, error, warning'</span><span style="color: #339933;">,</span></li><li>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span></li><li>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span></li><li>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span></li></ol></div></pre><!--END_DEVFMTCODE--></p>
<p>Visit the application and notice that at the bottom of the screen a table is shown displaying everything Yii does to make it possible for the website to be shown correctly. This is especially useful when you&#8217;re new to the Yii DB stuff.</p>
<p>Let&#8217;s start by getting all of our products. If you are trying this tutorial, then please create a simple new installation, config the database settings in /protected/config/main.php and create your database with the proposed columns from the model seen earlier.</p>
<p>You can test this in any controller, because the system already has a default SiteController in: /protected/controllers/siteController.php, this tutorial will use siteController.php with the view-file in:  /protected/views/site/index.php</p>
<p>So we&#8217;ll first modify our SiteController::actionIndex method.</p>
<p><!--DEVFMTCODE--><pre class="devcodeblock" title="PHP"><div class="devcodeoverflow"><ol><li>&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> actionIndex<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span></li><li>&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #009900;">&#123;</span></li><li>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">render</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'index'</span><span style="color: #339933;">,</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span></li><li>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #0000ff;">'Products'</span> <span style="color: #339933;">=&gt;</span> Product<span style="color: #339933;">::</span><span style="color: #004000;">model</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">findAll</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span></li><li>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></li><li>&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #009900;">&#125;</span></li></ol></div></pre><!--END_DEVFMTCODE--></p>
<p>Open the view-file related to the actionIndex() The system has an array that will be passed to our index view. This array contains &#8216;Products&#8217; which will be usable as &#8216;$Products&#8217; in our view: /protected/views/site/index.php</p>
<p><!--DEVFMTCODE--><pre class="devcodeblock" title="PHP"><div class="devcodeoverflow"><ol><li><span style="color: #000000; font-weight: bold;">&lt;?php</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">pageTitle</span><span style="color: #339933;">=</span>Yii<span style="color: #339933;">::</span><span style="color: #004000;">app</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">name</span><span style="color: #339933;">;</span> <span style="color: #000000; font-weight: bold;">?&gt;</span></li><li>&nbsp;</li><li><span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">h1</span>&gt;</span></li><li>&nbsp;&nbsp;&nbsp;&nbsp;Welcome, <span style="color: #000000; font-weight: bold;">&lt;?php</span> <span style="color: #b1b100;">echo</span> Yii<span style="color: #339933;">::</span><span style="color: #004000;">app</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">user</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">name</span><span style="color: #339933;">;</span> <span style="color: #000000; font-weight: bold;">?&gt;</span>!</li><li><span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">h1</span>&gt;</span></li><li><span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">table</span>&gt;</span></li><li><span style="color: #000000; font-weight: bold;">&lt;?php</span> <span style="color: #b1b100;">foreach</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$Products</span> <span style="color: #b1b100;">AS</span> <span style="color: #000088;">$Product</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">:</span><span style="color: #000000; font-weight: bold;">?&gt;</span></li><li>&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">tr</span>&gt;</span></li><li>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">td</span>&gt;</span><span style="color: #000000; font-weight: bold;">&lt;?php</span> <span style="color: #b1b100;">echo</span> <span style="color: #000088;">$Product</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">name</span><span style="color: #339933;">;</span><span style="color: #000000; font-weight: bold;">?&gt;</span><span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">td</span>&gt;</span></li><li>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">td</span>&gt;</span><span style="color: #000000; font-weight: bold;">&lt;?php</span> <span style="color: #b1b100;">echo</span> <span style="color: #000088;">$Product</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">price</span><span style="color: #339933;">;</span><span style="color: #000000; font-weight: bold;">?&gt;</span><span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">td</span>&gt;</span></li><li>&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">tr</span>&gt;</span></li><li><span style="color: #000000; font-weight: bold;">&lt;?php</span> <span style="color: #b1b100;">endforeach</span><span style="color: #339933;">;</span><span style="color: #000000; font-weight: bold;">?&gt;</span></li><li><span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">table</span>&gt;</span></li></ol></div></pre><!--END_DEVFMTCODE--></p>
<p>The system (Yii) writes the SQL statements for us thanks to the AR and everything behind that, but we got back an array of product objects.</p>
<p>Now let&#8217;s use the &#8216;brand&#8217; relation that is set in our Product model.</p>
<p>Without changing the controller, we can simply change the view and add the line to output the brand name.</p>
<p><!--DEVFMTCODE--><pre class="devcodeblock" title="PHP"><div class="devcodeoverflow"><ol><li><span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">td</span>&gt;</span><span style="color: #000000; font-weight: bold;">&lt;?php</span> <span style="color: #b1b100;">echo</span> <span style="color: #000088;">$Product</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">brand</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">name</span><span style="color: #339933;">;</span><span style="color: #000000; font-weight: bold;">?&gt;</span><span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">td</span>&gt;</span></li></ol></div></pre><!--END_DEVFMTCODE--></p>
<p>Now how did Yii do that? Look at the screen logger. You will see that it used the &#8216;lazy-loading&#8217; technique to query the Brand table at that point.</p>
<p><!--DEVFMTCODE--><pre class="devcodeblock" title="SQL"><div class="devcodeoverflow"><ol><li>Querying <span style="color: #993333; font-weight: bold;">SQL</span>: <span style="color: #993333; font-weight: bold;">SELECT</span> <span style="color: #66cc66;">*</span> <span style="color: #993333; font-weight: bold;">FROM</span> <span style="color: #ff0000;">`Product`</span></li><li>&nbsp;</li><li>Querying <span style="color: #993333; font-weight: bold;">SQL</span>: <span style="color: #993333; font-weight: bold;">SELECT</span> <span style="color: #ff0000;">`Product`</span><span style="color: #66cc66;">.</span><span style="color: #ff0000;">`id`</span> <span style="color: #993333; font-weight: bold;">AS</span> <span style="color: #ff0000;">`t0_c0`</span><span style="color: #66cc66;">,</span> t1<span style="color: #66cc66;">.</span><span style="color: #ff0000;">`id`</span> <span style="color: #993333; font-weight: bold;">AS</span> <span style="color: #ff0000;">`t1_c0`</span><span style="color: #66cc66;">,</span></li><li>t1<span style="color: #66cc66;">.</span><span style="color: #ff0000;">`name`</span> <span style="color: #993333; font-weight: bold;">AS</span> <span style="color: #ff0000;">`t1_c1`</span><span style="color: #66cc66;">,</span> t1<span style="color: #66cc66;">.</span><span style="color: #ff0000;">`website`</span> <span style="color: #993333; font-weight: bold;">AS</span> <span style="color: #ff0000;">`t1_c2`</span> <span style="color: #993333; font-weight: bold;">FROM</span> <span style="color: #ff0000;">`Product`</span> <span style="color: #993333; font-weight: bold;">LEFT</span> <span style="color: #993333; font-weight: bold;">OUTER</span></li><li><span style="color: #993333; font-weight: bold;">JOIN</span> <span style="color: #ff0000;">`Brand`</span> t1 <span style="color: #993333; font-weight: bold;">ON</span> <span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">`Product`</span><span style="color: #66cc66;">.</span><span style="color: #ff0000;">`brandId`</span><span style="color: #66cc66;">=</span>t1<span style="color: #66cc66;">.</span><span style="color: #ff0000;">`id`</span><span style="color: #66cc66;">&#41;</span> <span style="color: #993333; font-weight: bold;">WHERE</span> <span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">`Product`</span><span style="color: #66cc66;">.</span><span style="color: #ff0000;">`id`</span><span style="color: #66cc66;">=</span><span style="color: #cc66cc;">3</span><span style="color: #66cc66;">&#41;</span></li><li>&nbsp;</li><li>Querying <span style="color: #993333; font-weight: bold;">SQL</span>: <span style="color: #993333; font-weight: bold;">SELECT</span> <span style="color: #ff0000;">`Product`</span><span style="color: #66cc66;">.</span><span style="color: #ff0000;">`id`</span> <span style="color: #993333; font-weight: bold;">AS</span> <span style="color: #ff0000;">`t0_c0`</span><span style="color: #66cc66;">,</span> t1<span style="color: #66cc66;">.</span><span style="color: #ff0000;">`id`</span> <span style="color: #993333; font-weight: bold;">AS</span> <span style="color: #ff0000;">`t1_c0`</span><span style="color: #66cc66;">,</span></li><li>t1<span style="color: #66cc66;">.</span><span style="color: #ff0000;">`name`</span> <span style="color: #993333; font-weight: bold;">AS</span> <span style="color: #ff0000;">`t1_c1`</span><span style="color: #66cc66;">,</span> t1<span style="color: #66cc66;">.</span><span style="color: #ff0000;">`website`</span> <span style="color: #993333; font-weight: bold;">AS</span> <span style="color: #ff0000;">`t1_c2`</span> <span style="color: #993333; font-weight: bold;">FROM</span> <span style="color: #ff0000;">`Product`</span> <span style="color: #993333; font-weight: bold;">LEFT</span> <span style="color: #993333; font-weight: bold;">OUTER</span></li><li><span style="color: #993333; font-weight: bold;">JOIN</span> <span style="color: #ff0000;">`Brand`</span> t1 <span style="color: #993333; font-weight: bold;">ON</span> <span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">`Product`</span><span style="color: #66cc66;">.</span><span style="color: #ff0000;">`brandId`</span><span style="color: #66cc66;">=</span>t1<span style="color: #66cc66;">.</span><span style="color: #ff0000;">`id`</span><span style="color: #66cc66;">&#41;</span> <span style="color: #993333; font-weight: bold;">WHERE</span> <span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">`Product`</span><span style="color: #66cc66;">.</span><span style="color: #ff0000;">`id`</span><span style="color: #66cc66;">=</span><span style="color: #cc66cc;">4</span><span style="color: #66cc66;">&#41;</span></li></ol></div></pre><!--END_DEVFMTCODE--></p>
<p>This is not the best way to go so let&#8217;s change the actionIndex in siteController.php</p>
<p><!--DEVFMTCODE--><pre class="devcodeblock" title="PHP"><div class="devcodeoverflow"><ol><li>&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> actionIndex<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span></li><li>&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #009900;">&#123;</span></li><li>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #666666; font-style: italic;">// renders the view file 'protected/views/site/index.php'</span></li><li>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #666666; font-style: italic;">// using the default layout 'protected/views/layouts/main.php'</span></li><li>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">render</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'index'</span><span style="color: #339933;">,</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span></li><li>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #0000ff;">'Products'</span> <span style="color: #339933;">=&gt;</span> Product<span style="color: #339933;">::</span><span style="color: #004000;">model</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">with</span><span style="color: #009900;">&#40;</span><span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'brand'</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">findAll</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span></li><li>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></li><li>&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #009900;">&#125;</span></li></ol></div></pre><!--END_DEVFMTCODE--></p>
<p>One query is all that is needed for all the data to be collected.</p>
<p><!--DEVFMTCODE--><pre class="devcodeblock" title="SQL"><div class="devcodeoverflow"><ol><li>Querying <span style="color: #993333; font-weight: bold;">SQL</span>: <span style="color: #993333; font-weight: bold;">SELECT</span> <span style="color: #ff0000;">`Product`</span><span style="color: #66cc66;">.</span><span style="color: #ff0000;">`id`</span> <span style="color: #993333; font-weight: bold;">AS</span> <span style="color: #ff0000;">`t0_c0`</span><span style="color: #66cc66;">,</span> <span style="color: #ff0000;">`Product`</span><span style="color: #66cc66;">.</span><span style="color: #ff0000;">`brandId`</span> <span style="color: #993333; font-weight: bold;">AS</span></li><li><span style="color: #ff0000;">`t0_c1`</span><span style="color: #66cc66;">,</span> <span style="color: #ff0000;">`Product`</span><span style="color: #66cc66;">.</span><span style="color: #ff0000;">`name`</span> <span style="color: #993333; font-weight: bold;">AS</span> <span style="color: #ff0000;">`t0_c2`</span><span style="color: #66cc66;">,</span> <span style="color: #ff0000;">`Product`</span><span style="color: #66cc66;">.</span><span style="color: #ff0000;">`price`</span> <span style="color: #993333; font-weight: bold;">AS</span> <span style="color: #ff0000;">`t0_c3`</span><span style="color: #66cc66;">,</span> t1<span style="color: #66cc66;">.</span><span style="color: #ff0000;">`id`</span></li><li><span style="color: #993333; font-weight: bold;">AS</span> <span style="color: #ff0000;">`t1_c0`</span><span style="color: #66cc66;">,</span> t1<span style="color: #66cc66;">.</span><span style="color: #ff0000;">`name`</span> <span style="color: #993333; font-weight: bold;">AS</span> <span style="color: #ff0000;">`t1_c1`</span><span style="color: #66cc66;">,</span> t1<span style="color: #66cc66;">.</span><span style="color: #ff0000;">`website`</span> <span style="color: #993333; font-weight: bold;">AS</span> <span style="color: #ff0000;">`t1_c2`</span> <span style="color: #993333; font-weight: bold;">FROM</span> <span style="color: #ff0000;">`Product`</span> </li><li><span style="color: #993333; font-weight: bold;">LEFT</span> <span style="color: #993333; font-weight: bold;">OUTER</span> <span style="color: #993333; font-weight: bold;">JOIN</span> <span style="color: #ff0000;">`Brand`</span> t1 <span style="color: #993333; font-weight: bold;">ON</span> <span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">`Product`</span><span style="color: #66cc66;">.</span><span style="color: #ff0000;">`brandId`</span><span style="color: #66cc66;">=</span>t1<span style="color: #66cc66;">.</span><span style="color: #ff0000;">`id`</span><span style="color: #66cc66;">&#41;</span></li></ol></div></pre><!--END_DEVFMTCODE--></p>
<p>The view file does not have to change for this. This technique is called eager-loading and has advantages over lazyloading sometimes.</p>
<blockquote><p>This tutorial is a modified tutorial from another website/blog that unfortunantly isn’t online anymore, domain is being sold by godaddy.com, so I search my archives to retrieve this on, and post him here, hence a few additions, deletes and rewrites from me.</p></blockquote>
<p class="facebook"><a href="http://www.facebook.com/share.php?u=http://webscriptz.be/2011/06/23/using-our-models-to-do-basic-queries-on-the-db/409" target="_blank"><img src="http://webscriptz.be/wp-content/plugins/add-to-facebook-plugin/facebook_share_icon.gif" alt="Share on Facebook" title="Share on Facebook" /></a><a href="http://www.facebook.com/share.php?u=http://webscriptz.be/2011/06/23/using-our-models-to-do-basic-queries-on-the-db/409" target="_blank" title="Share on Facebook">Share on Facebook</a></p>]]></content:encoded>
			<wfw:commentRss>http://webscriptz.be/2011/06/23/using-our-models-to-do-basic-queries-on-the-db/409/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>YiiFramework adding functions to your models</title>
		<link>http://webscriptz.be/2011/06/23/yiiframework-adding-functions-to-your-models/406</link>
		<comments>http://webscriptz.be/2011/06/23/yiiframework-adding-functions-to-your-models/406#comments</comments>
		<pubDate>Thu, 23 Jun 2011 21:16:04 +0000</pubDate>
		<dc:creator>webscriptz</dc:creator>
				<category><![CDATA[tutorials]]></category>
		<category><![CDATA[AR]]></category>
		<category><![CDATA[database]]></category>
		<category><![CDATA[framework]]></category>
		<category><![CDATA[learn]]></category>
		<category><![CDATA[MVC]]></category>
		<category><![CDATA[php]]></category>
		<category><![CDATA[queries]]></category>
		<category><![CDATA[query]]></category>
		<category><![CDATA[tutorial]]></category>
		<category><![CDATA[yii]]></category>
		<category><![CDATA[yii framework]]></category>
		<category><![CDATA[yiiframework]]></category>

		<guid isPermaLink="false">http://webscriptz.be/?p=406</guid>
		<description><![CDATA[MVC has some principals to it, like the fat model principal that I&#8217;ll show in this tutorial and which is used in a lot of frameworks. I&#8217;m going to show how to add and use functions that can be added to a model. There are a few ways to add to a models that increase [...]]]></description>
			<content:encoded><![CDATA[<p>MVC has some principals to it, like the fat model principal that I&#8217;ll show in this tutorial and which is used in a lot of frameworks. I&#8217;m going to show how to add and use functions that can be added to a model.</p>
<p>There are a few ways to add to a models that increase code re-usability. One way is to just add methods to the models, methods which can execute complex queries and make your controller thinner. </p>
<p>The first example, a query that will get all products within a price range, it will need a limit and an offset for pagination, so in the Products model, the following function can be added:</p>
<p><!--DEVFMTCODE--><pre class="devcodeblock" title="PHP"><div class="devcodeoverflow"><ol><li><span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> findAllByPrice<span style="color: #009900;">&#40;</span><span style="color: #000088;">$min</span><span style="color: #339933;">=</span><span style="color: #cc66cc;">0</span><span style="color: #339933;">,</span> <span style="color: #000088;">$max</span><span style="color: #339933;">=</span><span style="color: #cc66cc;">1000000</span><span style="color: #339933;">,</span> <span style="color: #000088;">$offset</span> <span style="color: #339933;">=</span> <span style="color: #cc66cc;">0</span><span style="color: #339933;">,</span> <span style="color: #000088;">$limit</span> <span style="color: #339933;">=</span> <span style="color: #cc66cc;">30</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span></li><li>&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #000088;">$Criteria</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> CDbCriteria<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></li><li>&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #000088;">$Criteria</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">condition</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;price &gt;= :min AND price &lt;=:max&quot;</span><span style="color: #339933;">;</span></li><li>&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #000088;">$Criteria</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">limit</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$limit</span><span style="color: #339933;">;</span></li><li>&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #000088;">$Criteria</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">offset</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$offset</span><span style="color: #339933;">;</span></li><li>&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #000088;">$Criteria</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">params</span> <span style="color: #339933;">=</span> <span style="color: #990000;">array</span> <span style="color: #009900;">&#40;</span> <span style="color: #0000ff;">':min'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #000088;">$min</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">':max'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #000088;">$max</span> <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></li><li>&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #b1b100;">return</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">with</span><span style="color: #009900;">&#40;</span><span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'brand'</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">findAll</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$Criteria</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></li><li><span style="color: #009900;">&#125;</span></li></ol></div></pre><!--END_DEVFMTCODE--></p>
<p>Now in any of the controlller files this method can be easily accessed and returns all of the &#8216;Product&#8217; objects with a single line command:</p>
<p><!--DEVFMTCODE--><pre class="devcodeblock" title="PHP"><div class="devcodeoverflow"><ol><li><span style="color: #000088;">$Products</span> <span style="color: #339933;">=</span> Product<span style="color: #339933;">::</span><span style="color: #004000;">model</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">findAllByPrice</span><span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">100</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">700</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">0</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">30</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></li></ol></div></pre><!--END_DEVFMTCODE--></p>
<p>Another way to do the same is to:</p>
<p><!--DEVFMTCODE--><pre class="devcodeblock" title="PHP"><div class="devcodeoverflow"><ol><li><span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> getProducts<span style="color: #009900;">&#40;</span><span style="color: #000088;">$category</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span></li><li>&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #b1b100;">return</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">findAll</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'categoryIdFk=:category'</span><span style="color: #339933;">,</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">':category'</span><span style="color: #339933;">=&gt;</span><span style="color: #000088;">$category</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></li><li><span style="color: #009900;">&#125;</span></li></ol></div></pre><!--END_DEVFMTCODE--></p>
<p>Let&#8217;s implement the first query using two named scope methods that I&#8217;ll create.</p>
<p><!--DEVFMTCODE--><pre class="devcodeblock" title="PHP"><div class="devcodeoverflow"><ol><li><span style="color: #666666; font-style: italic;">// Price Between - Named Scope ---------------------------------//</span></li><li><span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> scopePriceBetween<span style="color: #009900;">&#40;</span><span style="color: #000088;">$min</span><span style="color: #339933;">,</span> <span style="color: #000088;">$max</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span></li><li>&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #000088;">$Criteria</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> CDbCriteria<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></li><li>&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #000088;">$Criteria</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">condition</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;<span style="color: #006699; font-weight: bold;">$this-&gt;tableName</span>().price BETWEEN :min AND :max&quot;</span><span style="color: #339933;">;</span></li><li>&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #000088;">$Criteria</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">params</span> <span style="color: #339933;">=</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">':min'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #000088;">$min</span><span style="color: #339933;">,</span>&nbsp;&nbsp; <span style="color: #0000ff;">':max'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #000088;">$max</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></li><li>&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">getDbCriteria</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">mergeWith</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$Criteria</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></li><li>&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #b1b100;">return</span> <span style="color: #000088;">$this</span><span style="color: #339933;">;</span></li><li><span style="color: #009900;">&#125;</span></li></ol></div></pre><!--END_DEVFMTCODE--></p>
<p>Now an additional named scope for the limit element</p>
<p><!--DEVFMTCODE--><pre class="devcodeblock" title="PHP"><div class="devcodeoverflow"><ol><li><span style="color: #666666; font-style: italic;">//&nbsp;&nbsp;Limit - Named Scope --------------------------------------//</span></li><li><span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> scopeLimit<span style="color: #009900;">&#40;</span><span style="color: #000088;">$limit</span> <span style="color: #339933;">=</span> <span style="color: #cc66cc;">30</span><span style="color: #339933;">,</span> <span style="color: #000088;">$offset</span> <span style="color: #339933;">=</span> <span style="color: #cc66cc;">0</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span></li><li>&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #000088;">$Criteria</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> CDbCriteria<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></li><li>&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #000088;">$Criteria</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">limit</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$limit</span><span style="color: #339933;">;</span></li><li>&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #000088;">$Criteria</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">offset</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$offset</span><span style="color: #339933;">;</span></li><li>&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">getDbCriteria</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">mergeWith</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$Criteria</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></li><li>&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #b1b100;">return</span> <span style="color: #000088;">$this</span><span style="color: #339933;">;</span></li><li><span style="color: #009900;">&#125;</span></li></ol></div></pre><!--END_DEVFMTCODE--></p>
<p>Now we can simply use our named scopes to get our data back, and then we could continue to pile on named scopes to return our data.</p>
<p><!--DEVFMTCODE--><pre class="devcodeblock" title="PHP"><div class="devcodeoverflow"><ol><li><span style="color: #000088;">$Products</span> <span style="color: #339933;">=</span> Product<span style="color: #339933;">::</span><span style="color: #004000;">model</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">scopePriceBetween</span><span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">0</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">5000</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">-&gt;</span><span style="color: #004000;">scopeLimit</span><span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">30</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">1</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">findAll</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></li></ol></div></pre><!--END_DEVFMTCODE--></p>
<p>The query that actually gets executed is simple and looks like this:</p>
<p><!--DEVFMTCODE--><pre class="devcodeblock" title="SQL"><div class="devcodeoverflow"><ol><li><span style="color: #993333; font-weight: bold;">SELECT</span> <span style="color: #66cc66;">*</span> <span style="color: #993333; font-weight: bold;">FROM</span> <span style="color: #ff0000;">`Product`</span> <span style="color: #993333; font-weight: bold;">WHERE</span> Product<span style="color: #66cc66;">.</span>price <span style="color: #993333; font-weight: bold;">BETWEEN</span> :<span style="color: #993333; font-weight: bold;">MIN</span> <span style="color: #993333; font-weight: bold;">AND</span> :<span style="color: #993333; font-weight: bold;">MAX</span> <span style="color: #993333; font-weight: bold;">LIMIT</span> <span style="color: #cc66cc;">30</span> OFFSET <span style="color: #cc66cc;">1</span></li></ol></div></pre><!--END_DEVFMTCODE--></p>
<blockquote><p>This tutorial is a modified tutorial from another website/blog that unfortunantly isn&#8217;t online anymore, domain is being sold by godaddy.com, so I search my archives to retrieve this on, and post him here, hence a few additions, deletes and rewrites from me.</p></blockquote>
<p class="facebook"><a href="http://www.facebook.com/share.php?u=http://webscriptz.be/2011/06/23/yiiframework-adding-functions-to-your-models/406" target="_blank"><img src="http://webscriptz.be/wp-content/plugins/add-to-facebook-plugin/facebook_share_icon.gif" alt="Share on Facebook" title="Share on Facebook" /></a><a href="http://www.facebook.com/share.php?u=http://webscriptz.be/2011/06/23/yiiframework-adding-functions-to-your-models/406" target="_blank" title="Share on Facebook">Share on Facebook</a></p>]]></content:encoded>
			<wfw:commentRss>http://webscriptz.be/2011/06/23/yiiframework-adding-functions-to-your-models/406/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>SQL &amp; Yii</title>
		<link>http://webscriptz.be/2011/06/21/sql-yii/399</link>
		<comments>http://webscriptz.be/2011/06/21/sql-yii/399#comments</comments>
		<pubDate>Tue, 21 Jun 2011 18:35:59 +0000</pubDate>
		<dc:creator>webscriptz</dc:creator>
				<category><![CDATA[General]]></category>
		<category><![CDATA[me]]></category>
		<category><![CDATA[mysql]]></category>
		<category><![CDATA[php]]></category>
		<category><![CDATA[sql]]></category>
		<category><![CDATA[tutorials]]></category>
		<category><![CDATA[yii]]></category>
		<category><![CDATA[yii framework]]></category>
		<category><![CDATA[yiiframework]]></category>

		<guid isPermaLink="false">http://webscriptz.be/?p=399</guid>
		<description><![CDATA[At school, I have a course ORACLE SQL which learned me some new tricks and options, I&#8217;ve also been testing them in MySQL and so the idea grew to see how Yii framework would interact with those tricks. I must say I didn&#8217;t expect them to work but they do so probably within the next [...]]]></description>
			<content:encoded><![CDATA[<p>At school, I have a course ORACLE SQL which learned me some new tricks and options, I&#8217;ve also been testing them in MySQL and so the idea grew to see how Yii framework would interact with those tricks. I must say I didn&#8217;t expect them to work but they do so probably within the next month I&#8217;ll write some new tutorials and infos in SQL and YiiFramework.</p>
<p class="facebook"><a href="http://www.facebook.com/share.php?u=http://webscriptz.be/2011/06/21/sql-yii/399" target="_blank"><img src="http://webscriptz.be/wp-content/plugins/add-to-facebook-plugin/facebook_share_icon.gif" alt="Share on Facebook" title="Share on Facebook" /></a><a href="http://www.facebook.com/share.php?u=http://webscriptz.be/2011/06/21/sql-yii/399" target="_blank" title="Share on Facebook">Share on Facebook</a></p>]]></content:encoded>
			<wfw:commentRss>http://webscriptz.be/2011/06/21/sql-yii/399/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Yiiframework Captcha problem</title>
		<link>http://webscriptz.be/2011/02/23/yiiframework-captcha-problem/379</link>
		<comments>http://webscriptz.be/2011/02/23/yiiframework-captcha-problem/379#comments</comments>
		<pubDate>Wed, 23 Feb 2011 14:48:10 +0000</pubDate>
		<dc:creator>webscriptz</dc:creator>
				<category><![CDATA[post]]></category>
		<category><![CDATA[internet]]></category>
		<category><![CDATA[php]]></category>
		<category><![CDATA[problem]]></category>
		<category><![CDATA[project]]></category>
		<category><![CDATA[website]]></category>
		<category><![CDATA[yii]]></category>
		<category><![CDATA[yiiframework]]></category>

		<guid isPermaLink="false">http://webscriptz.be/2011/02/23/yiiframework-captcha-problem/379</guid>
		<description><![CDATA[While lanching a new website I had problems with the AJAX request allowing the captcha to refresh itself. The link &#8216;Get a new code&#8217; didn&#8217;t work properly and I didn&#8217;t really find an alternative at first. The only alternative I found it to add in the $this->widget(); add an array after &#8216;CCaptcha&#8217;: Share on Facebook]]></description>
			<content:encoded><![CDATA[<p>While lanching a new website I had problems with the AJAX request allowing the captcha to refresh itself. The link  &#8216;Get a new code&#8217; didn&#8217;t work properly and I didn&#8217;t really find an alternative at first.</p>
<p>The only alternative I found it to add in the $this->widget(); add an array after &#8216;CCaptcha&#8217;:</p>
<p><?php $this->widget(&#8216;CCaptcha&#8217;, array(&#8216;clickableImage&#8217;=>true, &#8216;showRefreshButton&#8217;=>false)); ?></p>
<p class="facebook"><a href="http://www.facebook.com/share.php?u=http://webscriptz.be/2011/02/23/yiiframework-captcha-problem/379" target="_blank"><img src="http://webscriptz.be/wp-content/plugins/add-to-facebook-plugin/facebook_share_icon.gif" alt="Share on Facebook" title="Share on Facebook" /></a><a href="http://www.facebook.com/share.php?u=http://webscriptz.be/2011/02/23/yiiframework-captcha-problem/379" target="_blank" title="Share on Facebook">Share on Facebook</a></p>]]></content:encoded>
			<wfw:commentRss>http://webscriptz.be/2011/02/23/yiiframework-captcha-problem/379/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Yii framework: RBAC</title>
		<link>http://webscriptz.be/2011/02/16/yii-framework-rbac/369</link>
		<comments>http://webscriptz.be/2011/02/16/yii-framework-rbac/369#comments</comments>
		<pubDate>Wed, 16 Feb 2011 18:44:43 +0000</pubDate>
		<dc:creator>webscriptz</dc:creator>
				<category><![CDATA[General]]></category>
		<category><![CDATA[access-control]]></category>
		<category><![CDATA[php]]></category>
		<category><![CDATA[programming]]></category>
		<category><![CDATA[RBAC]]></category>
		<category><![CDATA[role-based-access]]></category>
		<category><![CDATA[web]]></category>
		<category><![CDATA[yii]]></category>
		<category><![CDATA[yii framework]]></category>

		<guid isPermaLink="false">http://webscriptz.be/2011/02/16/yii-framework-rbac/369</guid>
		<description><![CDATA[Rbac in the Yiiframework isn&#8217;t hard at all but how do you keep your db clean and readable code readable and logic! After you have successfully configured the RBAC in /protected/config/main.php you need to insert the sql into your database and introduce your rules and accesses into to table, mine looks like this: And my [...]]]></description>
			<content:encoded><![CDATA[<p>Rbac in the Yiiframework isn&#8217;t hard at all but how do you keep your db clean and readable code readable and logic!</p>
<p>After you have successfully configured the RBAC in /protected/config/main.php you need to insert the sql into your database and introduce your rules and accesses into to table, mine looks like this:</p>
<p><img src="http://webscriptz.be/wp-content/uploads/2011/02/002.png" alt="" title="Selection_001" /></p>
<p>And my code:</p>
<p><img src="http://webscriptz.be/wp-content/uploads/2011/02/001.png" alt="" title="Selection_002" /></p>
<p>This shows you how clean it can be first i have &#8216;moduleName.controllerName.actionName&#8217; which even when working in the db has you focused on what you&#8217;re doing.</p>
<p class="facebook"><a href="http://www.facebook.com/share.php?u=http://webscriptz.be/2011/02/16/yii-framework-rbac/369" target="_blank"><img src="http://webscriptz.be/wp-content/plugins/add-to-facebook-plugin/facebook_share_icon.gif" alt="Share on Facebook" title="Share on Facebook" /></a><a href="http://www.facebook.com/share.php?u=http://webscriptz.be/2011/02/16/yii-framework-rbac/369" target="_blank" title="Share on Facebook">Share on Facebook</a></p>]]></content:encoded>
			<wfw:commentRss>http://webscriptz.be/2011/02/16/yii-framework-rbac/369/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Yii, divide your models into folders</title>
		<link>http://webscriptz.be/2010/11/13/yii-divide-your-models-into-folders/354</link>
		<comments>http://webscriptz.be/2010/11/13/yii-divide-your-models-into-folders/354#comments</comments>
		<pubDate>Sat, 13 Nov 2010 15:24:45 +0000</pubDate>
		<dc:creator>webscriptz</dc:creator>
				<category><![CDATA[post]]></category>
		<category><![CDATA[configuration]]></category>
		<category><![CDATA[hierarchy]]></category>
		<category><![CDATA[module]]></category>
		<category><![CDATA[php]]></category>
		<category><![CDATA[Projects]]></category>
		<category><![CDATA[yii]]></category>
		<category><![CDATA[yii framework]]></category>

		<guid isPermaLink="false">http://webscriptz.be/2010/11/13/yii-divide-oyour-models-into-folders/354</guid>
		<description><![CDATA[Working with models in Yii is easy but you can&#8217;t let them confuse you so I propose you subdivide your models in two categories the form models and the AR models. Note that you will to have to indicate this in each module: Share on Facebook]]></description>
			<content:encoded><![CDATA[<p>Working with models in Yii is easy but you can&#8217;t let them confuse you so I propose you subdivide your models in two categories the form models and the AR models.</p>
<p><img class="alignnone size-full wp-image-352" title="yiiArForms" src="http://webscriptz.be/wp-content/uploads/2010/11/yiiArForms.png" alt="yii config" width="420" height="137" /></p>
<p>Note that you will to have to indicate this in each module:</p>
<p><img class="alignnone size-full wp-image-353" title="yiiArFormModule" src="http://webscriptz.be/wp-content/uploads/2010/11/yiiArFormModule.png" alt="yii module" width="536" height="215" /></p>
<p class="facebook"><a href="http://www.facebook.com/share.php?u=http://webscriptz.be/2010/11/13/yii-divide-your-models-into-folders/354" target="_blank"><img src="http://webscriptz.be/wp-content/plugins/add-to-facebook-plugin/facebook_share_icon.gif" alt="Share on Facebook" title="Share on Facebook" /></a><a href="http://www.facebook.com/share.php?u=http://webscriptz.be/2010/11/13/yii-divide-your-models-into-folders/354" target="_blank" title="Share on Facebook">Share on Facebook</a></p>]]></content:encoded>
			<wfw:commentRss>http://webscriptz.be/2010/11/13/yii-divide-your-models-into-folders/354/feed</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Yii &amp; encryption</title>
		<link>http://webscriptz.be/2010/11/13/yii-encryption/344</link>
		<comments>http://webscriptz.be/2010/11/13/yii-encryption/344#comments</comments>
		<pubDate>Sat, 13 Nov 2010 15:02:45 +0000</pubDate>
		<dc:creator>webscriptz</dc:creator>
				<category><![CDATA[post]]></category>
		<category><![CDATA[AES]]></category>
		<category><![CDATA[decrypt]]></category>
		<category><![CDATA[encrypt]]></category>
		<category><![CDATA[encryption]]></category>
		<category><![CDATA[php]]></category>
		<category><![CDATA[programming]]></category>
		<category><![CDATA[rijdael]]></category>
		<category><![CDATA[security]]></category>
		<category><![CDATA[yii]]></category>

		<guid isPermaLink="false">http://webscriptz.be/2010/11/13/yii-encryption/344</guid>
		<description><![CDATA[I&#8217;ve already said a few things about Yii and the encryption in a few posts prior to this one so now let me help everybody a hand in working with the Yii encryption. Basic Configuration The image is the minimal configuration you will need in &#8216;yourApplication/protected/config/main.php Usage Once you have installed this, the only thing [...]]]></description>
			<content:encoded><![CDATA[<p>I&#8217;ve already said a few things about Yii and the encryption in a few posts prior to this one so now let me help everybody a hand in working with the Yii encryption.</p>
<p><strong>Basic Configuration</strong></p>
<p><img class="size-medium wp-image-347 alignnone" title="yiiEncrypt" src="http://webscriptz.be/wp-content/uploads/2010/11/yiiEncrypt.png" alt="" width="620" height="126" /></p>
<p>The image is the minimal configuration you will need in &#8216;yourApplication/protected/config/main.php</p>
<p><strong>Usage</strong></p>
<p>Once you have installed this, the only thing to do is to call for it using the following:</p>
<p>Yii::app()-&gt;secuirtyManager-&gt;encrypt()  &amp; decryp.</p>
<p><strong>A note to the wise</strong></p>
<p>If you, like me, use AES-256, you&#8217;ll need base64_encode &amp; _decode to save it to database if not you won&#8217;t be able to save your passwords and retrieve them correctly.</p>
<p>Using it by encapsulating the Yii::app()-&gt;secuirtyManager-&gt;encrypt() in a base64_encode() if you need to encrypt a password.</p>
<p><a href="http://webscriptz.be/wp-content/uploads/2010/11/yiiEncryptEx.png"><img class="alignnone size-medium wp-image-360" title="yiiEncryptEx" src="http://webscriptz.be/wp-content/uploads/2010/11/yiiEncryptEx.png" alt="" width="740" height="55" /></a></p>
<p>If you want to decode a string you need to encapsulate base64_decode in Yii::app()-&gt;secuirtyManager-&gt;encrypt().</p>
<p class="facebook"><a href="http://www.facebook.com/share.php?u=http://webscriptz.be/2010/11/13/yii-encryption/344" target="_blank"><img src="http://webscriptz.be/wp-content/plugins/add-to-facebook-plugin/facebook_share_icon.gif" alt="Share on Facebook" title="Share on Facebook" /></a><a href="http://www.facebook.com/share.php?u=http://webscriptz.be/2010/11/13/yii-encryption/344" target="_blank" title="Share on Facebook">Share on Facebook</a></p>]]></content:encoded>
			<wfw:commentRss>http://webscriptz.be/2010/11/13/yii-encryption/344/feed</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Rijndael_256 &amp; php &amp; Yii frameworkstoring encrypted data</title>
		<link>http://webscriptz.be/2010/11/02/rijndael_256-php-yii-frameworkstoring-encrypted-data/337</link>
		<comments>http://webscriptz.be/2010/11/02/rijndael_256-php-yii-frameworkstoring-encrypted-data/337#comments</comments>
		<pubDate>Mon, 01 Nov 2010 23:16:04 +0000</pubDate>
		<dc:creator>webscriptz</dc:creator>
				<category><![CDATA[post]]></category>
		<category><![CDATA[AES]]></category>
		<category><![CDATA[auth]]></category>
		<category><![CDATA[authentication]]></category>
		<category><![CDATA[db]]></category>
		<category><![CDATA[php]]></category>
		<category><![CDATA[programming]]></category>
		<category><![CDATA[rijdael]]></category>
		<category><![CDATA[yii]]></category>

		<guid isPermaLink="false">http://webscriptz.be/2010/11/02/rijndael_256-php-yii-frameworkstoring-encrypted-data/337</guid>
		<description><![CDATA[While programming my authentication module today, i&#8217;ve been experimenting with Rijndael 256 or AES as it is also known. This isn&#8217;t hard, at least not the code because php already includes everything as does Yii for that matter, no, the hard thing is to store an encrypted password. AES encodes your password but does so [...]]]></description>
			<content:encoded><![CDATA[<p>While programming my authentication module today, i&#8217;ve been experimenting with Rijndael 256 or AES as it is also known. This isn&#8217;t hard, at least not the code because php already includes everything as does Yii for that matter, no, the hard thing is to store an encrypted password.</p>
<p>AES encodes your password but does so also by using characters that aren&#8217;t &#8216;printable&#8217; at least not as signs. So I basically began looking for a way to have AES encode everything in characters in the ascii range, luckily for me php already has such functions included: base64_encode and _decode which is nice.</p>
<p class="facebook"><a href="http://www.facebook.com/share.php?u=http://webscriptz.be/2010/11/02/rijndael_256-php-yii-frameworkstoring-encrypted-data/337" target="_blank"><img src="http://webscriptz.be/wp-content/plugins/add-to-facebook-plugin/facebook_share_icon.gif" alt="Share on Facebook" title="Share on Facebook" /></a><a href="http://www.facebook.com/share.php?u=http://webscriptz.be/2010/11/02/rijndael_256-php-yii-frameworkstoring-encrypted-data/337" target="_blank" title="Share on Facebook">Share on Facebook</a></p>]]></content:encoded>
			<wfw:commentRss>http://webscriptz.be/2010/11/02/rijndael_256-php-yii-frameworkstoring-encrypted-data/337/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>MarchandiseDuWeb.com</title>
		<link>http://webscriptz.be/2010/10/21/marchandiseduweb-com/336</link>
		<comments>http://webscriptz.be/2010/10/21/marchandiseduweb-com/336#comments</comments>
		<pubDate>Thu, 21 Oct 2010 21:03:37 +0000</pubDate>
		<dc:creator>webscriptz</dc:creator>
				<category><![CDATA[post]]></category>
		<category><![CDATA[db]]></category>
		<category><![CDATA[marchandiseduweb]]></category>
		<category><![CDATA[mdw]]></category>
		<category><![CDATA[mdw.com]]></category>
		<category><![CDATA[mysql]]></category>
		<category><![CDATA[php]]></category>
		<category><![CDATA[project]]></category>
		<category><![CDATA[yii]]></category>

		<guid isPermaLink="false">http://webscriptz.be/2010/10/21/marchandiseduweb-com/336</guid>
		<description><![CDATA[Alright, The first real website I&#8217;ve written in Yii framework is online, just head over to http://marchandiseduweb.com (hopefully you know some French). I&#8217;m not telling you it&#8217;s a finished project because the database needs some items in it and you don&#8217;t see all the website (yet) online, just the &#8216;front website&#8217; because the other parts [...]]]></description>
			<content:encoded><![CDATA[<p>Alright,</p>
<p>The first real website I&#8217;ve written in Yii framework is online, just head over to <a href="http://marchandiseduweb.com" rel="nofollow">http://marchandiseduweb.com</a> (hopefully you know some French).</p>
<p>I&#8217;m not telling you it&#8217;s a finished project because the database needs some items in it and you don&#8217;t see all the website (yet) online, just the &#8216;front website&#8217; because the other parts have yet to be scripted, luckely the database design is complet/</p>
<p class="facebook"><a href="http://www.facebook.com/share.php?u=http://webscriptz.be/2010/10/21/marchandiseduweb-com/336" target="_blank"><img src="http://webscriptz.be/wp-content/plugins/add-to-facebook-plugin/facebook_share_icon.gif" alt="Share on Facebook" title="Share on Facebook" /></a><a href="http://www.facebook.com/share.php?u=http://webscriptz.be/2010/10/21/marchandiseduweb-com/336" target="_blank" title="Share on Facebook">Share on Facebook</a></p>]]></content:encoded>
			<wfw:commentRss>http://webscriptz.be/2010/10/21/marchandiseduweb-com/336/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>I made my decision about my project, hav&#8230;</title>
		<link>http://webscriptz.be/2010/10/19/i-made-my-decision-about-my-project-hav/335</link>
		<comments>http://webscriptz.be/2010/10/19/i-made-my-decision-about-my-project-hav/335#comments</comments>
		<pubDate>Tue, 19 Oct 2010 20:37:44 +0000</pubDate>
		<dc:creator>webscriptz</dc:creator>
				<category><![CDATA[status]]></category>
		<category><![CDATA[me]]></category>
		<category><![CDATA[php]]></category>
		<category><![CDATA[project]]></category>
		<category><![CDATA[yii]]></category>

		<guid isPermaLink="false">http://webscriptz.be/2010/10/19/i-made-my-decision-about-my-project-hav/335</guid>
		<description><![CDATA[I made my decision about my project, having learned a lot with it, I think it&#8217;s time to recode the little thing. The database only counts 12 or 13 tables so it&#8217;s a not much but it&#8217;s rather that part of my application that needs some love and attention to get it to look more [...]]]></description>
			<content:encoded><![CDATA[<p>I made my decision about my project, having learned a lot with it, I think it&#8217;s time to recode the little thing. The database only counts 12 or 13 tables so it&#8217;s a not much but it&#8217;s rather that part of my application that needs some love and attention to get it to look more professional and uniform.</p>
<p class="facebook"><a href="http://www.facebook.com/share.php?u=http://webscriptz.be/2010/10/19/i-made-my-decision-about-my-project-hav/335" target="_blank"><img src="http://webscriptz.be/wp-content/plugins/add-to-facebook-plugin/facebook_share_icon.gif" alt="Share on Facebook" title="Share on Facebook" /></a><a href="http://www.facebook.com/share.php?u=http://webscriptz.be/2010/10/19/i-made-my-decision-about-my-project-hav/335" target="_blank" title="Share on Facebook">Share on Facebook</a></p>]]></content:encoded>
			<wfw:commentRss>http://webscriptz.be/2010/10/19/i-made-my-decision-about-my-project-hav/335/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

