Retrieve the data from the database.
CakePHP come with the functions to get the data from the database.
find: It is a multifunctional function of all model data-retrieval function.
It is used under the controller.
It is used for fetching the data from the database according to the given condition.
types are
1. first- It is used to get the first matched result.
$lastCreated = $this->Article->find('first', array(
'order' => array('Article.created' => 'desc')
));
2. count- It is used to find the integer value. It count the number of data that satisfy the condition.
$total = $this->Article->find('count');
$pending = $this->Article->find('count', array(
'conditions' => array('Article.status' => 'pending')
));
Like it is used to find the number of pending article.
3. all- It is used to get all the data that satisfy the condition
$pending = $this->Article->find('all', array(
'conditions' => array('Article.status' => 'pending')
));
4. list- It is used to get an indexed array it is helpful where we want a list like we want the list of states or country or city.
$data = $this->State->find('list', array('fields' => array('State.name'),'order' => array('State.name' => 'ASC')));
5. threaded- It return a nested array and is used where want to give the parent id field of the model data to build nested result.
$comments = $this->Comment->find('threaded', array(
'conditions' => array('article_id' => 50)
));
6 neighbor- It is used to get the data which is before and after the requested one.
$neighbors = $this->Article->find(
'neighbors',
array('field' => 'id', 'value' => 3)
);
Set the data
We have to set the retrieved data to our view.
We can do this by using controller.
Like for the above example we can set the data for state
$this->set('states', $data);
where state is the passed variable which will be used in ctp.
0 Comment(s)