3.7.3.1 find

find($type, $params)

$type is either 'all', 'first', 'count', 'list', 'neighbors' or 'threaded'. 'first' is the default find type.

$params is an array with any of the following available options as keys:

array(
	'conditions' => array('Model.field' => $thisValue), //array of conditions
	'recursive' => 1, //int
	'fields' => array('Model.field1', 'Model.field2'), //array of field names
	'order' => 'Model.created', //string or array defining order
	'group' => array('Model.field'), //fields to GROUP BY
	'limit' => n, //int
	'page' => n //int
)
  1. array(
  2. 'conditions' => array('Model.field' => $thisValue), //array of conditions
  3. 'recursive' => 1, //int
  4. 'fields' => array('Model.field1', 'Model.field2'), //array of field names
  5. 'order' => 'Model.created', //string or array defining order
  6. 'group' => array('Model.field'), //fields to GROUP BY
  7. 'limit' => n, //int
  8. 'page' => n //int
  9. )

If you are using find('list'), the 'fields' key in $params defines the key, value and group

// generated list will be indexed by Post.id, with value of Post.title
$this->Post->find('list', array('fields'=>'Post.title'));
 
// generated list will be indexed by Post.slug, with value of Post.title
$this->Post->find('list', array('fields'=>array('Post.slug', 'Post.title')));
 
// generated list will be grouped by Post.author_id, and each group indexed (aka, optgroup) by Post.id, with value of Post.title
$this->Post->find('list', array('fields'=>array('Post.id', 'Post.title', 'Post.author_id')));
  1. // generated list will be indexed by Post.id, with value of Post.title
  2. $this->Post->find('list', array('fields'=>'Post.title'));
  3. // generated list will be indexed by Post.slug, with value of Post.title
  4. $this->Post->find('list', array('fields'=>array('Post.slug', 'Post.title')));
  5. // generated list will be grouped by Post.author_id, and each group indexed (aka, optgroup) by Post.id, with value of Post.title
  6. $this->Post->find('list', array('fields'=>array('Post.id', 'Post.title', 'Post.author_id')));

If you wish to use an optgroup label from a parent table, be sure to set 'recursive'=>1 in the parameters.

If you are using find('neighbors'), a 'field' key in $params defines the field to analyze, and a 'value' key in the $params array defines the value to look at to determine the next and previous. Note that the 'field' and 'value' keys are not used for find('all') and this is a special case for find('neighbors').

// assuming we have id's from 1-10, we'll see prev set to 1 and next set to 3
$this->Post->id = 2;
$one = $this->Post->find('neighbors');
// To get the neighboring data using a different field..
$two = $this->Post->find('neighbors', array('field'=>'Post.title', 'value'=>$data['Post']['title']));
  1. // assuming we have id's from 1-10, we'll see prev set to 1 and next set to 3
  2. $this->Post->id = 2;
  3. $one = $this->Post->find('neighbors');
  4. // To get the neighboring data using a different field..
  5. $two = $this->Post->find('neighbors', array('field'=>'Post.title', 'value'=>$data['Post']['title']));

For backwards compatibility, find also accepts the previous syntax:

find(string $conditions, array $fields, string $order, int $recursive)