Welcome to the Cookbook

loading...

3.7.3.1.1 find('first')

find('first', $params)

'first' is the default find type, and will return one result, you'd use this for any use where you expect only one result. Below are a couple of simple (controller code) examples:

function some_function() {
   ...
   $this->Article->order = null; // resetting if it's set
   $semiRandomArticle = $this->Article->find();
   $this->Article->order = 'Article.created DESC'; // simulating the model having a default order
   $lastCreated = $this->Article->find();
   $alsoLastCreated = $this->Article->find('first', array('order' => array('Article.created DESC')));
   $specificallyThisOne = $this->Article->find('first', array('conditions' => array('Article.id' => 1)));
   ...
}
  1. function some_function() {
  2. ...
  3. $this->Article->order = null; // resetting if it's set
  4. $semiRandomArticle = $this->Article->find();
  5. $this->Article->order = 'Article.created DESC'; // simulating the model having a default order
  6. $lastCreated = $this->Article->find();
  7. $alsoLastCreated = $this->Article->find('first', array('order' => array('Article.created DESC')));
  8. $specificallyThisOne = $this->Article->find('first', array('conditions' => array('Article.id' => 1)));
  9. ...
  10. }

In the first example, no parameters at all are passed to find - therefore no conditions or sort order will be used. The format returned from find('first') call is of the form:

Array
(
    [ModelName] => Array
        (
            [id] => 83
            [field1] => value1
            [field2] => value2
            [field3] => value3
        )

    [AssociatedModelName] => Array
        (
            [id] => 1
            [field1] => value1
            [field2] => value2
            [field3] => value3
        )
)

There are no additional parameters used by find('first').