public function findLatest($authorId = null, $limit = 3) {
if (!$authorId) {
return array();
}
return $this->find(
‘all’,
array(
‘conditions’ => array(‘Post.author.id => $authorId)
‘order’ => array(‘Post.created => ‘desc’),
‘limit’ => $limit
)
);
}
Then from the post controller you can call:
$lastThreePosts = $this->Post->findLatest($authorId, 3);
Or from the Author model (assuming Author hasMany Post):
$latestPost = $this->Post->findLatest($authorId, 1);
On 24 May 2014, at 04:33, Andrew Barry <jagguy999@gmail.com> wrote:
In cakephp what is the method to filter data on a database request.
Do I filter the data from a model or a controller?
Say if I have a table with 1000's of rows and I only want to select rows on a condition , like a where clause on sql.
The user enters in a certain fields to select rows from a table.
I then pass this variable to the controller and the controller filters this data or is the model supposed to filter the rows selected?
I didnt quite see this clearly from the cakephp docs.
I am not sure if a model or controller is the best way to go about it.a controller would do /// public function view($id = null) { $post = $this->Post->findById($id); $this->set('post', $post); } a model would do something like the below where you set the condition and this seems a really messy way to go about things. class Recipe extends AppModel { public $hasAndBelongsToMany = array( 'Ingredient' => array( 'className' => 'Ingredient', 'joinTable' => 'ingredients_recipes', 'foreignKey' => 'recipe_id', 'associationForeignKey' => 'ingredient_id', 'unique' => true, 'conditions' => '', 'fields' => '', 'order' => '', 'limit' => '', 'offset' => '', 'finderQuery' => '', 'with' => '' ) ); }
--
Like Us on FaceBook https://www.facebook.com/CakePHP
Find us on Twitter http://twitter.com/CakePHP
---
You received this message because you are subscribed to the Google Groups "CakePHP" group.
To unsubscribe from this group and stop receiving emails from it, send an email to cake-php+unsubscribe@googlegroups.com.
To post to this group, send email to cake-php@googlegroups.com.
Visit this group at http://groups.google.com/group/cake-php.
For more options, visit https://groups.google.com/d/optout.
No comments:
Post a Comment