简体   繁体   中英

List posts by category in CakePHP

I'm new to CakePHP. Please help me to write a function to retrieve posts under a particular category for my blog app built using CakePHP.

My table structure:

posts: id, post, body, created, category_id category: id, group

Also I had defined: Inside post model - var $belongsTo = 'Category'; Inside category model - var $hasMany = 'Post';

find() is the generic query method for Models in CakePHP.

An example would be:

$results = $this->Post->find('recursive' => -1, 'conditions' => array('Post.category_id' => 1));
debug($results);

There are many ways to achieve what you want. I encourage you to read the docs or working through the CakePHP Blog Tutorial .

$this->Post->find('all', array('conditions' => array('Post.category_id' => $category_id)));

where $category_id is the id of category that you want to retrieve results from database

hope this helps

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM