2. What is Recursive?
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
3. A Simple Example
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.
4. Example (cont…)
public function index()
{
$authors=$this->Author->find('all');
print_r($authors);
}
The above query will display the
list of all Authors.
5. 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.
6. 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';
}
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 above
will display the list of all Authors as
well as their respective books also.
8. 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.
9. 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';
}
10.
11. 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.
12.
13. 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');
14. 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.
for your
15. To learn more about CakePHP, start
reading our CakePHP Tutorials
Series. CakePHP Tutorials Series
16. PHP Dev Zone
Published by : www.php-dev-zone.com @phpdzone