Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • Recursive function in cakephp

    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 278
    Comment on it

    Recursive in Cakephp is used to set the depth of result when find() methods are used. It retrieves the records associated with model data.Using recursive we can limit the required data. Lets consider an example of recursive by taking an example of book,author and reader. An author has many books and a Book belongs to an Author so need to define association between book and author in a model.

    class Book extends AppModel { 
    var $name = 'Book'; 
    var $belongsTo ='Author'; 
    } 
    
    class Author extends AppModel { 
    var $name = 'Author';
     var $hasMany = 'Book'; 
    }
    

    Now if i use recursive =1 it will display all authors with their associated books.

    $this->Author->recursive=1; 
     $authors=$this->Author->find('all'); 
    

    Now consider one more model name as reader as book has many readers and readers belong to book

    class Reader extends AppModel {
     var $name = 'Reader'; 
    var $belongsTo ='Book'; 
    }
    

    So the depth is now become 2 if i want to fetch all the authors with their associated books and their readers.

     $this->Author->recursive=2; 
    $authors=$this->Author->find('all'); 
    

    If there is a association and we require only authors result then in that case we need to set recursive=-1.

 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: