In cakephp it allows a beautiful feature that helps in fetching all the data of the Model and set on the view.
The fetching can be done in cakephp with the find method and the set function is used to set the data on the view.
The find method can be used to get a particular id data but here we are fetching all the data from the Model, so it can be done as :
$this->Post->find('all')
we can implement the same in controller to set the data from the controller to view as :
public function index() {
$this->set('posts', $this->Post->find('all'));
}
here in the index function in controller we have fetched the data from the Model by $this->Post->find('all') and then passed it to the variable posts , the data send to the posts is in the form of the associative array.
Now, to send data to view from controller to view we have used set function as
$this->set('posts', $this->Post->find('all')); ,
Accessing the data of posts array on view can be done using the foreach loop on view.
0 Comment(s)