A function to retrieve data in CakePHP is the findAll() model function.
It has the following syntax:

PHP:

  1. findAll($conditions, $fields, $order, $limit, $page, $recursive);
  2. string $conditions;
  3. array $fields;
  4. string $order;
  5. int $limit;
  6. int $page;
  7. int $recursive;

For example to retrive all the data from a table you will use following:

PHP:

  1. $this->set($var, $this->Modelname->findAll());

With the “set function” the $var value will be send to the view. In the case above all the fields of the table will be retrieve.

Examples using findAll()

To sort the data by a certain field we can use:

PHP:

  1. $this->set($var, $this->Modelname->findAll(null, null, ‘Modelname.field’));

For ascending:

PHP:

  1. $this->set($var, $this->Modelname->findAll(null, null, ‘Modelname.field ASC’));

Descending:

PHP:

  1. $this->set($var, $this->Modelname->findAll(null, null, ‘Modelname.field DESC’));

Only first 10 results:

PHP:

  1. $this->set($var, $this->Modelname->findAll(null, null, ‘Modelname.field DESC’, 10));

The second results page:

PHP:

  1. $this->set($var, $this->Modelname->findAll(null, null, ‘Modelname.field DESC’, 10, 2));

Putting some conditions in the query:

PHP:

  1. $this->set($var, $this->Modelname->findAll(‘Modelname.id> 1’, null, ‘Modelname.field DESC’, 10 ));