<?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; tutorials</title>
	<atom:link href="http://webscriptz.be/topics/tutorials/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>mySQL view and YiiFramework</title>
		<link>http://webscriptz.be/2011/06/23/mysql-view-and-yiiframework/402</link>
		<comments>http://webscriptz.be/2011/06/23/mysql-view-and-yiiframework/402#comments</comments>
		<pubDate>Thu, 23 Jun 2011 18:03:19 +0000</pubDate>
		<dc:creator>webscriptz</dc:creator>
				<category><![CDATA[tutorials]]></category>
		<category><![CDATA[database]]></category>
		<category><![CDATA[learn]]></category>
		<category><![CDATA[mysql]]></category>
		<category><![CDATA[myssql]]></category>
		<category><![CDATA[sql]]></category>
		<category><![CDATA[sql view]]></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=402</guid>
		<description><![CDATA[The sql standard gives the possibility to create a view, a view is or can be a mask of one column for a special user that only has access to some of the data and thus not all the data stored in the column? The view can also be used to regroup data from multiple [...]]]></description>
			<content:encoded><![CDATA[<p>The sql standard gives the possibility to create a view, a view is or can be a mask of one column for a special user that only has access to some of the data and thus not all the data stored in the column? The view can also be used to regroup data from multiple column into one, this can be handy if you need to display that selection a lot.</p>
<p><strong>Limitations</strong><br />
It might be wise to, first, learn what you can do with it as it is not a all-in-one solutions for al your troubles and ideas. You can use a view to DISPLAY data, but unless you display all the data from the column or columns, which most of the time doesn&#8217;t make sense, you can NEVER use it to insert data IF any other columns are required to be filled out.</p>
<p><strong>How to create a view in mySQL</strong><br />
The syntax, normally, can be used in other databases to but I&#8217;m focussing on mySQL. </p>
<p><!--DEVFMTCODE--><pre class="devcodeblock" title="DOS"><div class="devcodeoverflow"><ol><li><span style="color: #33cc33;">&lt;</span>code lang=&quot;sql[lines]&quot;<span style="color: #33cc33;">&gt;</span>CREATE VIEW [name of the view] AS [ your query];</li></ol></div></pre><!--END_DEVFMTCODE--></code></p>
<p>Best is that you make a clear distinction between the original tables and the view by for instance writing "_view" after it, this will help later when reviewing the database or using the view element with the YiiFramework.</p>
<p>You can in fact put everything you want in the [your query] section as long as it is valid SQL. You also need to be careful when using this that whatever query you make, you don't ask the Cartesian product, this however you need to do with every single query you ever make where joints and where clauses are involved.</p>
<p><strong>YiiFramework and Views</strong><br />
This is the fastest part, you create your model with Gii, and you do the rest of your magic to write your application.</p>
<p class="facebook"><a href="http://www.facebook.com/share.php?u=http://webscriptz.be/2011/06/23/mysql-view-and-yiiframework/402" 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/mysql-view-and-yiiframework/402" target="_blank" title="Share on Facebook">Share on Facebook</a></p>]]></content:encoded>
			<wfw:commentRss>http://webscriptz.be/2011/06/23/mysql-view-and-yiiframework/402/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Fedora Core &#8211; virtual hosts httpd</title>
		<link>http://webscriptz.be/2011/03/25/configuring-virtual-hosts-in-apache/382</link>
		<comments>http://webscriptz.be/2011/03/25/configuring-virtual-hosts-in-apache/382#comments</comments>
		<pubDate>Thu, 24 Mar 2011 23:06:20 +0000</pubDate>
		<dc:creator>webscriptz</dc:creator>
				<category><![CDATA[tutorials]]></category>
		<category><![CDATA[127.0.0.1]]></category>
		<category><![CDATA[apache]]></category>
		<category><![CDATA[apache2]]></category>
		<category><![CDATA[bind9]]></category>
		<category><![CDATA[configuration]]></category>
		<category><![CDATA[console]]></category>
		<category><![CDATA[development]]></category>
		<category><![CDATA[dns]]></category>
		<category><![CDATA[fedora]]></category>
		<category><![CDATA[hosts]]></category>
		<category><![CDATA[httpd]]></category>
		<category><![CDATA[linux]]></category>
		<category><![CDATA[local domain name]]></category>
		<category><![CDATA[name-based virtual host]]></category>
		<category><![CDATA[nano]]></category>
		<category><![CDATA[virtual hosts]]></category>

		<guid isPermaLink="false">http://webscriptz.be/?p=382</guid>
		<description><![CDATA[I&#8217;ve reinstalled my pc with Fedora Core recently, because of the extended SELinux and more tools that are at my disposal. A problem I always had with ubuntu was the configuration of Apache virtual hosts. Once they where installed I needed to put them into the host file and each time I reconnected to a [...]]]></description>
			<content:encoded><![CDATA[<p>I&#8217;ve reinstalled my pc with Fedora Core recently, because of the extended SELinux and more tools that are at my disposal. A problem I always had with ubuntu was the configuration of Apache virtual hosts. Once they where installed I needed to put them into the host file and each time I reconnected to a network the NetworkManager rewrote the hosts file annoying me and the fact that if one was configured localhost would also begin pointing to it, a rather nasty thing.</p>
<p>First you need to open the console and login with your root user, type su and fedora will already know what to do.</p>
<p>You need to shutdown the httpd servic deamon with the command :<br />
<!--DEVFMTCODE--><pre class="devcodeblock" title="DOS"><div class="devcodeoverflow"><ol><li>/etc/init.d/httpd stop</li></ol></div></pre><!--END_DEVFMTCODE--></p>
<p>You will have to open /etc/httpd/conf/httpd.conf, once opened you will have to locate the section on virtual hosts and you should see an example that is already commented out., I edited the files with nano in the console.</p>
<p><!--DEVFMTCODE--><pre class="devcodeblock" title="DOS"><div class="devcodeoverflow"><ol><li>### Section 3: Virtual Hosts</li></ol></div></pre><!--END_DEVFMTCODE--></p>
<p>Instead of telling you which lines to uncomment, I&#8217;m going to show you below, the stanzas to add to the Virtual Host section to get companyABC.com and companyDEF.com websites running:<br />
<!--DEVFMTCODE--><pre class="devcodeblock" title="DOS"><div class="devcodeoverflow"><ol><li>#</li><li># Virtual hosts</li><li>#</li><li>&nbsp;</li><li># Virtual host Default Virtual Host</li><li><span style="color: #33cc33;">&lt;</span>VirtualHost *<span style="color: #33cc33;">&gt;</span></li><li>DocumentRoot /var/www/html/</li><li>ErrorLog logs/error_log</li><li>ServerAdmin root<span style="color: #33cc33;">@</span>localhost</li><li>&nbsp;</li><li>&nbsp;</li><li>&nbsp;</li><li>ServerSignature email</li><li>TransferLog logs/access_log</li><li>DirectoryIndex index.php index.html index.htm index.shtml</li><li>&nbsp;</li><li>&nbsp;</li><li>SSLEngine off</li><li>SSLCertificateFile /etc/pki/tls/certs/localhost.crt</li><li>SSLCertificateKeyFile /etc/pki/tls/private/localhost.key</li><li>&nbsp;</li><li>SSLCACertificateFile /etc/pki/tls/certs/ca-bundle.crt</li><li>&nbsp;</li><li>SSLOptions</li><li>LogFormat &quot;None&quot;</li><li>TransferLog logs/access_log</li><li>ErrorLog logs/error_log</li><li>LogLevel debug</li><li>HostNameLookups off</li><li>&nbsp;</li><li>&nbsp;</li><li>&nbsp;</li><li>&nbsp;</li><li><span style="color: #33cc33;">&lt;</span>/VirtualHost<span style="color: #33cc33;">&gt;</span></li><li>&nbsp;</li><li># Virtual host resume.loc</li><li><span style="color: #33cc33;">&lt;</span>VirtualHost 127.0.0.1<span style="color: #33cc33;">&gt;</span></li><li>DocumentRoot /var/www/html/mdw/resume</li><li>ErrorLog logs/error_log</li><li>ServerAdmin root<span style="color: #33cc33;">@</span>localhost</li><li>ServerName resume.loc</li><li>&nbsp;</li><li>&nbsp;</li><li>ServerSignature email</li><li>TransferLog logs/access_log</li><li>DirectoryIndex index.html index.php index.shtml</li><li>&nbsp;</li><li><span style="color: #33cc33;">&lt;</span>Directory &quot;/var/www/html/mdw/resume/&quot;<span style="color: #33cc33;">&gt;</span></li><li>Options all</li><li>&nbsp;</li><li>AllowOverride all</li><li>&nbsp;</li><li>&nbsp;</li><li>&nbsp;</li><li><span style="color: #33cc33;">&lt;</span>/Directory<span style="color: #33cc33;">&gt;</span></li><li>&nbsp;</li><li>&nbsp;</li><li>SSLEngine off</li><li>SSLCertificateFile /etc/pki/tls/certs/localhost.crt</li><li>SSLCertificateKeyFile /etc/pki/tls/private/localhost.key</li><li>&nbsp;</li><li>SSLCACertificateFile /etc/pki/tls/certs/ca-bundle.crt</li><li>&nbsp;</li><li>SSLOptions</li><li>LogFormat &quot;None&quot;</li><li>TransferLog logs/access_log</li><li>ErrorLog logs/error_log</li><li>LogLevel error</li><li>HostNameLookups on</li><li>&nbsp;</li><li>&nbsp;</li><li>&nbsp;</li><li>&nbsp;</li><li><span style="color: #33cc33;">&lt;</span>/VirtualHost<span style="color: #33cc33;">&gt;</span></li><li>&nbsp;</li><li># Virtual host linker</li><li><span style="color: #33cc33;">&lt;</span>VirtualHost 127.0.0.1<span style="color: #33cc33;">&gt;</span></li><li>DocumentRoot /var/www/html/mdw/linkerv2/</li><li>ErrorLog logs/error_log</li><li>ServerAdmin root<span style="color: #33cc33;">@</span>localhost</li><li>ServerName linker.loc</li><li>&nbsp;</li><li>&nbsp;</li><li>ServerSignature email</li><li>TransferLog logs/access_log</li><li>DirectoryIndex index.html index.php index.shtml</li><li>&nbsp;</li><li>&nbsp;</li><li>SSLEngine off</li><li>SSLCertificateFile /etc/pki/tls/certs/localhost.crt</li><li>SSLCertificateKeyFile /etc/pki/tls/private/localhost.key</li><li>&nbsp;</li><li>SSLCACertificateFile /etc/pki/tls/certs/ca-bundle.crt</li><li>&nbsp;</li><li>SSLOptions</li><li>LogFormat &quot;None&quot;</li><li>TransferLog logs/access_log</li><li>ErrorLog logs/error_log</li><li>LogLevel error</li><li>HostNameLookups on</li><li>&nbsp;</li><li>&nbsp;</li><li>&nbsp;</li><li>&nbsp;</li><li><span style="color: #33cc33;">&lt;</span>/VirtualHost<span style="color: #33cc33;">&gt;</span></li><li>&nbsp;</li><li># Virtual host Virtual Host 1</li><li><span style="color: #33cc33;">&lt;</span>VirtualHost 127.0.0.1<span style="color: #33cc33;">&gt;</span></li><li>DocumentRoot /var/www/html/mdw/site</li><li>ErrorLog logs/error_log</li><li>ServerAdmin root<span style="color: #33cc33;">@</span>localhost</li><li>ServerName mdw.loc</li><li>&nbsp;</li><li>&nbsp;</li><li>ServerSignature email</li><li>TransferLog logs/access_log</li><li>DirectoryIndex index.html index.php index.shtml</li><li>&nbsp;</li><li>&nbsp;</li><li>SSLEngine off</li><li>SSLCertificateFile /etc/pki/tls/certs/localhost.crt</li><li>SSLCertificateKeyFile /etc/pki/tls/private/localhost.key</li><li>&nbsp;</li><li>SSLCACertificateFile /etc/pki/tls/certs/ca-bundle.crt</li><li>&nbsp;</li><li>SSLOptions</li><li>&nbsp;</li><li>TransferLog logs/access_log</li><li>ErrorLog logs/error_log</li><li>LogLevel error</li><li>HostNameLookups on</li><li>&nbsp;</li><li>&nbsp;</li><li>&nbsp;</li><li>&nbsp;</li><li><span style="color: #33cc33;">&lt;</span>/VirtualHost<span style="color: #33cc33;">&gt;</span></li><li>&nbsp;</li><li> </li></ol></div></pre><!--END_DEVFMTCODE--></p>
<p>After you have added the host(s), save the file. At this point, you will need to add the appropriate records to your DNS server to make these domains reachable by anyone on the network. If you have no access to a DNS server (BIND9), or you just want to test the configuration, just edit your /etc/hosts file. Here is what my file looks like:</p>
<p><!--DEVFMTCODE--><pre class="devcodeblock" title="DOS"><div class="devcodeoverflow"><ol><li># <span style="color: #00b100; font-weight: bold;">Do</span> <span style="color: #000000; font-weight: bold;">not</span> remove the following line, or various programs</li><li># that require network functionality will fail.</li><li>127.0.0.1&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; localhost.localdomain localhost</li><li>127.0.0.1&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; host1.loc</li><li>127.0.0.1&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; host2.loc</li><li></li></ol></div></pre><!--END_DEVFMTCODE--><br />
Once hosts is saved you just restart your server and everything should work without a problem</p>
<p class="facebook"><a href="http://www.facebook.com/share.php?u=http://webscriptz.be/2011/03/25/configuring-virtual-hosts-in-apache/382" 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/03/25/configuring-virtual-hosts-in-apache/382" target="_blank" title="Share on Facebook">Share on Facebook</a></p>]]></content:encoded>
			<wfw:commentRss>http://webscriptz.be/2011/03/25/configuring-virtual-hosts-in-apache/382/feed</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Annoyed&#8230;with coding</title>
		<link>http://webscriptz.be/2010/02/22/annoyed-with-coding/259</link>
		<comments>http://webscriptz.be/2010/02/22/annoyed-with-coding/259#comments</comments>
		<pubDate>Sun, 21 Feb 2010 23:39:23 +0000</pubDate>
		<dc:creator>webscriptz</dc:creator>
				<category><![CDATA[General]]></category>
		<category><![CDATA[Projects]]></category>
		<category><![CDATA[tutorials]]></category>
		<category><![CDATA[annoyed]]></category>
		<category><![CDATA[framework]]></category>
		<category><![CDATA[oop]]></category>
		<category><![CDATA[php]]></category>
		<category><![CDATA[yii]]></category>
		<category><![CDATA[yii framework]]></category>

		<guid isPermaLink="false">http://webscriptz.be/?p=259</guid>
		<description><![CDATA[I&#8217;ve been coding away all week, that is between my other jobs at home In all my &#8216;wisdom&#8217; and persistence i made a small app with Yii which stores data in an array and serializes it into a db, and a basic CRUD, a proof of concept to see if the framework was capable of [...]]]></description>
			<content:encoded><![CDATA[<p>I&#8217;ve been coding away all week, that is between my other jobs at home <img src='http://webscriptz.be/wp-includes/images/smilies/icon_razz.gif' alt=':P' class='wp-smiley' /> </p>
<p>In all my &#8216;wisdom&#8217; and persistence i made a small app with Yii which stores data in an array and serializes it into a db, and a basic CRUD, a proof of concept to see if the framework was capable of doing this <span style="text-decoration: line-through;">and in the fastest way possible.</span></p>
<p>No problem so far, getting it is simple, but apparently you Yii doesn&#8217;t permit everything. So you need a $temp variable to store the model data in, suffice to say that it isn&#8217;t really the shortest route to take nor in my opinion the fastest bus alas I have to do with it. This little app of not more then a 60 lines of code took me four days, four damn days to figure out the error,  honestly if we have methods to &#8216;save space&#8217; in your coding why not permit them?</p>
<p>I&#8217;m not going to bother everybody with my nagging so, I&#8217;ll stop here.</p>
<p><a href="http://www.yiiframework.com/forum/index.php?/topic/7409-ar-array-problem/">For those who want to see the topic on the yii forums.</a></p>
<p class="facebook"><a href="http://www.facebook.com/share.php?u=http://webscriptz.be/2010/02/22/annoyed-with-coding/259" 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/02/22/annoyed-with-coding/259" target="_blank" title="Share on Facebook">Share on Facebook</a></p>]]></content:encoded>
			<wfw:commentRss>http://webscriptz.be/2010/02/22/annoyed-with-coding/259/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>YII framework configuration</title>
		<link>http://webscriptz.be/2009/12/20/yii-framework-configuration/253</link>
		<comments>http://webscriptz.be/2009/12/20/yii-framework-configuration/253#comments</comments>
		<pubDate>Sun, 20 Dec 2009 22:47:49 +0000</pubDate>
		<dc:creator>webscriptz</dc:creator>
				<category><![CDATA[tutorials]]></category>
		<category><![CDATA[development]]></category>
		<category><![CDATA[framework]]></category>
		<category><![CDATA[MVC]]></category>
		<category><![CDATA[oop]]></category>
		<category><![CDATA[php]]></category>
		<category><![CDATA[php5]]></category>
		<category><![CDATA[project]]></category>
		<category><![CDATA[yii]]></category>
		<category><![CDATA[yii framework]]></category>

		<guid isPermaLink="false">http://webscriptz.be/?p=253</guid>
		<description><![CDATA[I&#8217;m toying with the Yii Framework for some time now and even if i cost me a lot of anger and frustration in the beginning I&#8217;m starting to like it more and more, alas I do have to say that the documentation isn&#8217;t always that clear and for someone who begins or who&#8217;ll write some [...]]]></description>
			<content:encoded><![CDATA[<p>I&#8217;m toying with the Yii Framework for some time now and even if i cost me a lot of anger and frustration in the beginning I&#8217;m starting to like it more and more, alas I do have to say that the documentation isn&#8217;t always that clear and for someone who begins or who&#8217;ll write some large applications the configuration file can be a hassle so here&#8217;s my solution:</p>
<blockquote><p>Brake down the configuration file in multiple files, this will give you some speed disadvantage and some will  saying that I&#8217;m raping Yii framework purpose for speed but at least to me it seems more clear</p></blockquote>
<p>This is the protected.config/main.php</p>
<p><pre class="brush:[php]">
dirname(__FILE__).DIRECTORY_SEPARATOR.'..',
'name'=&gt;'WEBSITENAME',
'modules'=&gt;array(
'users'=&gt;array(
//sub modules in the module users
'modules'=&gt;array(
'messaging',
'profile',
'dashboard',
)
),
'about',
'forums',
),
// preloading 'log' component active loading
'preload'=&gt;array('log'),

// autoloading model and component classes lazy loading
// I make the difference between CformModel and CActiveRecord
'import'=&gt;array(
'application.models.*',
'application.models.forms.*',
'application.models.database.*',
),

// application components
'components'=&gt;array(
// enable cookie-based authentication
'user'=&gt;array('allowAutoLogin'=&gt;true),

// data relinquished to database.php
// for easy access and usability as also for the future
// installation procedure, it's less to write to a file
'db'=&gt;include(dirname(__FILE__).'/database.php'),

// for a better overview we exculded url routes to a seperate file
'urlManager'=&gt;include(dirname(__FILE__).'/routes.php'),

//authentication component needs data from db for CdbConnection
'authManager'=&gt;array(
'class'=&gt;'CDbAuthManager',
'connectionID'=&gt;'db',
'defaultRoles'=&gt;array('authenticated', 'guest'),
),

//security measures
'request'=&gt;array(
'enableCsrfValidation'=&gt;true,
'enableCookieValidation'=&gt;true,
),
),

// application-level parameters that can be accessed
// using Yii::app()-&gt;params['paramName']
// uncomment the following if you want static params in the application
//'params'=&gt;array(include(dirname(__FILE__).'/params.php'))
);</pre></p>
<p>database.php<br />
<pre class="brush:[php]">
'CDbConnection',
'connectionString'=&gt;'mysql:host=localhost;dbname=mysql',
//'connectionString'=&gt;'pgsql:host=localhost;port=5432;dbname=mysql',
'username' =&gt; 'root',
'password' =&gt; '',
);
?&gt;
</pre></p>
<p>routes.php<br />
<pre class="brush:[php]">
'path', // path or get
'urlSuffix' =&gt; '', //.html .whateverextentionyouwant
'showScriptName' =&gt; true,
'rules'=&gt;array(
'users/recovery/perimeterSecurity/'=&gt;'users/recovery/perimeterSecurity',
),
);
?&gt;
</pre></p>
<p>param.php<br />
<pre class="brush:[php]">//nothing in it at the moment</pre></p>
<p class="facebook"><a href="http://www.facebook.com/share.php?u=http://webscriptz.be/2009/12/20/yii-framework-configuration/253" 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/2009/12/20/yii-framework-configuration/253" target="_blank" title="Share on Facebook">Share on Facebook</a></p>]]></content:encoded>
			<wfw:commentRss>http://webscriptz.be/2009/12/20/yii-framework-configuration/253/feed</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Codeigniter Redirect</title>
		<link>http://webscriptz.be/2009/06/30/codeigniter-redirect/231</link>
		<comments>http://webscriptz.be/2009/06/30/codeigniter-redirect/231#comments</comments>
		<pubDate>Tue, 30 Jun 2009 19:45:08 +0000</pubDate>
		<dc:creator>webscriptz</dc:creator>
				<category><![CDATA[Technology]]></category>
		<category><![CDATA[tutorials]]></category>
		<category><![CDATA[CI function]]></category>
		<category><![CDATA[codeigniter]]></category>
		<category><![CDATA[php]]></category>
		<category><![CDATA[redirect]]></category>

		<guid isPermaLink="false">http://webscriptz.be/?p=231</guid>
		<description><![CDATA[redirect() Does a &#8220;header redirect&#8221; to the local URI specified. Just like other functions in this helper, this one is designed to redirect to a local URL within your site. You will not specify the full site URL, but rather simply the URI segments to the controller you want to direct to. The function will [...]]]></description>
			<content:encoded><![CDATA[<blockquote>
<h2>redirect()</h2>
<p>Does a &#8220;header redirect&#8221; to the local URI specified. Just like other functions in this helper, this one is designed to redirect to a local URL within your site.  You will <strong>not</strong> specify the full site URL, but rather simply the URI segments to the controller you want to direct to. The function will build the URL based on your config file values.</p>
<p>The optional second parameter allows you to choose between the &#8220;location&#8221; method (default) or the &#8220;refresh&#8221; method. Location is faster, but on Windows servers it can sometimes be a problem. The optional third parameter allows you to send a specific HTTP Response Code &#8211; this could be used for example to create 301 redirects for search engine purposes. The default Response Code is 302. The third parameter is <em>only</em> available with &#8216;location&#8217; redirects, and not &#8216;refresh&#8217;. Examples:</p>
<p><code>if ($logged_in == FALSE)<br />
{<br />
redirect('/login/form/', 'refresh');<br />
}</p>
<p>// with 301 redirect<br />
redirect('/article/13', 'location', 301);</code></p></blockquote>
<p>This was the excerpt from the codeigniter userguide but what they don&#8217;t really tell is the essential:</p>
<p>redirect(&#8216;controller/function/param&#8217;, &#8216;refresh&#8217;);</p>
<blockquote></blockquote>
<p class="facebook"><a href="http://www.facebook.com/share.php?u=http://webscriptz.be/2009/06/30/codeigniter-redirect/231" 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/2009/06/30/codeigniter-redirect/231" target="_blank" title="Share on Facebook">Share on Facebook</a></p>]]></content:encoded>
			<wfw:commentRss>http://webscriptz.be/2009/06/30/codeigniter-redirect/231/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Agile Application with php &amp; mySQL</title>
		<link>http://webscriptz.be/2009/03/28/agile-application-with-php-mysql/227</link>
		<comments>http://webscriptz.be/2009/03/28/agile-application-with-php-mysql/227#comments</comments>
		<pubDate>Sat, 28 Mar 2009 19:35:14 +0000</pubDate>
		<dc:creator>webscriptz</dc:creator>
				<category><![CDATA[tutorials]]></category>
		<category><![CDATA[dbal]]></category>
		<category><![CDATA[howto]]></category>
		<category><![CDATA[my]]></category>
		<category><![CDATA[php]]></category>
		<category><![CDATA[tutorisql]]></category>

		<guid isPermaLink="false">http://webscriptz.be/?p=227</guid>
		<description><![CDATA[I'm working on a website system an facing this problem where and how to store my configurations in the database. ]]></description>
			<content:encoded><![CDATA[<p>I&#8217;m working on a website system an facing this problem where and how to store my configurations in the database.  So you want to store your configuration vars in a configuration table in the database, that&#8217;s great and fun to do too.</p>
<p>Why do it?</p>
<p>Your application becomes easier to update to a newer version, most of the time, when creating a newer version you will strip &#8211; change &#8211; add some variables for configurations, with a database it&#8217;s easier and you (or the admin) can change things on the fly.</p>
<p>How?</p>
<p>One or more tables like this:</p>
<p><code><br />
Mysql table:<br />
+------------------+<br />
|       config           |<br />
+------------------+<br />
|  id                      |<br />
|  name                 |<br />
|  value                 |<br />
+------------------+<br />
</code></p>
<p>If you want you can add an selection column to get only the ones needed for that function but this is the best way when being in a development environment I think</p>
<p class="facebook"><a href="http://www.facebook.com/share.php?u=http://webscriptz.be/2009/03/28/agile-application-with-php-mysql/227" 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/2009/03/28/agile-application-with-php-mysql/227" target="_blank" title="Share on Facebook">Share on Facebook</a></p>]]></content:encoded>
			<wfw:commentRss>http://webscriptz.be/2009/03/28/agile-application-with-php-mysql/227/feed</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>CodeIgniter the views and .htaccess</title>
		<link>http://webscriptz.be/2009/03/14/codeigniter-and-views-and-htaccess/219</link>
		<comments>http://webscriptz.be/2009/03/14/codeigniter-and-views-and-htaccess/219#comments</comments>
		<pubDate>Sat, 14 Mar 2009 20:23:53 +0000</pubDate>
		<dc:creator>webscriptz</dc:creator>
				<category><![CDATA[General]]></category>
		<category><![CDATA[tutorials]]></category>
		<category><![CDATA[.htaccess]]></category>
		<category><![CDATA[apps]]></category>
		<category><![CDATA[Ci]]></category>
		<category><![CDATA[MVC]]></category>
		<category><![CDATA[php]]></category>
		<category><![CDATA[programming]]></category>
		<category><![CDATA[themes]]></category>
		<category><![CDATA[views]]></category>

		<guid isPermaLink="false">http://webscriptz.be/?p=219</guid>
		<description><![CDATA[I must admit that I not a huge fan of Frameworks and Patterns because: A) They make things complicated B) Subject you to some nasty code stuff and rules C) Because templating and Frameworks can be water and fire. Codeigniter on the opposite has some advantage on it side for the templating stuff. This is [...]]]></description>
			<content:encoded><![CDATA[<p>I must admit that I not a huge fan of Frameworks and Patterns because:</p>
<blockquote><p>A) They make things complicated</p>
<p>B) Subject you to some nasty code stuff and rules</p>
<p>C) Because templating and Frameworks can be water and fire.</p></blockquote>
<p>Codeigniter on the opposite has some advantage on it side for the templating stuff. This is really a short explanation for a common problem in other framework.</p>
<p>What to do if you want a nice folder structure or have different template styles, remember this is just a basic thing which happen to work in CI 1.7.</p>
<p><code><br />
function index(){<br />
$this-&gt;load-&gt;view('Themename/news/index);<br />
}</code></p>
<p>Just add some slashes an it works so normally it should work if you would make an adaptation for themes too with a variable in it.</p>
<p>The next thing was kind of hard to find on the web so let me share it. It&#8217;s for when you develop with CI and your not working in the root directory of your server.</p>
<p><code><br />
DirectoryIndex index.php<br />
RewriteEngine on<br />
RewriteCond $1 !^(index\.php|images|css|js|robots\.txt|favicon\.ico)<br />
RewriteCond %{REQUEST_FILENAME} !-f<br />
RewriteCond %{REQUEST_FILENAME} !-d<br />
RewriteRule ^(.*)$ ./index.php/$1 [L,QSA] </code></p>
<p class="facebook"><a href="http://www.facebook.com/share.php?u=http://webscriptz.be/2009/03/14/codeigniter-and-views-and-htaccess/219" 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/2009/03/14/codeigniter-and-views-and-htaccess/219" target="_blank" title="Share on Facebook">Share on Facebook</a></p>]]></content:encoded>
			<wfw:commentRss>http://webscriptz.be/2009/03/14/codeigniter-and-views-and-htaccess/219/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Restful_authentication</title>
		<link>http://webscriptz.be/2008/08/20/restful_authentication/98</link>
		<comments>http://webscriptz.be/2008/08/20/restful_authentication/98#comments</comments>
		<pubDate>Wed, 20 Aug 2008 19:41:10 +0000</pubDate>
		<dc:creator>webscriptz</dc:creator>
				<category><![CDATA[General]]></category>
		<category><![CDATA[Technology]]></category>
		<category><![CDATA[tutorials]]></category>
		<category><![CDATA[apps]]></category>
		<category><![CDATA[build]]></category>
		<category><![CDATA[create]]></category>
		<category><![CDATA[framework]]></category>
		<category><![CDATA[internet]]></category>
		<category><![CDATA[rails]]></category>
		<category><![CDATA[www]]></category>

		<guid isPermaLink="false">http://webscriptz.be/2008/08/20/restful_authentication/</guid>
		<description><![CDATA[This is probably one of the easiest authantication plugins for rails. I&#8217;m learning it at the moment and it isn&#8217;t easy, the biggest obstacle is the tutorials that often are but available for rails 1.x.x. Railscasts &#8211; restful_authentication. Share on Facebook]]></description>
			<content:encoded><![CDATA[<p>This is probably one of the easiest authantication plugins for rails. I&#8217;m learning it at the moment and it isn&#8217;t easy, the biggest obstacle is the tutorials that often are but available for rails 1.x.x.</p>
<p><a href="http://railscasts.com/episodes/67-restful-authentication">Railscasts &#8211; restful_authentication</a>.</p>
<p class="facebook"><a href="http://www.facebook.com/share.php?u=http://webscriptz.be/2008/08/20/restful_authentication/98" 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/2008/08/20/restful_authentication/98" target="_blank" title="Share on Facebook">Share on Facebook</a></p>]]></content:encoded>
			<wfw:commentRss>http://webscriptz.be/2008/08/20/restful_authentication/98/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

