SlideShare a Scribd company logo
Recursive 
in 
CakePHP
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
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.
Example (cont…) 
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'; 
}
Recursive in CakePHP
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.
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');
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
To learn more about CakePHP, start 
reading our CakePHP Tutorials 
Series. CakePHP Tutorials Series
PHP Dev Zone 
Published by : www.php-dev-zone.com @phpdzone

More Related Content

PDF
Dictionary part 1
PDF
Conditionally add keys in JavaScript
PPT
php file uploading
PPTX
SH 2 - SES 3 - MongoDB Aggregation Framework.pptx
PDF
Json perl example
PPTX
Data structure in perl
TXT
Threading
PDF
Class 7b: Files & File I/O
Dictionary part 1
Conditionally add keys in JavaScript
php file uploading
SH 2 - SES 3 - MongoDB Aggregation Framework.pptx
Json perl example
Data structure in perl
Threading
Class 7b: Files & File I/O

Viewers also liked (20)

PDF
Advanced Querying with CakePHP 3
PDF
Agile database access with CakePHP 3
PDF
CakePHP mistakes made
PPT
Top 50 Interview Questions and Answers in CakePHP
PDF
Customize CakePHP bake
PDF
Road to CakePHP 3.0
PPTX
CakePHP - Admin Acl Controlled
PDF
CakePHP Community Keynote 2014
PPTX
PHPUnit with CakePHP and Yii
PDF
REST API with CakePHP
PPT
9 Awesome cake php tutorials and resources
KEY
Full-Stack CakePHP Deployment
PDF
Criando e consumindo Web Services (REST) com o CakePHP
PDF
Tutorial de cakePHP itst
PDF
Cakephp 3
PPTX
RESTful Web Development with CakePHP
PPTX
PPTX
PPT - A slice of cake php
PDF
PDF
CakePHP and AJAX
Advanced Querying with CakePHP 3
Agile database access with CakePHP 3
CakePHP mistakes made
Top 50 Interview Questions and Answers in CakePHP
Customize CakePHP bake
Road to CakePHP 3.0
CakePHP - Admin Acl Controlled
CakePHP Community Keynote 2014
PHPUnit with CakePHP and Yii
REST API with CakePHP
9 Awesome cake php tutorials and resources
Full-Stack CakePHP Deployment
Criando e consumindo Web Services (REST) com o CakePHP
Tutorial de cakePHP itst
Cakephp 3
RESTful Web Development with CakePHP
PPT - A slice of cake php
CakePHP and AJAX
Ad

Similar to Recursive in CakePHP (20)

PDF
DBIx::Class introduction - 2010
PPTX
laravel-53
PDF
RESTful API in Node.pdf
PDF
cake phptutorial
PDF
A single language for backend and frontend from AngularJS to cloud with Clau...
PDF
A single language for backend and frontend from AngularJS to cloud with Clau...
PDF
DBIx::Class beginners
PPTX
PHP FUNCTIONS AND ARRAY.pptx
PPT
PHP and MySQL with snapshots
PPT
xquery.ppt Add more information to your upload
PDF
Seu primeiro app Android e iOS com Compose Multiplatform
PPTX
Licão 13 functions
PDF
Build REST API clients for AngularJS
PDF
Mvc - Model: the great forgotten
PDF
3-CodeJugalbandi-currying-pfa-healthycodemagazine#mar2015
PDF
813 LAB Library book sorting Note that only maincpp can .pdf
PDF
Functional Java 8 in everyday life
PDF
lab4_php
PDF
lab4_php
DBIx::Class introduction - 2010
laravel-53
RESTful API in Node.pdf
cake phptutorial
A single language for backend and frontend from AngularJS to cloud with Clau...
A single language for backend and frontend from AngularJS to cloud with Clau...
DBIx::Class beginners
PHP FUNCTIONS AND ARRAY.pptx
PHP and MySQL with snapshots
xquery.ppt Add more information to your upload
Seu primeiro app Android e iOS com Compose Multiplatform
Licão 13 functions
Build REST API clients for AngularJS
Mvc - Model: the great forgotten
3-CodeJugalbandi-currying-pfa-healthycodemagazine#mar2015
813 LAB Library book sorting Note that only maincpp can .pdf
Functional Java 8 in everyday life
lab4_php
lab4_php
Ad

Recently uploaded (20)

PDF
Spectral efficient network and resource selection model in 5G networks
PDF
Reach Out and Touch Someone: Haptics and Empathic Computing
PPTX
Big Data Technologies - Introduction.pptx
PDF
Diabetes mellitus diagnosis method based random forest with bat algorithm
PDF
Electronic commerce courselecture one. Pdf
PDF
cuic standard and advanced reporting.pdf
PDF
Profit Center Accounting in SAP S/4HANA, S4F28 Col11
PPTX
Spectroscopy.pptx food analysis technology
PPTX
20250228 LYD VKU AI Blended-Learning.pptx
PPTX
Effective Security Operations Center (SOC) A Modern, Strategic, and Threat-In...
PPTX
Programs and apps: productivity, graphics, security and other tools
PPTX
KOM of Painting work and Equipment Insulation REV00 update 25-dec.pptx
PPTX
Digital-Transformation-Roadmap-for-Companies.pptx
PDF
MIND Revenue Release Quarter 2 2025 Press Release
DOCX
The AUB Centre for AI in Media Proposal.docx
PDF
Blue Purple Modern Animated Computer Science Presentation.pdf.pdf
PPTX
Understanding_Digital_Forensics_Presentation.pptx
PDF
Peak of Data & AI Encore- AI for Metadata and Smarter Workflows
PDF
Network Security Unit 5.pdf for BCA BBA.
PDF
Dropbox Q2 2025 Financial Results & Investor Presentation
Spectral efficient network and resource selection model in 5G networks
Reach Out and Touch Someone: Haptics and Empathic Computing
Big Data Technologies - Introduction.pptx
Diabetes mellitus diagnosis method based random forest with bat algorithm
Electronic commerce courselecture one. Pdf
cuic standard and advanced reporting.pdf
Profit Center Accounting in SAP S/4HANA, S4F28 Col11
Spectroscopy.pptx food analysis technology
20250228 LYD VKU AI Blended-Learning.pptx
Effective Security Operations Center (SOC) A Modern, Strategic, and Threat-In...
Programs and apps: productivity, graphics, security and other tools
KOM of Painting work and Equipment Insulation REV00 update 25-dec.pptx
Digital-Transformation-Roadmap-for-Companies.pptx
MIND Revenue Release Quarter 2 2025 Press Release
The AUB Centre for AI in Media Proposal.docx
Blue Purple Modern Animated Computer Science Presentation.pdf.pdf
Understanding_Digital_Forensics_Presentation.pptx
Peak of Data & AI Encore- AI for Metadata and Smarter Workflows
Network Security Unit 5.pdf for BCA BBA.
Dropbox Q2 2025 Financial Results & Investor Presentation

Recursive in CakePHP

  • 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'; }
  • 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.
  • 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