Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>I would recommend that you check the documentation first.</p> <p>Recursive defines the amount of data that will be fetched from the database, Cakephp by default will get the data of the Model/Table that you're querying for and the data of the Models/Tables that are linked to the main Model/table (hasmany, belongsto, etc.) </p> <p>By setting recursive, you're forcing Cakephp to only fetch a certain amount of data, it can be more or less, depending on how much deep are the association between the models/tables and the number specified in recursive. </p> <p>Setting recursive to -1 will only get the data of the model that you're querying for, setting it higher will ask Cakephp to fetch deeper association.</p> <p>Lets say that in our app we have authors that sell books and they get commented by readers.</p> <p>Author 1 &lt;> * Book 1 &lt;> * Comment</p> <p>If we don't set recursive while fetching the list of authors, Cakephp will get the list of authors their books and comments. </p> <pre><code>$authors = $this-&gt;Author-&gt;find('all'); </code></pre> <p>The problem is that for each list display, Cakephp and the database are dealing with a lot of unnecessary data ! which in return impact the performance of your http &amp; database server. </p> <p>Imagine that the list is shown 10/s and each list shows 20 authors (authors who can have from 1 book to *, lets say 10 books as an average number for this example with 5 comments each) do the math and you will see that the servers are processing a lot of unnecessary data which wont be used in the end. </p> <p>The user want to only see the authors list, so there's no need to fetch all the books and comments unless you're going to process them in the controller or to display them in the views. We can do so by setting recursive to -1.</p> <pre><code>$this-&gt;Author-&gt;recursive = -1; $authors = $this-&gt;Author-&gt;find('all'); </code></pre> <p>You may want to optimize your queries so it fetches only the fields that you're going to use, it will boost the overall performance, but that's another subject.</p> <p>Sometimes you will find yourself wanting to do the reverse of that : lets say that the app update the Auth Session variable whenever the user log-in (update ip, browser info, oauth token, group info etc.) and that the app use all the user relatives info to adapt the user experience, for example if the user belongs to a certain group shows relative info&amp;options to that particular group, if the user has allowed the app to access certain account info of a third party provider (google ?) show services that uses that kind of data - lets say show google+ feed or something - etc. </p> <p>It would be a lot easier to fetch all the relative info of the user once he's logged in and store it in Session, which in return will be used by views to adapt the user experience. One way of doing so would be to fetch the relative data one by one and storing it in Session or simply set recursive to 2 and store the result in Session, it will fetch all the relative data of the user model.</p> <hr> <h2>OLd response</h2> <p>recursive allow you to define the amount of data to get from the database. Lets say that the Author has many publication.</p> <p>if you specify -1 for recursive before getting a certain author from the database like so:</p> <pre><code>$this-&gt;Author-&gt;recursive = -1; $author = $this-&gt;Author-&gt;findByName('Someone'); </code></pre> <p>you would get only the Author information/you will get information only from the the Authors table and none from the related tables like publications.</p> <p>you can see this by yourself by using this code:</p> <pre><code>//only author info $this-&gt;Author-&gt;recursive = -1; $author = $this-&gt;Author-&gt;findByName('Someone'); //display the result debug($author); //get the author and related publications info $this-&gt;Author-&gt;recursise = 1; $authorAndPublications = $this-&gt;Author-&gt;findByName('Someone'); //display result debug($authorAndPublications); exit; </code></pre> <p>The recursive property then specify how much information do you want from your database.</p> <p>where should i use it ?</p> <p>lets suppose each author has at least 10 publications and you want to query the database to find the authors, if you didn't specify the recursive property, Cakephp will get all the authors and their publications too!! so lets say 50 authors * 10 publications..... you get the picture, you are querying for a ton of unnecessary data. </p> <p>it mater a lot if it is a high traffic site since for example at each authors list display you query for 500 unnecessary publications informations(that wont be used) just to display some information of the 50 authors in a list/table. </p> <p>by using recursive = -1; before querying for the authors you ease the strain on the database which result in better reactivity and performance.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload