Why use recursive in cakephp find query


Posted on 06 Nov 15 by Vickey

In cakephp we use model binding or assoceation for binding models for example hasMany, belongsTo,hasOne.When we use cakephp query for a model by default it fetch all assoceated models data. but in some places we do not need the all models data we need the main model's data then we use recursive keyword. for example i have two tables one use 'Users' and second is 'Blogs', one use has many blogs so when i will use query in 'User' model cakephp by default fetch all related data from Blog also but i do not need it then i can force cakephp to fetch only User data by using  recursive=-1 and if i need more related data i will use recursive=0,recursive=1,recursive=2

$authors = $this->User->find('all','recursive'=>-1);
$authors = $this->User->find('all','recursive'=>0);
$authors = $this->User->find('all','recursive'=>1);
$authors = $this->User->find('all','recursive'=>2);


If this post help you reduce time to develop, you can help me to maintain this website :)