<?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>Abhi&#039;s Weblog &#187; SQL</title>
	<atom:link href="http://abhinavsingh.com/blog/tag/sql/feed/" rel="self" type="application/rss+xml" />
	<link>http://abhinavsingh.com/blog</link>
	<description>PHP, Memcached, XMPP and Web Development</description>
	<lastBuildDate>Mon, 27 Feb 2012 09:12:57 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
		<item>
		<title>Symfony model layer tips and tricks &#8211; A PHP Framework &#8211; Part 3</title>
		<link>http://abhinavsingh.com/blog/2008/11/symfony-model-layer-tips-and-tricks-a-php-framework-part-3/</link>
		<comments>http://abhinavsingh.com/blog/2008/11/symfony-model-layer-tips-and-tricks-a-php-framework-part-3/#comments</comments>
		<pubDate>Mon, 10 Nov 2008 16:30:07 +0000</pubDate>
		<dc:creator>Abhinav Singh</dc:creator>
				<category><![CDATA[PHP]]></category>
		<category><![CDATA[Database]]></category>
		<category><![CDATA[Model Layer]]></category>
		<category><![CDATA[Propel]]></category>
		<category><![CDATA[SQL]]></category>
		<category><![CDATA[Symfony]]></category>

		<guid isPermaLink="false">http://abhinavsingh.com/blog/2008/11/symfony-model-layer-tips-and-tricks-a-php-framework-part-3/</guid>
		<description><![CDATA[I had a tough time searching for examples over the net for all kind of symfony queries. Though symfony is well documented here, I didn&#8217;t find enough examples there. If you have landed up on this post straight and are new to symfony, you may want to first read previous posts: Getting started with Symfony [...]]]></description>
			<content:encoded><![CDATA[<div class="tweetmeme_button" style="float: right; margin-left: 10px;">
			<a href="http://api.tweetmeme.com/share?url=http%3A%2F%2Fabhinavsingh.com%2Fblog%2F2008%2F11%2Fsymfony-model-layer-tips-and-tricks-a-php-framework-part-3%2F"><br />
				<img src="http://api.tweetmeme.com/imagebutton.gif?url=http%3A%2F%2Fabhinavsingh.com%2Fblog%2F2008%2F11%2Fsymfony-model-layer-tips-and-tricks-a-php-framework-part-3%2F&amp;source=imoracle&amp;style=normal&amp;service=bit.ly&amp;service_api=R_f027b5a79a20a49b713f16282f1e0857&amp;hashtags=Database,Model+Layer,PHP,Propel,SQL,Symfony&amp;b=2" height="61" width="50" /><br />
			</a>
		</div>
<p><span style="color: rgb(0, 0, 0);">I had a tough time searching for examples over the net for all kind of symfony queries. Though symfony is well documented <a href="http://www.symfony-project.org/book/1_0/08-Inside-the-Model-Layer">here</a>, I didn&#8217;t find enough examples there.</p>
<p>If you have landed up on this post straight and are new to symfony, you may want to first read previous posts:<br />
<a href="http://abhinavsingh.com/blog/2008/10/getting-started-with-symfony-a-php-framework-part-1/">Getting started with Symfony &#8211; A PHP Framework &#8211; Part 1</a><br />
<a href="http://abhinavsingh.com/blog/2008/10/how-to-build-a-login-registration-system-using-symfony-a-php-framework-part-2/">How to build a login-registration system using Symfony &#8211; A PHP Framework &#8211; Part 2</a><br />
</span><span style="font-weight: bold; color: rgb(51, 102, 255); text-decoration: underline;"><br />
</span><span style="color: rgb(0, 0, 0);">If you are well equipped with the basics, read on</span><span style="font-weight: bold; color: rgb(51, 102, 255); text-decoration: underline;"></p>
<p>Select Query:<br />
</span><span style="color: rgb(0, 0, 0);">Lets start with the few basic example queries.<br />
</span>
<ol>
<li><span style="color: rgb(0, 0, 0); text-decoration: underline;">Extract list of all users from the users table</span></p>
<p style="border: 1px solid rgb(221, 221, 221); padding: 5px; background: rgb(238, 238, 238) none repeat scroll 0% 50%; -moz-background-clip: -moz-initial; -moz-background-origin: -moz-initial; -moz-background-inline-policy: -moz-initial;">$c = new Criteria();<br />
$this-&gt;resultSet = UsersPeer::doSelect($c);
</p>
<p>Now lets see how can we use this $resultset within the model layer and in the template files.<br />
To use the resultset within model layer, we can do something like this:</p>
<p style="border: 1px solid rgb(221, 221, 221); padding: 5px; background: rgb(238, 238, 238) none repeat scroll 0% 50%; -moz-background-clip: -moz-initial; -moz-background-origin: -moz-initial; -moz-background-inline-policy: -moz-initial;">foreach($this-&gt;resultset as $rs) {<br />
&nbsp;&nbsp;&nbsp; $uid = $ps-&gt;getId();<br />
&nbsp;&nbsp;&nbsp; $fname = $ps-&gt;getFirstName();<br />
&nbsp;&nbsp;&nbsp; $lname = $ps-&gt;getLastName();<br />
}
</p>
<p>To use the resultset in template files (viewer layer), we can do something like this:</p>
<p style="border: 1px solid rgb(221, 221, 221); padding: 5px; background: rgb(238, 238, 238) none repeat scroll 0% 50%; -moz-background-clip: -moz-initial; -moz-background-origin: -moz-initial; -moz-background-inline-policy: -moz-initial;">foreach($resultset as $rs) {<br />
&nbsp;&nbsp;&nbsp; $uid = $ps-&gt;getId();<br />
&nbsp;&nbsp;&nbsp; $fname = $ps-&gt;getFirstName();<br />
&nbsp;&nbsp;&nbsp; $lname = $ps-&gt;getLastName();<br />
}
</p>
</li>
<li><span style="text-decoration: underline;">Extract list of first 10 users only</span>
<p style="border: 1px solid rgb(221, 221, 221); padding: 5px; background: rgb(238, 238, 238) none repeat scroll 0% 50%; -moz-background-clip: -moz-initial; -moz-background-origin: -moz-initial; -moz-background-inline-policy: -moz-initial;">$c = new Criteria();<br />
$c-&gt;setLimit(10);<br />
$this-&gt;resultSet = UsersPeer::doSelect($c);
</p>
</li>
<li><span style="text-decoration: underline;">Extract list of 10 users starting from 100th user</span>
<p style="border: 1px solid rgb(221, 221, 221); padding: 5px; background: rgb(238, 238, 238) none repeat scroll 0% 50%; -moz-background-clip: -moz-initial; -moz-background-origin: -moz-initial; -moz-background-inline-policy: -moz-initial;">$c = new Criteria();<br />
$c-&gt;setOffset(100);<br />
$c-&gt;setLimit(10);<br />
$this-&gt;resultSet = UsersPeer::doSelect($c);
</p>
</li>
<li><span style="text-decoration: underline;">Extract user data where username = &#8216;imoracle&#8217;</span>
<p style="border: 1px solid rgb(221, 221, 221); padding: 5px; background: rgb(238, 238, 238) none repeat scroll 0% 50%; -moz-background-clip: -moz-initial; -moz-background-origin: -moz-initial; -moz-background-inline-policy: -moz-initial;">$c = new Criteria();<br />
$c-&gt;add(UsersPeer::USERNAME,&#8221;imoracle&#8221;);&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; // Note USERNAME is all in caps, even if your column name is in small case<br />
$this-&gt;resultSet = UsersPeer::doSelect($c);
</p>
</li>
<li><span style="text-decoration: underline;">Another approach to do a select query</span>
<p style="border: 1px solid rgb(221, 221, 221); padding: 5px; background: rgb(238, 238, 238) none repeat scroll 0% 50%; -moz-background-clip: -moz-initial; -moz-background-origin: -moz-initial; -moz-background-inline-policy: -moz-initial;">$c = new Criteria();<br />
$c-&gt;add(UsersPeer::USERNAME,&#8221;imoracle&#8221;);<br />
$resultSet = UsersPeer::doSelectRS($c);
</p>
<p>To access this resultset in the model layer itself, we can do something like this:</p>
<p style="border: 1px solid rgb(221, 221, 221); padding: 5px; background: rgb(238, 238, 238) none repeat scroll 0% 50%; -moz-background-clip: -moz-initial; -moz-background-origin: -moz-initial; -moz-background-inline-policy: -moz-initial;">while($resultSet-&gt;next()) {<br />
&nbsp;&nbsp;&nbsp; $uid = $resultSet-&gt;get(1);<br />
&nbsp;&nbsp;&nbsp; $fname = $resultSet-&gt;get(2);<br />
&nbsp;&nbsp;&nbsp; $lname = $resultSet-&gt;get(3);<br />
}
</p>
<p>Where get(1) fetches the 1st column for you of the resultset. Hence $resultSet-&gt;get(n) will fetch you the nth column of the resultSet.</li>
</ol>
<p><span style="font-weight: bold; color: rgb(51, 102, 255); text-decoration: underline;">Insert Query:<br />
</span><span style="color: rgb(0, 0, 0);">Lets see a few quick examples for inserting a new row in a table.</span><span style="font-weight: bold; color: rgb(51, 102, 255); text-decoration: underline;"><br />
</span>
<ol>
<li><span style="text-decoration: underline;">Insert a user entry in user table</span></p>
<p style="border: 1px solid rgb(221, 221, 221); padding: 5px; background: rgb(238, 238, 238) none repeat scroll 0% 50%; -moz-background-clip: -moz-initial; -moz-background-origin: -moz-initial; -moz-background-inline-policy: -moz-initial;">$new_user = new Users();<br />
$new_user-&gt;setUserName($uname);<br />
$new_user-&gt;setFirstName($fname);<br />
$new_user-&gt;setLastName($lname);<br />
$new_user-&gt;setPassword(md5($pass));</p>
<p>$new_user-&gt;save();<br />
$new_user_id = $new_user-&gt;getId();
</p>
<p>Finally we get the last inserted user row id.
</li>
</ol>
<p><span style="font-weight: bold; color: rgb(51, 102, 255); text-decoration: underline;">Update Query:<br />
</span><span style="color: rgb(0, 0, 0);">Symfony provides nothing special for the update queries. The same above format works for updating a row as well. Symfony is smart enough to analyze whether its a select or update query.<br />
</span>
<ol>
<li><span style="text-decoration: underline;">First Method for Update Query (Recommended)</span></p>
<p style="border: 1px solid rgb(221, 221, 221); padding: 5px; background: rgb(238, 238, 238) none repeat scroll 0% 50%; -moz-background-clip: -moz-initial; -moz-background-origin: -moz-initial; -moz-background-inline-policy: -moz-initial;">$c = new Criteria();<br />
$c-&gt;add(UsersPeer::USERNAME,&#8221;imoracle&#8221;);<br />
$c-&gt;add(UsersPeer::PASSWORD,$newpass);<br />
$res = UsersPeer::doUpdate($c);
</p>
<p>In the above example we chose to update the password for user with username &#8220;imoracle&#8221;.</li>
<li><span style="text-decoration: underline;">Another Method for Update Query</span>
<p style="border: 1px solid rgb(221, 221, 221); padding: 5px; background: rgb(238, 238, 238) none repeat scroll 0% 50%; -moz-background-clip: -moz-initial; -moz-background-origin: -moz-initial; -moz-background-inline-policy: -moz-initial;">$user = new User();<br />
$user-&gt;setNew(false);<br />
$user-&gt;setId($uid);<br />
$user-&gt;setPassword($newpass);<br />
$user-&gt;save();
</p>
<p>Important code in above 5 lines is the 2nd line. Without this line it will add a new row and not update. Also another drawback which I found in this approach is that in third line you need to give the PK (primary key) of the table. Like our 1st method you cannot give username in the third line.</p>
<p>Finally I haven&#8217;t tried and tested this method enough. So I will not recommend this.
</li>
</ol>
<p><span style="font-weight: bold; color: rgb(51, 102, 255); text-decoration: underline;">Delete Query:</span><br />
Its basically the same above methods, with change of final call.</p>
<ol>
<li><span style="text-decoration: underline;">Delete a user&#8217;s account where username = &#8216;imoracle&#8217;</span></p>
<p style="border: 1px solid rgb(221, 221, 221); padding: 5px; background: rgb(238, 238, 238) none repeat scroll 0% 50%; -moz-background-clip: -moz-initial; -moz-background-origin: -moz-initial; -moz-background-inline-policy: -moz-initial;">$c = new Criteria();<br />
$c-&gt;add(UsersPeer::USERNAME,&#8217;imoracle&#8217;);<br />
$res = UsersPeer::doDelete($c);
</p>
</li>
</ol>
<p><span style="font-weight: bold; color: rgb(51, 102, 255); text-decoration: underline;">How to alter a table in symfony?<br />
</span>
<ol>
<li><span style="text-decoration: underline;">Method 1 (Non-standard)</span><br />
A drawback which I have seen in symfony till now is that, when you try to alter a table by adding new columns in your schema.yml, It flushes the data from the table and gives you an altered table with no data. This is really bad, if you don&#8217;t know this behavior and move ahead altering the table. Though Symfony provider propel-dump-data to handle alter tables, but it is still quite buggy and slow for big databases. The clean and smooth way to achieve this is:</p>
<div style="border: 1px solid rgb(221, 221, 221); padding: 5px; background: rgb(238, 238, 238) none repeat scroll 0% 50%; -moz-background-clip: -moz-initial; -moz-background-origin: -moz-initial; -moz-background-inline-policy: -moz-initial;">
<ol>
<li>Take a mysql dump with following options:<br />
mysqldump -h hostname -u username -p password -c -t dbname &gt; dbname.sql<br />
Do not forget to use option -c and -t, without which it can land you in a problem
</li>
<li>Update your schema.yml file, with new columns which you want to add and run<br />
sudo symfony propel-build-all
</li>
<li>Restore the database content using following command:<br />
mysql -h hostname -u username -p password dbname &lt; dbname.sql
</li>
</ol>
</div>
<p>And you have your new altered table without any loss of data.</li>
<li><span style="text-decoration: underline;">Method 2 (Standard)</span><br />
If you are afraid of your data being flushed by the stupid symfony, then you may choose not to touch your database using propel at all. Here is what you can do in such a case.</p>
<div style="border: 1px solid rgb(221, 221, 221); padding: 5px; background: rgb(238, 238, 238) none repeat scroll 0% 50%; -moz-background-clip: -moz-initial; -moz-background-origin: -moz-initial; -moz-background-inline-policy: -moz-initial;">
<ol>
<li>Update your schema.yml file and run<br />
sudo symfony propel-build-model
</li>
<li>Then manually go and alter the table in the database.
</li>
</ol>
</div>
<p>Here propel-build-model will only rebuild the ORM layer to incorporate your schema.yml changes. However propel-build-all not only builds the ORM layer, but also actually creates the table in the database (flushing the data)</li>
</ol>
<p>In future I shall keep adding more SQL queries on this post, as and when I write them using symfony. So keep glued to this one <img src='http://abhinavsingh.com/blog/wp-includes/images/smilies/icon_wink.gif' alt=';)' class='wp-smiley' /> </p>
<script type="text/javascript">var wordpress_toolbar_urls = ["http:\/\/api.tweetmeme.com\/share?url=http%3A%2F%2Fabhinavsingh.com%2Fblog%2F2008%2F11%2Fsymfony-model-layer-tips-and-tricks-a-php-framework-part-3%2F","http:\/\/www.symfony-project.org\/book\/1_0\/08-Inside-the-Model-Layer"];var wordpress_toolbar_url = "http://abhinavsingh.com/blog/wp-content/plugins/wordpress-toolbar/toolbar.php";var wordpress_toolbar_oinw = "oinw";var wordpress_toolbar_hash = "aHR0cDovL2FiaGluYXZzaW5naC5jb20vYmxvZy8yMDA4LzExL3N5bWZvbnktbW9kZWwtbGF5ZXItdGlwcy1hbmQtdHJpY2tzLWEtcGhwLWZyYW1ld29yay1wYXJ0LTMvPHdwdGI%2BU3ltZm9ueSBtb2RlbCBsYXllciB0aXBzIGFuZCB0cmlja3MgJiM4MjExOyBBIFBIUCBGcmFtZXdvcmsgJiM4MjExOyBQYXJ0IDM8d3B0Yj5odHRwOi8vYWJoaW5hdnNpbmdoLmNvbS9ibG9nPHdwdGI%2BQWJoaSYjMDM5O3MgV2VibG9n";</script><ul class="related_post"><li><a href="http://abhinavsingh.com/blog/2008/10/how-to-build-a-login-registration-system-using-symfony-a-php-framework-part-2/" title="How to build a login-registration system using Symfony &#8211; A PHP Framework &#8211; Part 2">How to build a login-registration system using Symfony &#8211; A PHP Framework &#8211; Part 2</a> (37)</li><li><a href="http://abhinavsingh.com/blog/2008/10/getting-started-with-symfony-a-php-framework-part-1/" title="Getting started with Symfony &#8211; A PHP Framework &#8211; Part 1">Getting started with Symfony &#8211; A PHP Framework &#8211; Part 1</a> (33)</li><li><a href="http://abhinavsingh.com/blog/2010/08/php-code-setup-and-demo-of-jaxl-boshchat-application/" title="PHP Code, Setup and Demo of Jaxl boshchat application">PHP Code, Setup and Demo of Jaxl boshchat application</a> (80)</li><li><a href="http://abhinavsingh.com/blog/2010/08/releasing-jaxl-2-0-object-oriented-xmpp-framework-in-php/" title="Releasing Jaxl 2.0 &#8211; Object oriented XMPP framework in PHP">Releasing Jaxl 2.0 &#8211; Object oriented XMPP framework in PHP</a> (6)</li><li><a href="http://abhinavsingh.com/blog/2010/04/setting-nginx-php-fastcgi-and-xcache-on-a-new-ubuntu/" title="Setting Nginx, PHP Fastcgi and XCache on a new Ubuntu">Setting Nginx, PHP Fastcgi and XCache on a new Ubuntu</a> (6)</li></ul>]]></content:encoded>
			<wfw:commentRss>http://abhinavsingh.com/blog/2008/11/symfony-model-layer-tips-and-tricks-a-php-framework-part-3/feed/</wfw:commentRss>
		<slash:comments>7</slash:comments>
		</item>
	</channel>
</rss>

