Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • Associations (Linking Models together) in cakephp

    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 578
    Comment on it

    Association is the relationship between two models. By using associations we can join tables. It is like a glue between different related models. Cakephp provides us very powerful feature of linking models together, just by call of one function. In CakePHP, relations between the database tables are defined through association. Association shows how one model is connected to another model. For this we have to define associations in models, then we can use this wonderful functionalities.


    There are four types of associations in cakephp :

     
    1. hasOne: Has one-to-one relationship between both tables. has One association is used when there is one to one relation.
     Ex:- A user can have only one department.
      

    <?php    
        App::uses('AuthComponent', 'Controller/Component');
    
        class User extends AppModel {
          public $hasOne = array(
                'Department' => array(
                    'className' => 'Department',
                    'conditions' => array('User.id = Department.user_id'),
                    'order' => ''
                )
            );
        }


          


    2. hasMany: Has one-to-many relationship between both tables. This association is used when there is one to many relations.

    Ex:- An author can have more than one books in books table.

       

    <?php
        App::uses('AuthComponent', 'Controller/Component');
        class Author extends AppModel {
          public $hasMany = array(
                'Book' => array(
                    'className' => 'Book',
                )
            );
        }

     


    3. belongsTo: Has many-to-one or one-to-one relationship between both tables. This association is used when there is many to one relation.

    Ex:- Many books can have one author.

      

    <?php
        App::uses('AuthComponent', 'Controller/Component');
        class Book extends AppModel {
        public $belongsTo = array(
            'Author' => array(
                'className' => 'Author',
                'conditions' => array('Book.auth_id = Author.id'),
                'order' => ''
            )
        );

     

     

    4. hasAndBelongsToMany (HABTM):  is many-to-many relationship between both tables. This is used when there is many to many relations.

    Ex:- Recipes can have many ingredients and ingredients can belong to many reciepes.

 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: