Assuming two database tables: Funds and Prices, in which Funds hasMany Prices.
What I wanted to do is to retrieve the latest 15 prices of a particular fund in a certain scenario. Is there a means in CakePHP to make a $this->Fund->find('all') call that would allow me to limit the number of rows to be retrieved from the associated Price table?
Note that I prefer not setting the 'limit' option in the Fund model’s $hasMany variable.
Note on accepted answer [Nov 2]:
In Jason’s answer which I had accepted, I personally opt for the bindModel solution as I felt despite feeling a bit “hack-y”, it bodes much better with me as to make a one-off override on the default Model bindings.
The code I used is as follows:
$this->Fund->bindModel(array(
'hasMany' => array(
'Price' => array(
'limit' => 15,
'order' => 'Price.date DESC'
)
)
);
No unbindModel is necessary. More information could be read from “3.7.6.6 Creating and Destroying Associations on the Fly” in the CakePHP manual.
As I understand the question, you don’t want to set the limit statically in the model. If you wish to use a hasMany association, there really isn’t any other way that I’m aware of other than changing that limit one way or another.
Here are a few ways to dynamically change it:
Example for #2