<?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>ScoringCriticals &#187; cakephp</title>
	<atom:link href="http://scoringcriticals.com/tag/cakephp/feed" rel="self" type="application/rss+xml" />
	<link>http://scoringcriticals.com</link>
	<description>A blog about cars, videogames and stuff we like.</description>
	<lastBuildDate>Fri, 18 Jun 2010 17:42:15 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0.1</generator>
		<item>
		<title>Cakephp Relations Bug</title>
		<link>http://scoringcriticals.com/diy/programming/cakephp-relations-bug</link>
		<comments>http://scoringcriticals.com/diy/programming/cakephp-relations-bug#comments</comments>
		<pubDate>Thu, 25 Feb 2010 18:41:39 +0000</pubDate>
		<dc:creator>Derick</dc:creator>
				<category><![CDATA[Programming]]></category>
		<category><![CDATA[cakephp]]></category>
		<category><![CDATA[php]]></category>
		<category><![CDATA[web developer]]></category>
		<category><![CDATA[webapps]]></category>

		<guid isPermaLink="false">http://scoringcriticals.com/?p=340</guid>
		<description><![CDATA[So, you are a PHP programmer and you like Cake framework. There are some things you need to know. Let&#8217;s say you are doing a very very VERY simple blog app, with just two tables in your database. Fig. 1 Posts table Fig. 2 Comments table Then you run the bake script in order to [...]]]></description>
			<content:encoded><![CDATA[
<div class="topsy_widget_data topsy_theme_blue" style="float: right;margin-left: 0.75em; background: url(data:,%7B%20%22url%22%3A%20%22http%253A%252F%252Fscoringcriticals.com%252Fdiy%252Fprogramming%252Fcakephp-relations-bug%22%2C%20%22shorturl%22%3A%20%22http%3A%2F%2Fbit.ly%2F9ZM0AB%22%2C%20%22style%22%3A%20%22small%22%2C%20%22title%22%3A%20%22Cakephp%20Relations%20Bug%22%20%7D);"></div>
<p>So, you are a PHP programmer and you like Cake framework. There are some things you need to know.<span id="more-340"></span></p>
<p>Let&#8217;s say you are doing a very very VERY simple blog app, with just two tables in your database.</p>
<p style="text-align: center;">
<h6 style="text-align: center;"><a href="http://scoringcriticals.com/blog/wp-content/uploads/2010/02/posts.png" rel="lightbox[340]"><img class="aligncenter size-full wp-image-346" src="http://scoringcriticals.com/blog/wp-content/uploads/2010/02/posts.png" alt="posts table" width="535" height="120" /></a>Fig. 1 Posts table</h6>
<h6 style="text-align: center;"><a href="http://scoringcriticals.com/blog/wp-content/uploads/2010/02/comments.png" rel="lightbox[340]"><img class="aligncenter size-full wp-image-345" src="http://scoringcriticals.com/blog/wp-content/uploads/2010/02/comments.png" alt="" width="546" height="127" /></a>Fig. 2 Comments table</h6>
<p>Then you run the bake script in order to create the app. Bake the models first, in this part the Bake script will ask you about the relations of the model and the other tables.<br />
<a href="http://scoringcriticals.com/blog/wp-content/uploads/2010/02/postRelations.png" rel="lightbox[340]"><img class="aligncenter size-full wp-image-348" src="http://scoringcriticals.com/blog/wp-content/uploads/2010/02/postRelations.png" alt="Post Model" width="581" height="121" /></a></p>
<h6 style="text-align: center;">Fig. 3 Post Model relations</h6>
<p><a href="http://scoringcriticals.com/blog/wp-content/uploads/2010/02/commentsRelations.png" rel="lightbox[340]"><img class="aligncenter size-full wp-image-347" src="http://scoringcriticals.com/blog/wp-content/uploads/2010/02/commentsRelations.png" alt="" width="574" height="115" /></a></p>
<h6 style="text-align: center;">Fig. 4 Comment Model relations</h6>
<p>Then you create the controllers of each model, normally you would bake a scaffold controller in order to see that the relations are ok, but as you are a GREAT web developer, you ARE sure that the relations are OK and you bake the controller without scaffold and create the basic class methods (index, edit, view and delete). Then, create the views for each model and soon you are on your way to be a great blogger. You create the first post and then you will try to do a comment on that post just to be sure everything is ok. This is the out come:</p>
<h6 style="text-align: center;"><a href="http://scoringcriticals.com/blog/wp-content/uploads/2010/02/postView.png" rel="lightbox[340]"><img class="aligncenter size-medium wp-image-349" src="http://scoringcriticals.com/blog/wp-content/uploads/2010/02/postView-300x153.png" alt="" width="300" height="153" /></a>Fig. 5 Post View (app/posts/view/1)</h6>
<p>Where are the related comments section? well, maybe you need to create a comment on this post to see it. You go then to add a comment (<span style="color: #0000ff;">app/comments/add</span>). SURPRISE! you don&#8217;t have a drop down input showing all the posts in the system, instead you get a simple input box.</p>
<h6 style="text-align: center;"><a href="http://scoringcriticals.com/blog/wp-content/uploads/2010/02/commentsAdd.png" rel="lightbox[340]"><img class="aligncenter size-medium wp-image-350" src="http://scoringcriticals.com/blog/wp-content/uploads/2010/02/commentsAdd-258x300.png" alt="app/comments/add" width="258" height="300" /></a>Fig. 6 Add Comment View (app/comments/add)</h6>
<p>You run and see the Comments controller to verify this and you noticed that there&#8217;re some lines missing:</p>
<p style="text-align: center;">
<p style="text-align: center;"><em>$posts = $this-&gt;Comment-&gt;Post-&gt;find(&#8216;list&#8217;);</em></p>
<p style="text-align: center;"><em>$this-&gt;set(&#8216;posts&#8217;, $posts);</em></p>
<p style="text-align: center;">
<p style="text-align: center;"><em><br />
</em></p>
<p>The lack of these lines will create the simple input text instead of the drop down input you expected to see. And now you are wondering, what the hell happened with CakePhp? it was so perfect, now the &#8220;relationship&#8221; is broken! (get it? Relationship! As relation between tables? &#8230; Oh please <strong>geek</strong> it up!) Ok, so you found a small bug in Cakephp 1.2.6 and 1.3 beta. How do we fix this?</p>
<h1><strong><span style="text-decoration: underline;">The Workaround</span></strong></h1>
<p>Ok, it took me a lot of time but with a little help from my friends at irc.freenode.net #cakephp. You need to bake the controllers using scaffolding, then bake the views, verify that everything is ok and then bake the controllers again without using scaffolding this will fix all the relation problems. <strong>Please beware NOT to bake the views again so you&#8217;ll end up with the same problem.</strong></p>
<p><strong><br />
</strong></p>
<p>After this workaround you&#8217;ll end up with this:</p>
<h6 style="text-align: center;"><a href="http://scoringcriticals.com/blog/wp-content/uploads/2010/02/commentsAddFixed.png" rel="lightbox[340]"><img class="aligncenter size-medium wp-image-351" src="http://scoringcriticals.com/blog/wp-content/uploads/2010/02/commentsAddFixed-269x300.png" alt="" width="269" height="300" /></a>Fig. 7 Now you have a nice drop down input</h6>
<h6 style="text-align: center;"><a href="http://scoringcriticals.com/blog/wp-content/uploads/2010/02/postsViewFixed.png" rel="lightbox[340]"><img class="aligncenter size-medium wp-image-352" src="http://scoringcriticals.com/blog/wp-content/uploads/2010/02/postsViewFixed-252x300.png" alt="" width="252" height="300" /></a>Fig. 8 Now you have all the relations in order</h6>
<p>So, it maybe look like a silly bug but believe me, if you don&#8217;t know the answer and you have a ton of relations in your app and you are just about to make all the changes without using the Bake script, you will seriously consider using other framework cause it&#8217;s a hell of a job. As of today, this bug is not closed and you will have to wait for a new version to close this up. I still love CakePhp to develop new apps and I understand that there&#8217;s a lot of room to improve, so I hope this post help you understand this bug and maybe the way CakePhp works. Have a great day!</p>
<p>Thanks to:</p>
<p>IRC #cakephp on irc.freenode.net</p>
<p>dehuman @ #cakephp on irc.freenode.net</p>
<p><a href="http://www.cakephp.org" target="_blank">Cakephp</a></p>

]]></content:encoded>
			<wfw:commentRss>http://scoringcriticals.com/diy/programming/cakephp-relations-bug/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
