Recursive in CakePHP

Preview:

DESCRIPTION

What is Recursive in CakePHP?

Citation preview

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?

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

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

The above query will display the list of all Authors.

Example (cont…)

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…)

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…)

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…)

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…)

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…)

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…)

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);

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. 

To learn more about CakePHP, start reading our CakePHP Tutorials

Series.CakePHP Tutorials Series

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