A function to retrieve data in CakePHP is the findAll() model function.
It has the following syntax:
PHP:
-
findAll($conditions, $fields, $order, $limit, $page, $recursive);
-
string $conditions;
-
array $fields;
-
string $order;
-
int $limit;
-
int $page;
-
int $recursive;
For example to retrive all the data from a table you will use following:
PHP:
-
$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:
-
$this->set($var, $this->Modelname->findAll(null, null, ‘Modelname.field’));
For ascending:
PHP:
-
$this->set($var, $this->Modelname->findAll(null, null, ‘Modelname.field ASC’));
Descending:
PHP:
-
$this->set($var, $this->Modelname->findAll(null, null, ‘Modelname.field DESC’));
Only first 10 results:
PHP:
-
$this->set($var, $this->Modelname->findAll(null, null, ‘Modelname.field DESC’, 10));
The second results page:
PHP:
-
$this->set($var, $this->Modelname->findAll(null, null, ‘Modelname.field DESC’, 10, 2));
Putting some conditions in the query:
PHP:
-
$this->set($var, $this->Modelname->findAll(‘Modelname.id> 1’, null, ‘Modelname.field DESC’, 10 ));
I am trying to retrieve data from 5 tables at one time.How can I do that using one query.