For beginners: famous pitfalls with cake development
Starting from scratch in cakephp is not always easy, especially when you are not used to MVC frameworks.
I hope this will help other beginners to make their life easier ;-)
Incorrect filenames
It happens to me over and over again, writing a filename with or without an "s" at the end, in code and as a filename also. This drives me crazy, because it takes hours and hours to find this mistake, if you only watch the code.Try to stick with the conventions:
Contollers are always plural, e.g. users_controller.php
Models are singular, e.g. user.php
Remember to use "echo" in views
Building a form with the form helper and it isn' there... very anoying. So check first if you use echo on all relevant lines ;-)I try to use plain html instead of php embedded html, so i have to write
<div>
<?php echo $some['array']; ?>
</div>
instead of
<?php
echo '<div>';
echo $some['array'];
echo '</div>';
This helps also in IDEs like Netbeans with codeformating, which will also raise productivity.Correct paths to images
You will experience problems, when you do not use the HtmlHelper for images in your views.The HtmlHelper will always create the right paths to the pictures, no matter where the site in your structur is found. Using a standard html img tag will often lead to a problem in your view, so the images will not be shown.
Links
HtmlHelper: http://book.cakephp.org/view/835/image
Unexpected results in ajax calls
This can be a pain in the back. All logic is fine, but the result is not that what you expected?I know.. happens almost everytime to me, after creating a new method.
So, check your debug level. It can mess up the result.
function foobar() {
configure::write('debug',0);
// somthing happens here
}
This will help a lot and will save time.Nice urls
With thinking about SEO and nice urls, so a lot of people (incl. me in the beginning) try to give controllers and actions a name that will look great in the addressbar.[p]Avoid that until you know what you are doing
Take what the database and the name convention gives you, just make everything work first and do not worry about the nice urls.
In the beginning i spend almost half of my time with thinking about names and stuff, don't waste your time and use the html-helper for links, like you are supposed to do, and later, when you are done coding, routes will do everything for you.
You will be surprised how great it works, because the routing will change all related links automatically, when the links are build with the html-helper
Links
HtmlHelper: http://book.cakephp.org/view/206/Inserting-Well-Formatted-elements#link-836
Routing: http://book.cakephp.org/view/46/Routes-Configuration
Put the query where it belongs
In the begining i always put my query in the controller, because it was an easy way to get what i want and it didn't bother me in any way.After getting used to the cakephp, i found out that it was just stupid, because the structure won't stay clean.
To stay on top of things, put all that stuff in the related model and get the data from there.
The controller will stay lean and thats what you want... just believe it.
If your projects starts to get more complex, you will be thankful that you did it that way ;-)
...and always remember...
No matter what you are about to do, this is still PHP and you CAN do almost everything you want,everywhere.But be careful with that, because getting too far away from the way you are supposed to do things, could make you rework your "lazy" code again, after having some weeks experience with developing in cake.

thank you so much for your great article.
I found this is very useful and let Japanese CakeBaker know about this.
Google's auto translation is something broken, so I made Japanese translation on my blog.
http://bakery.cakephp-users.jp/2011/02/03/for-beginners-famous-pitfalls-with-cake-development/
http://book.cakephp.org/view/72/Additional-Methods-and-Properties
(1) how do you put a query in the model? by defining a function as a method of the model which takes certain crud conditions as a parameter?
(2) in which way would that be any more clear or anything?
i really don't get it. even in the cook book blog example (yeah, kinda lame) they "put the query" in the controller.....
Were you after all just talking about using model associations instead of making manual joins via this->query in the controller?
if so than this hint is very confusing.
would appreciate your answer!!!
pere
exactly. thats the way to keep the controller clean and lean. :)
this is preservation of MVC principles. you just don't want this kind of stuff in the controller.
i figured out that the "Containable"-Behaviour is very helpful to get the data i need, without making my code more complicated.
i hope this will help to answer your questions.
for manual queries, check out:
http://book.cakephp.org/view/456/query
for the "Containable"-Behaviour, check out:
http://book.cakephp.org/view/856/Using-Containable
Good work ! ;-)
* As a further point to the part about filenaming, also be aware of when you should use camelBacking and when_to_join_with_underscores - read the CakePHP coding conventions!
* Database naming - remember, tables associated with models should be pluralised
* AJAX - if you are trying to get beautiful UI transitions within your views and you're using AJAX learn to use basic FireBug with Firefox
* Be aware of whats being passed to the views and what it contains - one of the super quick, super dirty newbie techniques I use is simply to put pr($this) sometimes in the view if I keep getting variable headaches
...of course, there are many more but they are the ones that come to mind first!
And make sure to use 'id' (case-sensitive) and 'name' database fields.
best regards
Simon
Cheers!
this means, that sometimes you might get lazy and put a query like
$this->Modelname->find('all',...'conditions' => array(....))into your controller, but this should be placed into the model.Controller Class:
<?phpclass AppController extends Controller {
if ( $this->RequestHandler->isAjax() ) {
Configure::write ( 'debug', 0 );
}
}
?>
This saved a bit of my sanity.