Recursive in CakePHP

16
Recursive in CakePHP

description

What is Recursive in CakePHP?

Transcript of Recursive in CakePHP

Page 1: Recursive in CakePHP

Recursive

in

CakePHP

Page 2: Recursive in CakePHP

Using this recursive property, Cake will know about the depth of the result that needs to be generated when find() and read() methods are used.

It is used to set the depth of retrieval of records associated with a model data. So we can limit how much data needs to be fetched from the query in case of multi levels of associations between your models.

What is Recursive?

Page 3: Recursive in CakePHP

To understand the concept of the Recursive in CakePHP, let’s consider one scenario where there is one controller called “AuthorsController”.

We wants to display the list of all Authors. For that we have to write find query in AuthorsController.php file’s index() function.

A Simple Example

Page 4: Recursive in CakePHP

public function index(){ $authors=$this->Author->find('all'); print_r($authors);}

The above query will display the list of all Authors.

Example (cont…)

Page 5: Recursive in CakePHP

Lets say, there is an Association between Author model and Book model.

For Example,An Author has Many Books and a Book belongs to an Author.

Example (cont…)

Page 6: Recursive in CakePHP

Author Model File

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

Book Model File

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

Example (cont…)

Page 7: Recursive in CakePHP

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 above will display the list of all Authors as well as their respective books also.

Example (cont…)

Page 8: Recursive in CakePHP

Now again, we are assuming that there is another Association between Book model and Reader model.

For Example,A Book has Many Readers and a Reader belongs to a Book.

Example (cont…)

Page 9: Recursive in CakePHP

Reader Model File

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

Book Model File

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

Example (cont…)

Page 10: Recursive in CakePHP
Page 11: Recursive in CakePHP

public function index(){ $this->Author->recursive=1; $authors=$this->Author->find('all'); print_r($authors);}

Here, given query will display the list of all Authors as well as their respective books only. If you want to display the readers records particular book wise, then you have to define the value of recursive. If we change the value from ‘1’ to ‘2’ then cake will fetch and find the records up to the second level of the association.

Example (cont…)

Page 12: Recursive in CakePHP
Page 13: Recursive in CakePHP

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 Readers Data

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

Page 14: Recursive in CakePHP

Note:  Default recursive level is 1. It means if there is an association established and

you haven’t added the recursive statement, even though it will fetch the data up to first level.

Lets suppose each author has at least 10 books and you want to query the database to find only the authors, if you didn't specify the recursive statement, even though CakePHP will get all the authors and their books too!! So lets say,

50 authors * 10 books..... you can imagine, this query will return a lots of unnecessary data.

Recursive level -1 is recommended for your application . This will avoid retrieving related data where that is unnecessary or even unwanted. 

Page 15: Recursive in CakePHP

To learn more about CakePHP, start reading our CakePHP Tutorials

Series.CakePHP Tutorials Series

Page 16: Recursive in CakePHP

PHP Dev ZonePublished by : www.php-dev-zone.com @phpdzone