Friday, September 5, 2008

Model associations in Cake...

I just bumped into this little trick in a forum post on model associations...


class Recipe extends AppModel {
var $hasOne = 'User';
}

class User extends AppModel {
var $hasMany = 'Recipe';
}
Associations seem to allow ->finds like these:

$this->User->Recipe->find('all', array('conditions' => array('cooking_time <' => '30 mins.'));


Who knew?! This automatically does a Left Join of User on Recipe. Note to self: Find a code snippet formatter/style for Blogspot! :-(