So, you are a PHP programmer and you like Cake framework. There are some things you need to know.

Let’s say you are doing a very very VERY simple blog app, with just two tables in your database.

posts tableFig. 1 Posts table
Fig. 2 Comments table

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.
Post Model

Fig. 3 Post Model relations

Fig. 4 Comment Model relations

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:

Fig. 5 Post View (app/posts/view/1)

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 (app/comments/add). SURPRISE! you don’t have a drop down input showing all the posts in the system, instead you get a simple input box.

app/comments/addFig. 6 Add Comment View (app/comments/add)

You run and see the Comments controller to verify this and you noticed that there’re some lines missing:

$posts = $this->Comment->Post->find(‘list’);

$this->set(‘posts’, $posts);


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 “relationship” is broken! (get it? Relationship! As relation between tables? … Oh please geek it up!) Ok, so you found a small bug in Cakephp 1.2.6 and 1.3 beta. How do we fix this?

The Workaround

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. Please beware NOT to bake the views again so you’ll end up with the same problem.


After this workaround you’ll end up with this:

Fig. 7 Now you have a nice drop down input
Fig. 8 Now you have all the relations in order

So, it maybe look like a silly bug but believe me, if you don’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’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’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!

Thanks to:

IRC #cakephp on irc.freenode.net

dehuman @ #cakephp on irc.freenode.net

Cakephp

Share and Enjoy:
  • Add to favorites
  • Print
  • email
  • LinkedIn
  • StumbleUpon
  • Digg
  • Facebook
  • del.icio.us
  • Mixx
  • Google Bookmarks
  • Reddit
  • Technorati