<?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; Propel</title>
	<atom:link href="http://abhinavsingh.com/blog/tag/propel/feed/" rel="self" type="application/rss+xml" />
	<link>http://abhinavsingh.com/blog</link>
	<description>PHP, Memcached, XMPP and Web Development</description>
	<lastBuildDate>Wed, 06 Apr 2011 22:19:11 +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> (5)</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>
		<item>
		<title>How to build a login-registration system using Symfony &#8211; A PHP Framework &#8211; Part 2</title>
		<link>http://abhinavsingh.com/blog/2008/10/how-to-build-a-login-registration-system-using-symfony-a-php-framework-part-2/</link>
		<comments>http://abhinavsingh.com/blog/2008/10/how-to-build-a-login-registration-system-using-symfony-a-php-framework-part-2/#comments</comments>
		<pubDate>Wed, 29 Oct 2008 15:51:01 +0000</pubDate>
		<dc:creator>Abhinav Singh</dc:creator>
				<category><![CDATA[PHP]]></category>
		<category><![CDATA[Framework]]></category>
		<category><![CDATA[MVC]]></category>
		<category><![CDATA[Propel]]></category>
		<category><![CDATA[Symfony]]></category>
		<category><![CDATA[Tutorial]]></category>

		<guid isPermaLink="false">http://abhinavsingh.com/blog/2008/10/how-to-build-a-login-registration-system-using-symfony-a-php-framework-part-2/</guid>
		<description><![CDATA[Hello again, In the last tutorial we saw a very basic implementation which will simply print &#8220;Hello World&#8221; on your browser screen. For those who have landed up here straight here, you may want to go through this blog post. Getting started with Symfony &#8211; A PHP Framework &#8211; Part 1 By now if you [...]]]></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%2F10%2Fhow-to-build-a-login-registration-system-using-symfony-a-php-framework-part-2%2F"><br />
				<img src="http://api.tweetmeme.com/imagebutton.gif?url=http%3A%2F%2Fabhinavsingh.com%2Fblog%2F2008%2F10%2Fhow-to-build-a-login-registration-system-using-symfony-a-php-framework-part-2%2F&amp;source=imoracle&amp;style=normal&amp;service=bit.ly&amp;service_api=R_f027b5a79a20a49b713f16282f1e0857&amp;hashtags=Framework,MVC,PHP,Propel,Symfony,Tutorial&amp;b=2" height="61" width="50" /><br />
			</a>
		</div>
<p>Hello again,</p>
<p>In the last tutorial we saw a very basic implementation which will simply print <span style="font-weight: bold;">&#8220;Hello World&#8221;</span> on your browser screen. For those who have landed up here straight here, you may want to go through this blog post. <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> By now if you have decided to go ahead with symfony and use it for your site development, this is what you will be looking for next.</p>
<p>You can download the code for this tutorial from google code base.</p>
<pre style="border: 1px solid rgb(153, 153, 153); padding: 5px; background: rgb(204, 204, 204) none repeat scroll 0% 50%; width: 500px; -moz-background-clip: -moz-initial; -moz-background-origin: -moz-initial; -moz-background-inline-policy: -moz-initial;">svn checkout http://abhinavsingh.googlecode.com/svn/trunk/PHP/<br/>Frameworks/Symfony/Authentication abhinavsingh-read-only
</pre>
<p><span style="font-weight: bold; text-decoration: underline; color: rgb(51, 51, 255);">A Registration-Login-Logout System for your site<br />
</span><span style="color: rgb(0, 0, 0);">This is mostly the first thing you end up building for your site. To be frank, I myself haven&#8217;t made this application. I will make it as I write this blog, and note down all my steps here, so that I don&#8217;t miss any detail.<br />
</span>
<ol>
<li>We will name our project as <span style="font-weight: bold;">Authentication</span></li>
<li>Create a folder called Authentication under the Symfony directory we made in the last tutorial. For me its here <span style="font-weight: bold;">C:\Workspace\Symfony\Authentication</span></li>
<li>Open your command line and point it to the above created folder</li>
<li>Type &#8220;<span style="font-weight: bold;">symfony init-project Authentication</span>&#8221; and it will create same directory structure that we discussed in last tutotial.</li>
<li>Lets create an application inside this, we will name it as <span style="font-weight: bold;">Auth</span></li>
<li>Type &#8220;<span style="font-weight: bold;">symfony init-app Auth</span>&#8221; and it will create an application named Auth under the apps folder. (<span style="font-weight: bold;">C:\Workspace\Symfony\Authentication\apps\Auth)</span></li>
<li>First we need a module called <span style="font-weight: bold;">Registration</span>, to allow our users to register.</li>
<li>Type &#8220;<span style="font-weight: bold;">symfony init-module Auth Registration</span>&#8221; and it will tell symfony to create a module named Registration inside our application Auth</li>
<li>As told in last tutorial copy the <span style="font-weight: bold;">C:\php5\pear\data\symfony\web\sf folder</span> to <span style="font-weight: bold;">C:\Workspace\Symfony\Authentication\web\sf</span>. At the end of this part, we will learn how can we skip this copying again and again</li>
<li>Open <span style="font-weight: bold;">C:\Workspace\Symfony\Authentication\apps\Auth\modules\Registration\actions\actions.class.php</span> and comment out the forwarding as discussed in last tutorial.</li>
<li>Go to your command line and type &#8220;<span style="font-weight: bold;">symfony clear-cache</span>&#8220;. Read why we do this in the last tutorial.</li>
<li>Open <a href="http://localhost/Symfony/Authentication/web/">http://localhost/Symfony/Authentication/web/</a> in your browser and you must get something like this.</li>
</ol>
<p><img src="http://abhinavsingh.com/library/images/symfony-2-1.gif" title="" alt="" style="border: 1px solid rgb(119, 119, 119); padding: 2px;" /><br />
Figure 1</p>
<p>OOPS! Did we missed something? Yes we did and intentionally. We could have gone to C:\Workspace\Symfony\Authentication\apps\Auth\config\routing.yml and redirected our Authentication application to our Registration Module by default. In that case we would have seen an empty page since indexSuccess.php file inside C:\Workspace\Symfony\Authentication\apps\Auth\modules\Registration\templates is empty. But since we didn&#8217;t modified the routing.yml file, it took us to the default module of symfony.<br />
<!-- ADSENSE LEFT TEXT AD --><br />
So how do we access our Registration module without modifying routing.yml file??<br />
<span style="font-weight: bold;">http://localhost/Symfony/Authentication/web/Registration/index</span> is the answer <img src='http://abhinavsingh.com/blog/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>
<p>As a convention<br />
<span style="font-weight: bold;">http://localhost/Symfony/Authentication/web/&lt;Module&gt;/&lt;ModuleFile&gt;<br />
</span>this is the standard for symfony.</p>
<p>Hence it went to Registration module and then indexSuccess.php page inside that. Cool so we actually just followed up from our last tutorial till here. Lets now do the actual development.</p>
<p><span style="font-weight: bold; text-decoration: underline; color: rgb(51, 51, 255);">Defining a layout for our website<br />
</span><span style="color: rgb(0, 0, 0);">Before we do any backend stuff, lets atleast get our site out of that blank page (</span><span style="font-weight: bold;">http://localhost/Symfony/Authentication/web/Registration/index)</span><span style="color: rgb(0, 0, 0);">. However, If you view the source of that blank page, you will actually find that there are already some header, css, body tags in the source code. How is this coming? when we have an empty <span style="font-style: italic;">indexSuccess.php</span> file.</p>
<p>From our last tutorial we saw the flow which symfony follows to finally display a page on the browser. But then I actually skipped a step to ease down the tutorial. In symfony we follow the <span style="font-style: italic;">Decorator Design Patterns</span> (I hope i m not wrong here) , to get the front end stuff. Essentially Decorator Design Pattern consists of a Layout and a Content page.</p>
<p>In symfony our layout file is located under at <span style="font-weight: bold;">C:\Workspace\Symfony\Authentication\apps\Auth\templates\layout.php. </span>Open it up and you will know the answer to &#8220;How are those meta tags, head tags and body tags coming&#8221;.</p>
<p>Cool enough. Lets go back to our indexSuccess.php file of Registration module and design our website. With a little CSS and HTML, I came up with this design page for our site. Mostly inspired and stolen from my new site <a href="http://gtalkbots.com">Gtalkbots.com</a></p>
<p></span><img src="http://abhinavsingh.com/library/images/symfony-2-2.gif" title="" alt="" style="border: 1px solid rgb(119, 119, 119); padding: 2px;" /><br />
Figure 2</p>
<p>We will shift a bit of code from this page to our layout.php page later on so that we need now write the same modules again and again. Download all the code, from the my google code vault location given at the beginning and end of this tutorial.</p>
<p><span style="font-weight: bold; text-decoration: underline; color: rgb(51, 51, 255);">Designing the backend database:</span><br />
Now we need&nbsp; a databases schema which will save our registered user&#8217;s data.&nbsp; Lets create our database structure.</p>
<p>As talked about in last tutorial, symfony follows ORM programming technique. For this, they provide a method through which you actually need not go to your database and create tables. Symfony will do that for you. Not only this, Symfony will also allow you to skip most of the SQL queries. Lets see how.</p>
<ol>
<li>Open<span style="font-weight: bold;"> C:\Workspace\Symfony\Authentication\config\schema.yml</span> . From extension we know that symfony follows YAML structures which is similar to XML, but a bit easy to write. We will define our table structures in this file.</li>
<li>Type the following in the schema.yml file:
<pre style="border: 1px solid rgb(153, 153, 153); padding: 5px; background: rgb(204, 204, 204) none repeat scroll 0% 50%; width: 600px; -moz-background-clip: -moz-initial; -moz-background-origin: -moz-initial; -moz-background-inline-policy: -moz-initial;">Propel:
  users:
    id:        { type: integer, primaryKey: true, autoIncrement: true }
    username:  varchar(100)
    emailid:   varchar(100)
    password:  varchar(100)
</pre>
</li>
<li>Note the indentation, it is very very important when you are using YAML. Indentation actually decides how the YAML parsers will read and interpret the information you want to write in .yml files. In our case we have a very simple schema.yml file having only one table. We will learn about what that &#8220;<span style="font-weight: bold;">Propel</span>&#8221; is all about later, but lets focus from line 2.</li>
<li>We are trying to make a table called <span style="font-weight: bold;">users </span>having fields like id, username, emailid and password</li>
<li>Now before Symfony makes this table for you, we need to tell symfony the database username and password. For this we have a configuration file called propel.ini, located at <span style="font-weight: bold;">C:\Workspace\Symfony\yahoo\config\propel.ini</span></li>
<li><span style="font-weight: bold;"></span>Open it and change the lines as below:
<pre style="border: 1px solid rgb(153, 153, 153); padding: 5px; background: rgb(204, 204, 204) none repeat scroll 0% 50%; width: 600px; -moz-background-clip: -moz-initial; -moz-background-origin: -moz-initial; -moz-background-inline-policy: -moz-initial;">propel.database.createUrl  = mysql://root@localhost/
propel.database.url        = mysql://root:password@localhost/authentication
</pre>
<p>Now go to your mysql database and create a database named <span style="font-weight: bold;">authentication,</span> which symfony will use from now on.
</li>
<li>Type &#8220;<span style="font-weight: bold;">symfony propel-build-all</span>&#8221; in your command line. This command will happily create a table named &#8220;users&#8221; in youy MySQL database &#8220;authentication&#8221;. Some of the possible errors which I got in the past are:
<ul>
<li>Your PHP must be compiled with XSLT, hence enable it by going in your php.ini file</li>
<li>In schema.yml file never use TABS. The YAML parsers don&#8217;t understand TABS. User SPACES intead of that.</li>
</ul>
</li>
<li>To cross verify check your database, if the table has been created. Now a bit about Propel.</li>
</ol>
<p>Propel is nothing but a third-party-vendor plugin which Symfony use. It helps in parsing the YAML files, creating and executing the whole ORM module including parsing of schema.yml , creating tables, creating ORM data objects. We will slowly and gradually learn a lot more about ORM.</p>
<p><span style="font-weight: bold; text-decoration: underline; color: rgb(51, 51, 255);">A look at the model layer<br />
</span><span style="color: rgb(0, 0, 0);">Another thing which the propel command did was, it created the whole ORM module. If you go to <span style="font-weight: bold;">C:\Workspace\Symfony\Authentication\lib\model</span> you can see 2 folders being created and a few files too.</p>
<p>For every table, propel build 5 files under the model folder. These files are basically the class files, which will provide use with a database object which we can use to select or insert data in the database.</p>
<p>C:\Workspace\Symfony\Authentication\lib\model\om\BaseUsers.php &amp;<br />
</span><span style="color: rgb(0, 0, 0);">C:\Workspace\Symfony\Authentication\lib\model\om\BaseUsersPeer.php are the only 2 files which will be re-created when you run the propel command again.</p>
<p>The file positioned at<br />
C:\Workspace\Symfony\Authentication\lib\model\Users.php &amp;<br />
</span><span style="color: rgb(0, 0, 0);">C:\Workspace\Symfony\Authentication\lib\model\UsersPeer.php will be as it is from this point in time. If you write your own methods in them, they will remain as it is even when you re-create your database schema.</p>
<p>We will use these created ORM files to insert, delete, select data from the databases. In symfony we can actually skip writing a SQL query (though we can always write a direct query too)</p>
<p></span><span style="font-weight: bold; text-decoration: underline; color: rgb(51, 51, 255);">Writing the backend Code:<br />
</span><span style="color: rgb(0, 0, 0);">With UI and database in shape, its time to write our backend code which will handle the registration form submission. If you notice the source code of our form, the action field is set as :</p>
<p>&lt;form id=&#8221;registration&#8221; action=&#8221;<span style="font-weight: bold;">&lt;?php echo url_for(&#8220;Registration/index&#8221;); ?&gt;</span>&#8221; method=&#8221;POST&#8221;&gt;</p>
<p>In symfony we have a lot of helper functions which we can use. In this case we users url_for helper function, which helps in generating a url with &lt;module&gt;/&lt;modulefile&gt; being passed as parameter.</p>
<p>So we have our form being submitted to indexSuccess.php itself, i.e. SELF.<br />
Hence lets go to executeIndex() method in action.class.php and write the code which will handle the submitted form.</p>
<p>Copy the modified action.class.php file from the vault location. I have documented every line inside the code, which will walk you through the code. Understand the flow and continue reading.</p>
<p>However before we insert any data in the database, we need to give the database parameters. Go to<br />
<span style="font-weight: bold;">C:\Workspace\Symfony\Authentication\config\databases.yml<br />
</span>and uncomment the lines of code in there. Finally it should be of the following structure:</span></p>
<pre style="border: 1px solid rgb(153, 153, 153); padding: 5px; background: rgb(204, 204, 204) none repeat scroll 0% 50%; width: 600px; -moz-background-clip: -moz-initial; -moz-background-origin: -moz-initial; -moz-background-inline-policy: -moz-initial;">all:
  propel:
    class:          sfPropelDatabase
    param:
      dsn:          mysql://root:password@localhost/authentication
</pre>
<p>Lastly open:<br />
<span style="font-weight: bold;">C:\Workspace\Symfony\Authentication\apps\Auth\config\settings.yml<br />
</span>and go to the last line of the file. You will find<br />
<span style="font-weight: bold;"># compat_10&nbsp;&nbsp;&nbsp; off<br />
</span>being commented out<br />
Uncomment it and then turn on compat_10 i.e.<br />
<span style="font-weight: bold;">compat_10&nbsp;&nbsp;&nbsp; on</span><br />
<span style="font-weight: bold;"></span><span style="font-weight: bold;"></span><br />
Clear your cache, before submitting the form (<span style="font-weight: bold;">symfony clear-cache</span>) and then try to register.</p>
<p>If you have followed everything, you will be able to successfully able to register and visit mainSuccess.php of the module.</p>
<p>I have documented all the code which you can download from the vault location. Read it, try it, play with it.</p>
<p><span style="font-weight: bold; color: rgb(255, 0, 0);">PS: The best documentation is available at symfony-project website. This tutorial was intended to </span><br style="font-weight: bold; color: rgb(255, 0, 0);" /><span style="font-weight: bold; color: rgb(255, 0, 0);">build a real life scenario application using Symfony. Though I must admit that I still haven&#8217;t used 100% possible symfony </span><br style="font-weight: bold; color: rgb(255, 0, 0);" /><span style="font-weight: bold; color: rgb(255, 0, 0);">for this application, just to make it simple but still give you a feel of how things flow in symfony.</p>
<p></span><span style="color: rgb(255, 0, 0);"><span style="color: rgb(0, 0, 0);">Leave a comment if you have any doubt or difficulty in implementing the code.</span><br style="color: rgb(0, 0, 0);" /><span style="color: rgb(0, 0, 0);">Do leave a comment even if you like the code <img src='http://abhinavsingh.com/blog/wp-includes/images/smilies/icon_wink.gif' alt=';)' class='wp-smiley' /> </span><br />
</span><span style="font-weight: bold; color: rgb(255, 0, 0);"></span></p>
<script type="text/javascript">var wordpress_toolbar_urls = ["http:\/\/api.tweetmeme.com\/share?url=http%3A%2F%2Fabhinavsingh.com%2Fblog%2F2008%2F10%2Fhow-to-build-a-login-registration-system-using-symfony-a-php-framework-part-2%2F","http:\/\/localhost\/Symfony\/Authentication\/web\/","http:\/\/gtalkbots.com"];var wordpress_toolbar_url = "http://abhinavsingh.com/blog/wp-content/plugins/wordpress-toolbar/toolbar.php";var wordpress_toolbar_oinw = "oinw";var wordpress_toolbar_hash = "aHR0cDovL2FiaGluYXZzaW5naC5jb20vYmxvZy8yMDA4LzEwL2hvdy10by1idWlsZC1hLWxvZ2luLXJlZ2lzdHJhdGlvbi1zeXN0ZW0tdXNpbmctc3ltZm9ueS1hLXBocC1mcmFtZXdvcmstcGFydC0yLzx3cHRiPkhvdyB0byBidWlsZCBhIGxvZ2luLXJlZ2lzdHJhdGlvbiBzeXN0ZW0gdXNpbmcgU3ltZm9ueSAmIzgyMTE7IEEgUEhQIEZyYW1ld29yayAmIzgyMTE7IFBhcnQgMjx3cHRiPmh0dHA6Ly9hYmhpbmF2c2luZ2guY29tL2Jsb2c8d3B0Yj5BYmhpJiMwMzk7cyBXZWJsb2c%3D";</script><ul class="related_post"><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/2009/08/building-a-custom-php-framework-with-a-custom-template-caching-engine-using-output-control-functions/" title="Building a Custom PHP Framework with a custom template caching engine using Output Control functions">Building a Custom PHP Framework with a custom template caching engine using Output Control functions</a> (10)</li><li><a href="http://abhinavsingh.com/blog/2008/11/symfony-model-layer-tips-and-tricks-a-php-framework-part-3/" title="Symfony model layer tips and tricks &#8211; A PHP Framework &#8211; Part 3">Symfony model layer tips and tricks &#8211; A PHP Framework &#8211; Part 3</a> (7)</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/2009/07/how-to-add-wordpress-like-add_filter-hooks-in-your-php-framework/" title="How to add wordpress like add_filter hooks in your PHP framework">How to add wordpress like add_filter hooks in your PHP framework</a> (18)</li></ul>]]></content:encoded>
			<wfw:commentRss>http://abhinavsingh.com/blog/2008/10/how-to-build-a-login-registration-system-using-symfony-a-php-framework-part-2/feed/</wfw:commentRss>
		<slash:comments>37</slash:comments>
		</item>
	</channel>
</rss>

