zen of coding

find(‘first’)… gotcha

Just a simple tip…

Here’s a typical find() example:

$this->Post->find('first', array('condition' => array('id' => 5)));

Why does it return me the post with ID = 1, rather than ID = 5?

Of course, a careful reader spotted the spelling issue in the key:
‘condition’ instead of ‘conditions’.

Moral of the story is that find(‘first’) can be a little tricky and misleading and if you are getting an unexpected result be sure to double-check your implementation.

find(‘first’, ‘blah’);

  • That’s why I have “macro” for this in my editor :)

  • teknoid

    @Nik Chankov

    Or wrap these calls in model-specific methods ;)

  • kendor

    If you want return a result rather 5, you can use as follow

    $this->Post->find(‘first’, array(‘condition’ => array(‘id > 5’)));
    If you have any ask, please ask here :)

  • teknoid

    @kendor

    ‘conditions’ NOT ‘condition’ ;)

  • Yup , many time we forget this :P :P
    Technology News

%d bloggers like this: