Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • How to use Recursive In CakePHP?

    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 1.82k
    Comment on it

    Hi Reader's,
    Welcome to FindNerd, today we are going to discuss how to use Recursive In CakePHP?

    Basically in CakePHP by default you can get the data from the model or table that you are querying for and the data of the Models that are linked to the main Model. You can get data using hasmany, belongsto. In simple word we can say that the recursive describe the amount of data which is fetched from the database.
    So,you can get the data of the single association as well as deeper association by using recursive.

    You can fetch the amount of data from database by setting recursive(0,-1,1,2) and It can be higher which is depending on association between Models.

    So,by setting recursive(0,-1,1,2),you are fetching the amount of data from database,it can be more or less depending on how much deep are the association between Models.

    Note:-In CakePHP default recursive level is 1.

    let explain one by one use of Recursive

    (A) -1 It is fetch only Group data, no joins.
    (B) 0 It is fetch Group data and its domain.
    (C) 1 It is fetch a Group data and it's domain and it's associated Users.
    (D) 2 It is fetch a Group, its domain, its associated Users, and the Users’ associated Articles.

    You can see below example

    Let take a Product,Category and Subcategory

    If you want just all product then your query will be like below

    <?php
      $product = $this->Product->find('all');
      print_r($product);
    ?>

    Here Product is a model and fetch the all Product data in database

    Now set recursive = 1 code will like below

    <?php
      $this->Product->recursive = 1;
      $product = $this->Product->find('all');
      print_r($product);
    ?>

    By this query you can get the data of products and it's category.

    Now, set the recursive = 2 the code will like below.

    <?php
      $this->Product->recursive = 2; 
      $product = $this->Product->find('all');
      print_r($product);
    ?>

    By this query you can get the data of products,category and it's associated subcategory.


    let see how to use hasMany and belongsTo

    <?php
      class Product extends AppModel{
        var $name = 'Product';
        var $hasMany = 'Category';
      }
    ?>

    In above example there are two model one is Product and another is Category.
    So, a product has many category and a category belongsTo product.

    Category Model will like below code

    <?php
      class Category extends AppModel { 
        var $name = 'Category';
        var $belongsTo ='Product';
      }
    ?>

    Now again,you can see there is another association between Category Model and Subcategory Model

    Subcategory Model will look like below code

    <?php
      class Subcategory extends AppModel{ 
        var $name = 'Subcategory';
        var $belongsTo ='Category';
      }
    ?>

    Category Model will look like below code

    <?php
      class Category extends AppModel{
        var $name = 'Category';
        var $belongsTo ='Product';
        var $hasMany ='Subcategory';
      }
    ?>
    

     

 0 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: