Here are two quick tips. It’ s something that made me waste some time – but an useful experience.
Why it’s always wise to Read The F*****g Manual
Convention over configuration is a great thing – one of the strong points behind cake’s success. It’s so natural – it almost makes you feel you can code by guessing.. or avoid the documentation. And it’s often true.
Well, the first big no-no I’ve met while following this mantra, is the data retrieval – when the query is not basic.
I lost 1 hour just by assuming that Model->find() with recursive >= 1, or using Containable behaviour, would do a single query, joining the related tables based on the defined models’ relations.
As every cake developer knows, that’s not the default behaviour. Cake automatically joins the directly related tables (belongs to relation), and then performs other queries for related tables, where related_id IN (list of ids from the result of the main query)
This is clearly stated in the book. And it’s obvious if you look at the sql dump (when debug is set to 2 app/config/core).
Realted topics are widely known – see this article in the bakery by Nate Abele and this alternative behavior by Rafael Bandeira
(I had no success in using it in a whole application where I was already using Containable, but I didn’t try a lot and decided to stick with Containable).
So, yes, it’s always better to RTFM.
But..
Why it’s not always enough
Another case: I spent some time tracking down a problem in some complex action. To make a long story short, the issue was with some condition like this:
- <?php
- if ( $this->myModel->saveField('my_field_name', $value) {
-
- //do the needed stuff
-
- }
- ?>
I had this code:
- <?php
- $user = ClassRegistry::init('User');
-
- if($user->saveField('role',$role, false)) {
- // code here
- }
- ?>
I found the problem was, the condition never evaluated to true. Even if the save worked fine.
http://book.cakephp.org/view/75/Saving-Your-Data didn’t clear my doubts, and the function doc tells:
@return boolean
Maybe I missed the part:
@see Model::save()
That explains it better. But, misleaded by the Boolean return type, I didn’t find immediately where the problema was.
Then I checked with debug kit and the debug function almost every other line, and..
- $result = $user->saveField('role',$role, false);
- debug($result); //// Here we are
Well, $result wasn’t “true” in case of success, but –like in Model::save- the function returned the whole array of the saved record.
So. Always read the f…..g manual. But don’t trust every single bit and always double check the code – a framework this big and complex, with his high development speed, may have 2 outdated words in the docs.
If something seems incongruent, chances are you read those 2 out of the other tens of thousend : )