<?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>Joey Rivera &#187; zend_codegenerator_php_class</title>
	<atom:link href="http://www.joeyrivera.com/tag/zend_codegenerator_php_class/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.joeyrivera.com</link>
	<description>Blogging about PHP, Actionscript, MySQL, and other interests.</description>
	<lastBuildDate>Fri, 02 Dec 2011 03:55:50 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.2.1</generator>
		<item>
		<title>Automate Db Model Creation with Zend_CodeGenerator_Php_Class</title>
		<link>http://www.joeyrivera.com/2009/automate-model-creation-with-zend_codegenerator_php_class/</link>
		<comments>http://www.joeyrivera.com/2009/automate-model-creation-with-zend_codegenerator_php_class/#comments</comments>
		<pubDate>Tue, 15 Dec 2009 19:47:35 +0000</pubDate>
		<dc:creator>Joey</dc:creator>
				<category><![CDATA[Flash]]></category>
		<category><![CDATA[MySQL]]></category>
		<category><![CDATA[codegenerator]]></category>
		<category><![CDATA[PHP]]></category>
		<category><![CDATA[zend]]></category>
		<category><![CDATA[zend framework]]></category>
		<category><![CDATA[zend_codegenerator_php_class]]></category>
		<category><![CDATA[zend_db_table_abstract]]></category>

		<guid isPermaLink="false">http://www.joeyrivera.com/?p=462</guid>
		<description><![CDATA[I&#8217;m working on a new tool at work that will automate several processes for a few employees so they don&#8217;t have to spend too much time doing very repetitive tasks. This tool has to do a good bit of database manipulation so I&#8217;ve decided I&#8217;ll build it in PHP using Zend Framework. I&#8217;ll be using [...]]]></description>
			<content:encoded><![CDATA[<p>I&#8217;m working on a new tool at work that will automate several processes for a few employees so they don&#8217;t have to spend too much time doing very repetitive tasks. This tool has to do a good bit of database manipulation so I&#8217;ve decided I&#8217;ll build it in PHP using Zend Framework.</p>
<p>I&#8217;ll be using <a title="Zend Db Table Docs" href="http://framework.zend.com/manual/en/zend.d.table.html">Zend_Db_Table_Abstract</a> to communicate with the db tables from my project and I&#8217;ll be creating a model for each table as well to store and manipulate data. I&#8217;ll be working with lots of tables in the database and many have lots of fields.</p>
<p>I start by opening up Zend Studio on one monitor and SQL Query Analyzer on the other and get to work. The first table I want to work with is the &#8216;Student&#8217; table. I create a new file in my project called Student.php. Place it on my models/DbTable folder and inside I simply have to declare &#8216;_name&#8217; as a protected property with the value &#8216;Student&#8217; and extend &#8216;Zend_Db_Table_Abstract&#8217;. Easy enough but now I want to create the model I will be using to convert the database data into workable objects through my mapper class.</p>
<h4>Problem</h4>
<p>I create a new file called &#8216;Student.php&#8217; and save it to my models folder. I open the file up and now I have to create a property (it&#8217;s actually an array _data with all properties defined as keys inside) for each field in the Student table&#8230; all 50 of them! I have to be careful to name each correctly as well as to not accidentally miss some field. It ends up being a time consuming process and inefficient so I start looking for a better way to accomplish this.<span id="more-465"></span></p>
<h4>Solution</h4>
<p>This is where <a title="Zend CodeGenerator Php Class" href="http://framework.zend.com/manual/en/zend.codegenerator.html">Zend_CodeGenerator_Php_Class</a> comes in. This component of Zend allows you to create php code on the fly. My theory was, instead of manually creating all these files and typing all the field names (very time consuming with lots of room for error), I could use Zend_CodeGenerator_Php to create all the content of my classes and echo it on the screen so I could copy/paste. Once I had this working, I took it to the next step and included the process of saving that content to files on my project folder. After working on this last night, everything worked like a charm and my classes are easily and quickly created, mapped directly to my database structure, by simply calling one method. No room for error either and I can run this at any time!</p>
<h4>Code</h4>
<p>I&#8217;ll explain the code below. <a title="File of code below" href="http://www.joeyrivera.com/blog_files/462/file.txt">You can download the file if you like</a>.</p>
<div class="dean_ch" style="white-space: wrap;"><span class="kw2">&lt;?php</span><br />
<span class="re0">$db</span> = Zend_Registry::<span class="me2">get</span><span class="br0">&#40;</span><span class="st0">&#8216;db&#8217;</span><span class="br0">&#41;</span>;<br />
<span class="re0">$paths</span> = Zend_Registry::<span class="me2">get</span><span class="br0">&#40;</span><span class="st0">&#8216;paths&#8217;</span><span class="br0">&#41;</span>;<br />
<span class="re0">$temp_path</span> = <span class="re0">$paths</span><span class="br0">&#91;</span><span class="st0">&#8216;temp&#8217;</span><span class="br0">&#93;</span>;</p>
<p><span class="co1">// get all tables in db</span><br />
<span class="re0">$tables</span> = <span class="re0">$db</span>-&gt;<span class="me1">listTables</span><span class="br0">&#40;</span><span class="br0">&#41;</span>;</p>
<p><span class="kw1">foreach</span><span class="br0">&#40;</span><span class="re0">$tables</span> <span class="kw1">as</span> <span class="re0">$table</span><span class="br0">&#41;</span><br />
<span class="br0">&#123;</span><br />
&nbsp; &nbsp; <span class="co1">// need to remove underline first, ucwords, and then remove space</span><br />
&nbsp; &nbsp; <span class="re0">$name</span> = <a href="http://www.php.net/str_replace"><span class="kw3">str_replace</span></a><span class="br0">&#40;</span><span class="st0">&#8216; &#8216;</span>, <span class="st0">&#8221;</span>, <a href="http://www.php.net/ucwords"><span class="kw3">ucwords</span></a><span class="br0">&#40;</span><a href="http://www.php.net/str_replace"><span class="kw3">str_replace</span></a><span class="br0">&#40;</span><span class="st0">&#8216;_&#8217;</span>, <span class="st0">&#8216; &#8216;</span>, <span class="re0">$table</span><span class="br0">&#41;</span><span class="br0">&#41;</span><span class="br0">&#41;</span>;</p>
<p>&nbsp; &nbsp; <span class="co1">// create new class generator</span><br />
&nbsp; &nbsp; <span class="re0">$class</span> = <span class="kw2">new</span> Zend_CodeGenerator_Php_Class<span class="br0">&#40;</span><span class="br0">&#41;</span>;</p>
<p>&nbsp; &nbsp; <span class="co1">// configure docblock</span><br />
&nbsp; &nbsp; <span class="re0">$docblock</span> = <span class="kw2">new</span> Zend_CodeGenerator_Php_Docblock<span class="br0">&#40;</span><a href="http://www.php.net/array"><span class="kw3">array</span></a><span class="br0">&#40;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="st0">&#8216;shortDescription&#8217;</span> =&gt; <span class="re0">$name</span> . <span class="st0">&#8216; model&#8217;</span>,<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="st0">&#8216;tags&#8217;</span> =&gt; <a href="http://www.php.net/array"><span class="kw3">array</span></a><span class="br0">&#40;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <a href="http://www.php.net/array"><span class="kw3">array</span></a><span class="br0">&#40;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="st0">&#8216;name&#8217;</span> =&gt; <span class="st0">&#8216;author&#8217;</span>,<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="st0">&#8216;description&#8217;</span> =&gt; <span class="st0">&#8216;Joey Rivera&#8217;</span>,<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#41;</span><span class="br0">&#41;</span>;</p>
<p>&nbsp; &nbsp; &nbsp; &nbsp; <span class="co1">// set name and docblock</span><br />
&nbsp; &nbsp; <span class="re0">$class</span>-&gt;<span class="me1">setName</span><span class="br0">&#40;</span><span class="st0">&#8216;Ct_Model_&#8217;</span> . <span class="re0">$name</span><span class="br0">&#41;</span>;<br />
&nbsp; &nbsp; <span class="re0">$class</span>-&gt;<span class="me1">setDocblock</span><span class="br0">&#40;</span><span class="re0">$docblock</span><span class="br0">&#41;</span>;</p>
<p>&nbsp; &nbsp; <span class="co1">// get all fields</span><br />
&nbsp; &nbsp; <span class="re0">$fields</span> = <span class="re0">$db</span>-&gt;<span class="me1">describeTable</span><span class="br0">&#40;</span><span class="re0">$table</span><span class="br0">&#41;</span>;</p>
<p>&nbsp; &nbsp; <span class="co1">// want to track primary ids for table</span><br />
&nbsp; &nbsp; <span class="re0">$primary</span> = <a href="http://www.php.net/array"><span class="kw3">array</span></a><span class="br0">&#40;</span><span class="br0">&#41;</span>;</p>
<p>&nbsp; &nbsp; <span class="co1">// add to columns each field with a default value</span><br />
&nbsp; &nbsp; <span class="re0">$columns</span> = <a href="http://www.php.net/array"><span class="kw3">array</span></a><span class="br0">&#40;</span><span class="br0">&#41;</span>;<br />
&nbsp; &nbsp; <span class="kw1">foreach</span><span class="br0">&#40;</span><span class="re0">$fields</span> <span class="kw1">as</span> <span class="re0">$field</span><span class="br0">&#41;</span><br />
&nbsp; &nbsp; <span class="br0">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span class="co1">// if int field default to 0</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span class="re0">$columns</span><span class="br0">&#91;</span><span class="re0">$field</span><span class="br0">&#91;</span><span class="st0">&#8216;COLUMN_NAME&#8217;</span><span class="br0">&#93;</span><span class="br0">&#93;</span> =<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <a href="http://www.php.net/strpos"><span class="kw3">strpos</span></a><span class="br0">&#40;</span><span class="re0">$field</span><span class="br0">&#91;</span><span class="st0">&#8216;DATA_TYPE&#8217;</span><span class="br0">&#93;</span>, <span class="st0">&#8216;int&#8217;</span><span class="br0">&#41;</span> !== <span class="kw2">false</span> ? <span class="nu0">0</span> : <span class="st0">&#8221;</span>;</p>
<p>&nbsp; &nbsp; &nbsp; &nbsp; <span class="co1">// track primary field(s) for table</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span class="kw1">if</span><span class="br0">&#40;</span><span class="re0">$field</span><span class="br0">&#91;</span><span class="st0">&#8216;PRIMARY&#8217;</span><span class="br0">&#93;</span><span class="br0">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="re0">$primary</span><span class="br0">&#91;</span><span class="br0">&#93;</span> = <span class="re0">$field</span><span class="br0">&#91;</span><span class="st0">&#8216;COLUMN_NAME&#8217;</span><span class="br0">&#93;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#125;</span><br />
&nbsp; &nbsp; <span class="br0">&#125;</span></p>
<p>&nbsp; &nbsp; <span class="co1">// add data array property to class</span><br />
&nbsp; &nbsp; <span class="re0">$class</span>-&gt;<span class="me1">setProperty</span><span class="br0">&#40;</span><a href="http://www.php.net/array"><span class="kw3">array</span></a><span class="br0">&#40;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="st0">&#8216;name&#8217;</span> =&gt; <span class="st0">&#8216;_data&#8217;</span>,<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="st0">&#8216;visibility&#8217;</span> =&gt; <span class="st0">&#8216;protected&#8217;</span>,<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="st0">&#8216;defaultValue&#8217;</span> =&gt; <span class="re0">$columns</span>,<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span class="st0">&#8216;docblock&#8217;</span> =&gt; <a href="http://www.php.net/array"><span class="kw3">array</span></a><span class="br0">&#40;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="st0">&#8216;tags&#8217;</span> =&gt; <a href="http://www.php.net/array"><span class="kw3">array</span></a><span class="br0">&#40;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="kw2">new</span> Zend_CodeGenerator_Php_Docblock_Tag<span class="br0">&#40;</span><a href="http://www.php.net/array"><span class="kw3">array</span></a><span class="br0">&#40;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="st0">&#8216;name&#8217;</span> =&gt; <span class="st0">&#8216;var&#8217;</span>,<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="st0">&#8216;description&#8217;</span> =&gt; <span class="st0">&#8216;array Maps to all fields in table&#8217;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#41;</span><span class="br0">&#41;</span>;</p>
<p>&nbsp; &nbsp; &nbsp; &nbsp; <a href="http://www.php.net/echo"><span class="kw3">echo</span></a> <span class="re0">$class</span>-&gt;<span class="me1">generate</span><span class="br0">&#40;</span><span class="br0">&#41;</span> . PHP_EOL;<br />
&nbsp; &nbsp; &nbsp; &nbsp; file_put_contents<span class="br0">&#40;</span><span class="re0">$temp_path</span> . <span class="re0">$name</span> . <span class="st0">&#8216;.php&#8217;</span>, <span class="st0">&#8216;&lt;?php&#8217;</span> . PHP_EOL . <span class="re0">$class</span>-&gt;<span class="me1">generate</span><span class="br0">&#40;</span><span class="br0">&#41;</span><span class="br0">&#41;</span>;</p>
<p>&nbsp; &nbsp; &nbsp; &nbsp; <span class="co1">// create zend_db_table_abstract</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span class="re0">$db_class</span> = <span class="kw2">new</span> Zend_CodeGenerator_Php_Class<span class="br0">&#40;</span><span class="br0">&#41;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span class="re0">$db_class</span>-&gt;<span class="me1">setName</span><span class="br0">&#40;</span><span class="st0">&#8216;Ct_Model_DbTable_&#8217;</span> . <span class="re0">$name</span><span class="br0">&#41;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span class="re0">$db_class</span>-&gt;<span class="me1">setDocblock</span><span class="br0">&#40;</span><span class="kw2">new</span> Zend_CodeGenerator_Php_Docblock<span class="br0">&#40;</span><a href="http://www.php.net/array"><span class="kw3">array</span></a><span class="br0">&#40;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="st0">&#8216;shortDescription&#8217;</span> =&gt; <span class="re0">$name</span> . <span class="st0">&#8216; db table abstract&#8217;</span>,<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="st0">&#8216;tags&#8217;</span> =&gt; <a href="http://www.php.net/array"><span class="kw3">array</span></a><span class="br0">&#40;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <a href="http://www.php.net/array"><span class="kw3">array</span></a><span class="br0">&#40;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="st0">&#8216;name&#8217;</span> =&gt; <span class="st0">&#8216;author&#8217;</span>,<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="st0">&#8216;description&#8217;</span> =&gt; <span class="st0">&#8216;Joey Rivera&#8217;</span>,<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#41;</span><span class="br0">&#41;</span><span class="br0">&#41;</span>;</p>
<p>&nbsp; &nbsp; &nbsp; &nbsp; <span class="re0">$db_class</span>-&gt;<span class="me1">setExtendedClass</span><span class="br0">&#40;</span><span class="st0">&#8216;Zend_Db_Table_Abstract&#8217;</span><span class="br0">&#41;</span>;</p>
<p>&nbsp; &nbsp; &nbsp; &nbsp; <span class="re0">$db_class</span>-&gt;<span class="me1">setProperty</span><span class="br0">&#40;</span><a href="http://www.php.net/array"><span class="kw3">array</span></a><span class="br0">&#40;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="st0">&#8216;name&#8217;</span> =&gt; <span class="st0">&#8216;_name&#8217;</span>,<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="st0">&#8216;visibility&#8217;</span> =&gt; <span class="st0">&#8216;protected&#8217;</span>,<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="st0">&#8216;defaultValue&#8217;</span> =&gt; <span class="re0">$table</span>,<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="st0">&#8216;docblock&#8217;</span> =&gt; <a href="http://www.php.net/array"><span class="kw3">array</span></a><span class="br0">&#40;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="st0">&#8216;tags&#8217;</span> =&gt; <a href="http://www.php.net/array"><span class="kw3">array</span></a><span class="br0">&#40;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="kw2">new</span> Zend_CodeGenerator_Php_Docblock_Tag<span class="br0">&#40;</span><a href="http://www.php.net/array"><span class="kw3">array</span></a><span class="br0">&#40;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="st0">&#8216;name&#8217;</span> =&gt; <span class="st0">&#8216;var&#8217;</span>,<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="st0">&#8216;description&#8217;</span> =&gt; <span class="st0">&#8216;string Name of db table&#8217;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#41;</span><span class="br0">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#41;</span><span class="br0">&#41;</span>;</p>
<p>&nbsp; &nbsp; &nbsp; &nbsp; <span class="kw1">if</span><span class="br0">&#40;</span><a href="http://www.php.net/count"><span class="kw3">count</span></a><span class="br0">&#40;</span><span class="re0">$primary</span><span class="br0">&#41;</span><span class="br0">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="re0">$db_class</span>-&gt;<span class="me1">setProperty</span><span class="br0">&#40;</span><a href="http://www.php.net/array"><span class="kw3">array</span></a><span class="br0">&#40;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="st0">&#8216;name&#8217;</span> =&gt; <span class="st0">&#8216;_primary&#8217;</span>,<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="st0">&#8216;visibility&#8217;</span> =&gt; <span class="st0">&#8216;protected&#8217;</span>,<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="st0">&#8216;defaultValue&#8217;</span> =&gt; <a href="http://www.php.net/count"><span class="kw3">count</span></a><span class="br0">&#40;</span><span class="re0">$primary</span><span class="br0">&#41;</span> &gt; <span class="nu0">1</span> ? <span class="re0">$primary</span> : <span class="re0">$primary</span><span class="br0">&#91;</span><span class="nu0">0</span><span class="br0">&#93;</span>,<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="st0">&#8216;docblock&#8217;</span> =&gt; <a href="http://www.php.net/array"><span class="kw3">array</span></a><span class="br0">&#40;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="st0">&#8216;tags&#8217;</span> =&gt; <a href="http://www.php.net/array"><span class="kw3">array</span></a><span class="br0">&#40;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="kw2">new</span> Zend_CodeGenerator_Php_Docblock_Tag<span class="br0">&#40;</span><a href="http://www.php.net/array"><span class="kw3">array</span></a><span class="br0">&#40;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="st0">&#8216;name&#8217;</span> =&gt; <span class="st0">&#8216;var&#8217;</span>,<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="st0">&#8216;description&#8217;</span> =&gt; <span class="st0">&#8216;string or array of fields in table&#8217;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#41;</span><span class="br0">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#41;</span><span class="br0">&#41;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#125;</span></p>
<p>&nbsp; &nbsp; &nbsp; &nbsp; <a href="http://www.php.net/echo"><span class="kw3">echo</span></a> <span class="re0">$db_class</span>-&gt;<span class="me1">generate</span><span class="br0">&#40;</span><span class="br0">&#41;</span> . PHP_EOL;<br />
&nbsp; &nbsp; &nbsp; &nbsp; file_put_contents<span class="br0">&#40;</span><span class="re0">$temp_path</span> . <span class="st0">&#8216;DbTable/&#8217;</span> . <span class="re0">$name</span> . <span class="st0">&#8216;.php&#8217;</span>, <span class="st0">&#8216;&lt;?php&#8217;</span> . PHP_EOL . <span class="re0">$db_class</span>-&gt;<span class="me1">generate</span><span class="br0">&#40;</span><span class="br0">&#41;</span><span class="br0">&#41;</span>;<br />
<span class="br0">&#125;</span></div>
<p>I start out by getting some needed variables such as my $db adapter and paths that I will use to store my files in. For this example I am using MySQL (5.1.x) database and Zend Framework 1.9.5. If you are not familiar on how to setup your database adapter <a title="Zend Db Adapter Doc" onclick="javascript:pageTracker._trackPageview('/outbound/article/http://framework.zend.com/manual/en/zend.db.html#zend.db.adapter.connecting.constructor');" href="http://framework.zend.com/manual/en/zend.db.html#zend.db.adapter.connecting.constructor">here is the documentation</a>.</p>
<p>The next step is to call listTables on the db adapter to get an array of all the tables in the database. We want to loop through the array and create two files for each table: an object model and a db table abstract. Each file has a corresponding instance of Zend_CodeGenerator_Php_Class. The first one we create in the code is for the object model. The docblock is the optional comments for the class if you want them. setName is the name you want for the class. In this case I&#8217;m using &#8216;Ct_Model_$name&#8217; where name is the name of the table without any underscores and with the first letter of each word in uppercase. For example, if the table name is: &#8216;user_info&#8217;, name is &#8216;UserInfo&#8217;.</p>
<p>The next information I need is all the fields for that table so I can add them to my data array for each model. Calling describeTable(&#8216;tableName&#8217;) returns that. Now that I have my array with all the fields information for this first table, so I loop through to:</p>
<ul>
<li>assign names and default value(s) of 0 for an int field type or null for anything else into the columns array.</li>
<li>track the primary key(s) for each table. Some tables can have more than one.</li>
</ul>
<p>setProperty is where we create the protected data property array for this class. The name of the property is &#8216;_data&#8217; and the default value is the array we just created &#8216;columns&#8217;. You can also add a docblock to the property which I did here. Since there is no Zend_CodeGenerator_Php_Docblock_Tag_Property I just used the default Tag class passing it name &#8216;var&#8217;. Had I used Zend_CodeGenerator_Php_Docblock_Tag_Param, my comments would have shown &#8216;@param&#8217; when it is really &#8216;@var&#8217;. Try it and you&#8217;ll see what I mean.</p>
<p>Our first class for the model is now complete! I&#8217;m echoing it to the screen so I can see it when I view-source or just use Zend_Debug::dump() instead to see it formatted. The next line saves the newly generated code to a file at a temp_path location. It&#8217;s important to add &#8216;&lt;?php&#8217; before class-&gt;generate() so your file have the php tag in them.</p>
<p>The next part of the code creates the second class that extends <a title="Zend Db Table Abstract Doc" href="http://framework.zend.com/manual/en/zend.db.table.html">Zend_Db_Table_Abstract</a>. I won&#8217;t go into detail with what is happening there since it&#8217;s very similar to the above. If you have any questions do feel free to ask. That&#8217;s pretty much it. Run this code, make sure you have your directories created and after executing you should now have two files for each table in your database.</p>
<p>Here is an example of what you should see after running the page:</p>
<div class="dean_ch" style="white-space: wrap;"><span class="coMULTI">/**<br />
&nbsp;* Bio model<br />
&nbsp;*<br />
&nbsp;* @author Joey Rivera<br />
&nbsp;*/</span><br />
<span class="kw2">class</span> Ct_Model_Bio<br />
<span class="br0">&#123;</span><br />
&nbsp; &nbsp; <span class="coMULTI">/**<br />
&nbsp; &nbsp; &nbsp;* @var array Maps to all fields in table<br />
&nbsp; &nbsp; &nbsp;*/</span><br />
&nbsp; &nbsp; protected <span class="re0">$_data</span> = <a href="http://www.php.net/array"><span class="kw3">array</span></a><span class="br0">&#40;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span class="st0">&#8216;bio_id&#8217;</span> =&gt; <span class="nu0">0</span>,<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span class="st0">&#8216;user_id&#8217;</span> =&gt; <span class="nu0">0</span>,<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span class="st0">&#8216;bio_type_id&#8217;</span> =&gt; <span class="nu0">0</span>,<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span class="st0">&#8216;title&#8217;</span> =&gt; <span class="kw2">null</span>,<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span class="st0">&#8216;content&#8217;</span> =&gt; <span class="kw2">null</span>,<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span class="st0">&#8216;last_update&#8217;</span> =&gt; <span class="kw2">null</span>,<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span class="st0">&#8216;enabled&#8217;</span> =&gt; <span class="nu0">0</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#41;</span>;<br />
<span class="br0">&#125;</span><br />
<span class="coMULTI">/**<br />
&nbsp;* Bio db table abstract<br />
&nbsp;*<br />
&nbsp;* @author Joey Rivera<br />
&nbsp;*/</span><br />
<span class="kw2">class</span> Ct_Model_DbTable_Bio <span class="kw2">extends</span> Zend_Db_Table_Abstract<br />
<span class="br0">&#123;</span><br />
&nbsp; &nbsp; <span class="coMULTI">/**<br />
&nbsp; &nbsp; &nbsp;* @var string Name of db table<br />
&nbsp; &nbsp; &nbsp;*/</span><br />
&nbsp; &nbsp; protected <span class="re0">$_name</span> = <span class="st0">&#8216;bio&#8217;</span>;<br />
&nbsp; &nbsp; <span class="coMULTI">/**<br />
&nbsp; &nbsp; &nbsp;* @var string or array of fields in table<br />
&nbsp; &nbsp; &nbsp;*/</span><br />
&nbsp; &nbsp; protected <span class="re0">$_primary</span> = <span class="st0">&#8216;bio_id&#8217;</span>;<br />
<span class="br0">&#125;</span></div>
<p>This is a work in progress since I&#8217;m still trying different things. After I finished writing this code I went back to the zend docs and noticed there is <a title="Zend CodeGenerator Php File Doc" href="http://framework.zend.com/manual/en/zend.codegenerator.examples.html#zend.codegenerator.examples.file">Zend_CodeGenerator_Php_File</a>. Seems to do the same thing I did above with file_put_contents. Feel free to try that instead.</p>
<h4>Conclusion</h4>
<p>I really enjoy the flexibility of using this method instead of typing everything out myself. It&#8217;s quicker with a lot less room for error. I&#8217;m sure there are already other programs/apps out there that do this but hey, it&#8217;s more fun to reinvent the wheel! Plus I learned about another Zend Framework module that I had not used before.</p>
<p>EDIT:</p>
<p>I had to make to small edits to this post to work more efficiently. The first was to change the line where we set the default value of each field for the columns array to &#8221; instead of null. The second was to set the primary property only if there was a primary field found on that table since not every table will always have one.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.joeyrivera.com/2009/automate-model-creation-with-zend_codegenerator_php_class/feed/</wfw:commentRss>
		<slash:comments>23</slash:comments>
		</item>
	</channel>
</rss>

