0

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';

tereško
  • 58,060
  • 25
  • 98
  • 150
stacked
  • 13
  • 5

2 Answers2

1

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.

Jason McCreary
  • 71,546
  • 23
  • 135
  • 174
0
$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

Anil kumar
  • 4,107
  • 1
  • 21
  • 36