Recursive in CakePHP


Today we are going to learn about 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 needed to set the depth of the retrieval of records associated with a model data so we can limit how much data is fetched from the query in case of multi levels of associations between your models.

Lets say for example we have Author module which has many Books.Now you have such case in which you want to retrive the data of the authors plus their books too.So in this case how Recursive play its role?

We have Model file for Author having the following code.
<?php

class Author extends AppModel
{
   var $name = 'Author';
}
?>

and Model for the Book,

<?php
class Book extends AppModel
{
   var $name = 'Book';
}
?>


Now write the index function in AuthorsController.php file

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

      echo '<pre>';

      print_r($authors);

      $this->set(compact('authors'));

   }


This function will output the authors data only.Now in order to get the data of related books,make some changes in our model and controller files.

Add    var $hasMany = 'Book'; in Author.php model file.As the name indicates author can have many books.

<?php

class Author extends AppModel

{

   var $name = 'Author';

   var $hasMany = 'Book';

}

?>


Now add var $belongsTo ='Author'; in Book.php model file of the Book.

<?php
class Book extends AppModel
{    var $name = 'Book';
     var $belongsTo ='Author';
}
?>


Now add just one line in the controller file of the Author.
$this->Author->recursive=1;


public function index()
 {
    $this->Author->recursive=1;

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

echo '<pre>';

    print_r($authors);

    $this->set(compact('authors'));

}

You will see that now print_r($authors); is showing both records authors and their respective books also.Now say for example books have many readers also.Now here requirement is like,we want the data of Authors, their books and readers of these books. 

So for this simply add the following lines,


Reader.php Model file



<?php

class Reader extends AppModel {

var $name = 'Reader';

var $belongsTo ='Book';

?>


and Model for the Book,

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


Now set the value of $this->Author->recursive = 2 and print the authors array.it will display all the records.

Conclusion


//print only author's data


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

echo '<pre>';

print_r($authors);



//print author's and book's data


$this->Author->recursive=1;

$authors = $this->Author->find('all');
echo '<pre>';
print_r($authors);

//print all the data of authors,books and readers

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




Note: 


Default recursive level is 1.

Lets suppose each author has at least 10 books and you want to query the database to find the authors, if you didn't specify the recursive property, then 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. This happens mostly in find() queries. Use it only when you need it or have to use in  Containable behavior.It can be achieved by simply adding it to the AppModel:
public $recursive = -1;


Comments

Popular posts from this blog

How to construct a B+ tree with example

How to show only month and year fields in android Date-picker?

Visitor Counter Script Using PHP