Recursive in CakePHP

download Recursive in CakePHP

of 16

  • date post

  • Category


  • view

  • download


Embed Size (px)


What is Recursive in CakePHP?

Transcript of Recursive in CakePHP

  • 1. RecursiveinCakePHP

2. What is Recursive?Using this recursive property, Cakewill know about the depth of theresult that needs to be generatedwhen find() and read() methods areused.It is used to set the depth ofretrieval of records associated witha model data. So we can limit howmuch data needs to be fetchedfrom the query in case of multi 3. A Simple ExampleTo understand the concept of theRecursive in CakePHP, lets considerone scenario where there is onecontroller called AuthorsController.We wants to display the list of allAuthors. For that we have to write findquery in AuthorsController.php filesindex() function. 4. Example (cont)public function index(){$authors=$this->Author->find('all');print_r($authors);}The above query will display thelist of all Authors. 5. Example (cont)Lets say, there is an Associationbetween Author model and Bookmodel.For Example,An Author has Many Books and aBook belongs to an Author. 6. Example (cont)Author Model Fileclass Author extends AppModel{var $name = 'Author';var $hasMany = 'Book';}Book Model Fileclass Book extends AppModel{var $name = 'Book';var $belongsTo ='Author';} 7. Example (cont)public function index(){$this->Author->recursive=1;$authors=$this->Author->find('all');print_r($authors);}Notice the line added in red color.Now the same query given abovewill display the list of all Authors aswell as their respective books also. 8. Example (cont)Now again, we are assuming thatthere is another Associationbetween Book model and Readermodel.For Example,A Book has Many Readers and aReader belongs to a Book. 9. Example (cont)Reader Model Fileclass Reader extends AppModel{var $name = 'Reader';var $belongsTo ='Book';}Book Model Fileclass Book extends AppModel{ var $name = 'Book';var $belongsTo ='Author';var $hasMany = 'Reader';} 10. Example (cont)public function index(){$this->Author->recursive=1;$authors=$this->Author->find('all');print_r($authors);}Here, given query will display the list of allAuthors as well as their respective booksonly. If you want to display the readersrecords particular book wise, then youhave to define the value of recursive. Ifwe change the value from 1 to 2 thencake will fetch and find the records up tothe second level of the association. 11. Conclusion// Display only Authors Data$authors = $this->Author->find('all');print_r($authors);// Display Authors and Books Data$this->Author->recursive=1;$authors = $this->Author->find('all');print_r($authors);// Display Authors, Books and ReadersData$this->Author->recursive=2;$authors = $this->Author->find('all'); 12. Note:Default recursive level is 1. It means if there is an association established andyou havent added the recursive statement, eventhough it will fetch the data up to first level. Lets suppose each author has at least 10 booksand you want to query the database to find onlythe authors, if you didn't specifythe recursive statement, even though CakePHP willget all the authors and their books too!! So letssay,50 authors * 10 books..... you canimagine, this query will return a lots of unnecessarydata.for your 13. To learn more about CakePHP, startreading our CakePHP TutorialsSeries. CakePHP Tutorials Series 14. PHP Dev ZonePublished by : @phpdzone