My Problem: Linked to my Employees table I’ve got an Address table containing a virtual field called full_name (I guess you can imagine by yourself what it does). I added the Containable Behaviour and this function
function beforeFind() {
$this->contain('Address.full_name');
}
to my Employees model, so that I don’t have to call $this->contain(..) in every single controller action (I need the full_name field in pretty every action). BUT id doesn’t work then if the controller action does just a $this->Employee->find('all') (or read(..). Contrary, it works if
- The controller action uses
$this->paginate();instead $this->Employee->contain('Address.full_name');gets called before the$this->Employee->find('all');call. I can’t imagine the cause for this because after this explicitcontain(..)call,contain(..)gets called again by the Model callback functionbeforeFind(), as adebugproofed which I inserted into the cake/libs/models/behaviours/containable.php:contain() function *cough*.
As far as I recall, a contain() statement only works once, for the query operation immediately following it. Subsequent queries will require their own contain() statement e.g.
I don’t recommend using contain() in beforeFind() as it is intended to modify specific returns. It soon becomes second nature to use it before each query where you will then have fine control of the data returned.
If you have a widespread requirement for a limited return, you can set that up in the associations on the model.