Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • How to fetch all data from model in cakephp 2.x

    • 0
    • 2
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 271
    Comment on it

    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)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: