Adding bindings on the fly
This recipe shows how to set up new bindings right before a find operation, including bindings that are automatically removed after the operation is executed, and bindings that are permanently added.
Getting ready
We need some sample models and data to work with. Follow the Getting ready section of the recipe, Modifying binding parameters for a find.
How to do it...
If we want to obtain the latest published Article
when we are fetching a User
, we could add a permanent binding to the User
model. However, if we want to do this on a need-by-need basis, it is smarter to add the binding before the find operation that needs it, thus avoiding the unneeded overhead for other operations.
We can add the needed binding and then issue the find
operation:
$this->User->bindModel(array( 'hasOne' => array( 'LastArticle' => array( 'className' => 'Article', 'conditions' => array( 'LastArticle.published' => 1 ), 'order' => array( 'LastArticle.created' => 'desc...