SlideShare una empresa de Scribd logo
1 de 16
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'; 
}
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.
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

Más contenido relacionado

Destacado

CakePHP mistakes made
CakePHP mistakes madeCakePHP mistakes made
CakePHP mistakes mademarkstory
 
Top 50 Interview Questions and Answers in CakePHP
Top 50 Interview Questions and Answers in CakePHPTop 50 Interview Questions and Answers in CakePHP
Top 50 Interview Questions and Answers in CakePHPKetan Patel
 
Customize CakePHP bake
Customize CakePHP bakeCustomize CakePHP bake
Customize CakePHP bakeKazuyuki Aoki
 
Road to CakePHP 3.0
Road to CakePHP 3.0Road to CakePHP 3.0
Road to CakePHP 3.0markstory
 
CakePHP - Admin Acl Controlled
CakePHP - Admin Acl ControlledCakePHP - Admin Acl Controlled
CakePHP - Admin Acl ControlledLuís Fred
 
CakePHP Community Keynote 2014
CakePHP Community Keynote 2014CakePHP Community Keynote 2014
CakePHP Community Keynote 2014James Watts
 
PHPUnit with CakePHP and Yii
PHPUnit with CakePHP and YiiPHPUnit with CakePHP and Yii
PHPUnit with CakePHP and Yiimadhavi Ghadge
 
9 Awesome cake php tutorials and resources
9 Awesome cake php tutorials and resources9 Awesome cake php tutorials and resources
9 Awesome cake php tutorials and resourcesiScripts
 
Criando e consumindo Web Services (REST) com o CakePHP
Criando e consumindo Web Services (REST) com o CakePHPCriando e consumindo Web Services (REST) com o CakePHP
Criando e consumindo Web Services (REST) com o CakePHP2km interativa!
 
Tutorial de cakePHP itst
Tutorial de cakePHP itstTutorial de cakePHP itst
Tutorial de cakePHP itstomicx
 
RESTful Web Development with CakePHP
RESTful Web Development with CakePHPRESTful Web Development with CakePHP
RESTful Web Development with CakePHPAndru Weir
 

Destacado (20)

Advanced Querying with CakePHP 3
Advanced Querying with CakePHP 3Advanced Querying with CakePHP 3
Advanced Querying with CakePHP 3
 
Agile database access with CakePHP 3
Agile database access with CakePHP 3Agile database access with CakePHP 3
Agile database access with CakePHP 3
 
CakePHP mistakes made
CakePHP mistakes madeCakePHP mistakes made
CakePHP mistakes made
 
Top 50 Interview Questions and Answers in CakePHP
Top 50 Interview Questions and Answers in CakePHPTop 50 Interview Questions and Answers in CakePHP
Top 50 Interview Questions and Answers in CakePHP
 
Customize CakePHP bake
Customize CakePHP bakeCustomize CakePHP bake
Customize CakePHP bake
 
Road to CakePHP 3.0
Road to CakePHP 3.0Road to CakePHP 3.0
Road to CakePHP 3.0
 
CakePHP - Admin Acl Controlled
CakePHP - Admin Acl ControlledCakePHP - Admin Acl Controlled
CakePHP - Admin Acl Controlled
 
CakePHP Community Keynote 2014
CakePHP Community Keynote 2014CakePHP Community Keynote 2014
CakePHP Community Keynote 2014
 
PHPUnit with CakePHP and Yii
PHPUnit with CakePHP and YiiPHPUnit with CakePHP and Yii
PHPUnit with CakePHP and Yii
 
REST API with CakePHP
REST API with CakePHPREST API with CakePHP
REST API with CakePHP
 
9 Awesome cake php tutorials and resources
9 Awesome cake php tutorials and resources9 Awesome cake php tutorials and resources
9 Awesome cake php tutorials and resources
 
Full-Stack CakePHP Deployment
Full-Stack CakePHP DeploymentFull-Stack CakePHP Deployment
Full-Stack CakePHP Deployment
 
Criando e consumindo Web Services (REST) com o CakePHP
Criando e consumindo Web Services (REST) com o CakePHPCriando e consumindo Web Services (REST) com o CakePHP
Criando e consumindo Web Services (REST) com o CakePHP
 
Tutorial de cakePHP itst
Tutorial de cakePHP itstTutorial de cakePHP itst
Tutorial de cakePHP itst
 
Cakephp 3
Cakephp 3 Cakephp 3
Cakephp 3
 
RESTful Web Development with CakePHP
RESTful Web Development with CakePHPRESTful Web Development with CakePHP
RESTful Web Development with CakePHP
 
CakePHP
CakePHPCakePHP
CakePHP
 
PPT - A slice of cake php
PPT - A slice of cake phpPPT - A slice of cake php
PPT - A slice of cake php
 
Cakephp
CakephpCakephp
Cakephp
 
CakePHP and AJAX
CakePHP and AJAXCakePHP and AJAX
CakePHP and AJAX
 

Similar a Recursive in CakePHP

DBIx::Class introduction - 2010
DBIx::Class introduction - 2010DBIx::Class introduction - 2010
DBIx::Class introduction - 2010leo lapworth
 
cake phptutorial
cake phptutorialcake phptutorial
cake phptutorialice27
 
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...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...Corley S.r.l.
 
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...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...Walter Dal Mut
 
DBIx::Class beginners
DBIx::Class beginnersDBIx::Class beginners
DBIx::Class beginnersleo lapworth
 
PHP FUNCTIONS AND ARRAY.pptx
PHP FUNCTIONS AND ARRAY.pptxPHP FUNCTIONS AND ARRAY.pptx
PHP FUNCTIONS AND ARRAY.pptxShaliniPrabakaran
 
PHP and MySQL with snapshots
 PHP and MySQL with snapshots PHP and MySQL with snapshots
PHP and MySQL with snapshotsrichambra
 
Seu primeiro app Android e iOS com Compose Multiplatform
Seu primeiro app Android e iOS com Compose MultiplatformSeu primeiro app Android e iOS com Compose Multiplatform
Seu primeiro app Android e iOS com Compose MultiplatformNelson Glauber Leal
 
Build REST API clients for AngularJS
Build REST API clients for AngularJSBuild REST API clients for AngularJS
Build REST API clients for AngularJSAlmog Baku
 
Mvc - Model: the great forgotten
Mvc - Model: the great forgottenMvc - Model: the great forgotten
Mvc - Model: the great forgottenDavid Rodenas
 
3-CodeJugalbandi-currying-pfa-healthycodemagazine#mar2015
3-CodeJugalbandi-currying-pfa-healthycodemagazine#mar20153-CodeJugalbandi-currying-pfa-healthycodemagazine#mar2015
3-CodeJugalbandi-currying-pfa-healthycodemagazine#mar2015Dhaval Dalal
 
813 LAB Library book sorting Note that only maincpp can .pdf
813 LAB Library book sorting Note that only maincpp can .pdf813 LAB Library book sorting Note that only maincpp can .pdf
813 LAB Library book sorting Note that only maincpp can .pdfsastaindin
 
Functional Java 8 in everyday life
Functional Java 8 in everyday lifeFunctional Java 8 in everyday life
Functional Java 8 in everyday lifeAndrea Iacono
 
Advanced Perl Techniques
Advanced Perl TechniquesAdvanced Perl Techniques
Advanced Perl TechniquesDave Cross
 

Similar a Recursive in CakePHP (20)

DBIx::Class introduction - 2010
DBIx::Class introduction - 2010DBIx::Class introduction - 2010
DBIx::Class introduction - 2010
 
laravel-53
laravel-53laravel-53
laravel-53
 
RESTful API in Node.pdf
RESTful API in Node.pdfRESTful API in Node.pdf
RESTful API in Node.pdf
 
cake phptutorial
cake phptutorialcake phptutorial
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...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...
 
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...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
DBIx::Class beginnersDBIx::Class beginners
DBIx::Class beginners
 
PHP FUNCTIONS AND ARRAY.pptx
PHP FUNCTIONS AND ARRAY.pptxPHP FUNCTIONS AND ARRAY.pptx
PHP FUNCTIONS AND ARRAY.pptx
 
PHP and MySQL with snapshots
 PHP and MySQL with snapshots PHP and MySQL with snapshots
PHP and MySQL with snapshots
 
Seu primeiro app Android e iOS com Compose Multiplatform
Seu primeiro app Android e iOS com Compose MultiplatformSeu primeiro app Android e iOS com Compose Multiplatform
Seu primeiro app Android e iOS com Compose Multiplatform
 
Licão 13 functions
Licão 13 functionsLicão 13 functions
Licão 13 functions
 
Php
PhpPhp
Php
 
Build REST API clients for AngularJS
Build REST API clients for AngularJSBuild REST API clients for AngularJS
Build REST API clients for AngularJS
 
Mvc - Model: the great forgotten
Mvc - Model: the great forgottenMvc - Model: the great forgotten
Mvc - Model: the great forgotten
 
3-CodeJugalbandi-currying-pfa-healthycodemagazine#mar2015
3-CodeJugalbandi-currying-pfa-healthycodemagazine#mar20153-CodeJugalbandi-currying-pfa-healthycodemagazine#mar2015
3-CodeJugalbandi-currying-pfa-healthycodemagazine#mar2015
 
813 LAB Library book sorting Note that only maincpp can .pdf
813 LAB Library book sorting Note that only maincpp can .pdf813 LAB Library book sorting Note that only maincpp can .pdf
813 LAB Library book sorting Note that only maincpp can .pdf
 
Functional Java 8 in everyday life
Functional Java 8 in everyday lifeFunctional Java 8 in everyday life
Functional Java 8 in everyday life
 
lab4_php
lab4_phplab4_php
lab4_php
 
lab4_php
lab4_phplab4_php
lab4_php
 
Advanced Perl Techniques
Advanced Perl TechniquesAdvanced Perl Techniques
Advanced Perl Techniques
 

Último

08448380779 Call Girls In Civil Lines Women Seeking Men
08448380779 Call Girls In Civil Lines Women Seeking Men08448380779 Call Girls In Civil Lines Women Seeking Men
08448380779 Call Girls In Civil Lines Women Seeking MenDelhi Call girls
 
Kalyanpur ) Call Girls in Lucknow Finest Escorts Service 🍸 8923113531 🎰 Avail...
Kalyanpur ) Call Girls in Lucknow Finest Escorts Service 🍸 8923113531 🎰 Avail...Kalyanpur ) Call Girls in Lucknow Finest Escorts Service 🍸 8923113531 🎰 Avail...
Kalyanpur ) Call Girls in Lucknow Finest Escorts Service 🍸 8923113531 🎰 Avail...gurkirankumar98700
 
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdfThe Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdfEnterprise Knowledge
 
08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking Men08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking MenDelhi Call girls
 
FULL ENJOY 🔝 8264348440 🔝 Call Girls in Diplomatic Enclave | Delhi
FULL ENJOY 🔝 8264348440 🔝 Call Girls in Diplomatic Enclave | DelhiFULL ENJOY 🔝 8264348440 🔝 Call Girls in Diplomatic Enclave | Delhi
FULL ENJOY 🔝 8264348440 🔝 Call Girls in Diplomatic Enclave | Delhisoniya singh
 
How to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerHow to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerThousandEyes
 
🐬 The future of MySQL is Postgres 🐘
🐬  The future of MySQL is Postgres   🐘🐬  The future of MySQL is Postgres   🐘
🐬 The future of MySQL is Postgres 🐘RTylerCroy
 
Maximizing Board Effectiveness 2024 Webinar.pptx
Maximizing Board Effectiveness 2024 Webinar.pptxMaximizing Board Effectiveness 2024 Webinar.pptx
Maximizing Board Effectiveness 2024 Webinar.pptxOnBoard
 
[2024]Digital Global Overview Report 2024 Meltwater.pdf
[2024]Digital Global Overview Report 2024 Meltwater.pdf[2024]Digital Global Overview Report 2024 Meltwater.pdf
[2024]Digital Global Overview Report 2024 Meltwater.pdfhans926745
 
Histor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slideHistor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slidevu2urc
 
Handwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed textsHandwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed textsMaria Levchenko
 
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...Miguel Araújo
 
SQL Database Design For Developers at php[tek] 2024
SQL Database Design For Developers at php[tek] 2024SQL Database Design For Developers at php[tek] 2024
SQL Database Design For Developers at php[tek] 2024Scott Keck-Warren
 
Understanding the Laravel MVC Architecture
Understanding the Laravel MVC ArchitectureUnderstanding the Laravel MVC Architecture
Understanding the Laravel MVC ArchitecturePixlogix Infotech
 
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptxHampshireHUG
 
Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...
Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...
Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...shyamraj55
 
WhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure service
WhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure serviceWhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure service
WhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure servicePooja Nehwal
 
Enhancing Worker Digital Experience: A Hands-on Workshop for Partners
Enhancing Worker Digital Experience: A Hands-on Workshop for PartnersEnhancing Worker Digital Experience: A Hands-on Workshop for Partners
Enhancing Worker Digital Experience: A Hands-on Workshop for PartnersThousandEyes
 
Scaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organizationScaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organizationRadu Cotescu
 
The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024Rafal Los
 

Último (20)

08448380779 Call Girls In Civil Lines Women Seeking Men
08448380779 Call Girls In Civil Lines Women Seeking Men08448380779 Call Girls In Civil Lines Women Seeking Men
08448380779 Call Girls In Civil Lines Women Seeking Men
 
Kalyanpur ) Call Girls in Lucknow Finest Escorts Service 🍸 8923113531 🎰 Avail...
Kalyanpur ) Call Girls in Lucknow Finest Escorts Service 🍸 8923113531 🎰 Avail...Kalyanpur ) Call Girls in Lucknow Finest Escorts Service 🍸 8923113531 🎰 Avail...
Kalyanpur ) Call Girls in Lucknow Finest Escorts Service 🍸 8923113531 🎰 Avail...
 
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdfThe Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
 
08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking Men08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking Men
 
FULL ENJOY 🔝 8264348440 🔝 Call Girls in Diplomatic Enclave | Delhi
FULL ENJOY 🔝 8264348440 🔝 Call Girls in Diplomatic Enclave | DelhiFULL ENJOY 🔝 8264348440 🔝 Call Girls in Diplomatic Enclave | Delhi
FULL ENJOY 🔝 8264348440 🔝 Call Girls in Diplomatic Enclave | Delhi
 
How to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerHow to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected Worker
 
🐬 The future of MySQL is Postgres 🐘
🐬  The future of MySQL is Postgres   🐘🐬  The future of MySQL is Postgres   🐘
🐬 The future of MySQL is Postgres 🐘
 
Maximizing Board Effectiveness 2024 Webinar.pptx
Maximizing Board Effectiveness 2024 Webinar.pptxMaximizing Board Effectiveness 2024 Webinar.pptx
Maximizing Board Effectiveness 2024 Webinar.pptx
 
[2024]Digital Global Overview Report 2024 Meltwater.pdf
[2024]Digital Global Overview Report 2024 Meltwater.pdf[2024]Digital Global Overview Report 2024 Meltwater.pdf
[2024]Digital Global Overview Report 2024 Meltwater.pdf
 
Histor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slideHistor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slide
 
Handwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed textsHandwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed texts
 
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
 
SQL Database Design For Developers at php[tek] 2024
SQL Database Design For Developers at php[tek] 2024SQL Database Design For Developers at php[tek] 2024
SQL Database Design For Developers at php[tek] 2024
 
Understanding the Laravel MVC Architecture
Understanding the Laravel MVC ArchitectureUnderstanding the Laravel MVC Architecture
Understanding the Laravel MVC Architecture
 
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
 
Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...
Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...
Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...
 
WhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure service
WhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure serviceWhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure service
WhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure service
 
Enhancing Worker Digital Experience: A Hands-on Workshop for Partners
Enhancing Worker Digital Experience: A Hands-on Workshop for PartnersEnhancing Worker Digital Experience: A Hands-on Workshop for Partners
Enhancing Worker Digital Experience: A Hands-on Workshop for Partners
 
Scaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organizationScaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organization
 
The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024
 

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