<?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>Jonah's Thoughts &#187; tables</title>
	<atom:link href="http://php-thoughts.cubedwater.com/tag/tables/feed/" rel="self" type="application/rss+xml" />
	<link>http://php-thoughts.cubedwater.com</link>
	<description>On PHP and things related</description>
	<lastBuildDate>Mon, 21 Jun 2010 01:38:44 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.9.1</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Table prefixes</title>
		<link>http://php-thoughts.cubedwater.com/2009/table-prefixes/</link>
		<comments>http://php-thoughts.cubedwater.com/2009/table-prefixes/#comments</comments>
		<pubDate>Thu, 09 Apr 2009 22:43:33 +0000</pubDate>
		<dc:creator>Jonah</dc:creator>
				<category><![CDATA[Yii]]></category>
		<category><![CDATA[active records]]></category>
		<category><![CDATA[tables]]></category>

		<guid isPermaLink="false">http://php-thoughts.cubedwater.com/?p=227</guid>
		<description><![CDATA[<link rel="stylesheet" type="text/css" href="/wp-content/plugins/syntax.css" />One question I hear many people ask around the Yii forum is how to use table prefixes with ActiveRecord.  I believe the best way to solve this problem is to extend CActiveRecord::tableName().  Now you can either extend this method in every active record class you would like to use a prefix manually, or [...]]]></description>
			<content:encoded><![CDATA[<link rel="stylesheet" type="text/css" href="/wp-content/plugins/syntax.css" /><p>One question I hear many people ask around the Yii forum is how to use table prefixes with ActiveRecord.  I believe the best way to solve this problem is to extend <a href="http://www.yiiframework.com/doc/api/CActiveRecord#tableName-detail">CActiveRecord::tableName()</a>.  Now you can either extend this method in every active record class you would like to use a prefix manually, or you can create a new active record class that supports this functionality, and then have all your other active record classes extend this new class.  This is what I mean:</p>
<p>Normally your active record classes would have the following inheritance:<br />
<code>[...] -> CModel -> CActiveRecord -> YourARModels</code></p>
<p>Instead, you may create a new active record class with custom functionality that you want all your active record classes to have, such as the table prefix functionality.  So the inheritance would look like this:<br />
<code>[...] -> CModel -> CActiveRecord -> ActiveRecord -> YourARModels</code></p>
<p>To do this simply create a ActiveRecord class and save it in a directory that is set to auto-load classes on demand (probably <em>/protected/models</em> or <em>/protected/components</em> should work).  It could look something like this:</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span class="kw2">&lt;?php</span>
<span class="kw2">class</span> ActiveRecord <span class="kw2">extends</span> CActiveRecord
<span class="br0">&#123;</span>
	<span class="kw2">public</span> <span class="re0">$tablePrefix</span> <span class="sy0">=</span> <span class="st_h">'myPrefix_'</span><span class="sy0">;</span>
&nbsp;
	<span class="kw2">public</span> <span class="kw2">function</span> tableName<span class="br0">&#40;</span><span class="br0">&#41;</span>
	<span class="br0">&#123;</span>
		<span class="kw1">return</span> <span class="re0">$this</span><span class="sy0">-&gt;</span><span class="me1">tablePrefix</span><span class="sy0">.</span>parent<span class="sy0">::</span><span class="me2">tableName</span><span class="br0">&#40;</span><span class="br0">&#41;</span><span class="sy0">;</span>
	<span class="br0">&#125;</span>
<span class="br0">&#125;</span></pre></div></div>

<p>Then you would have all your active record classes simply extend <em>ActiveRecord</em> instead of <em>CActiveRecord</em>.  If you do not want a single active record to have a prefix, you can set the <em>$tablePrefix</em> attribute to an empty string in that active record.</p>
<p>You may of course add other useful functionalities that you would like your active record models to share in this class.</p>
]]></content:encoded>
			<wfw:commentRss>http://php-thoughts.cubedwater.com/2009/table-prefixes/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
	</channel>
</rss>
