affiliate_link

Model Association in Cake PHP

There have four types of association in CakePHP are: hasOne, hasMany, belongsTo, and hasAndBelongsToMany (HABTM)

Has One:
It is one to one relationship. For Ex. A user has one profile.

      class User extends AppModel {
                public $hasOne = array(
                       'Profile' => array(
                                   'className' => 'Profile',
                                   'foreignKey' => 'user_id',
                                    'dependent' => true
                                   )
                                );
                    }

Has Many:
It is one to many relationship. For Ex. A students can have multiple subjects.

   class Student extends AppModel {
               public $hasMany = array(
                      'Subject' => array(
                                'className' => 'Subject',
                                'foreignKey' => 'student_id',
                                'dependent' => true
                                 )
                              );
                    }

Belongs To:
It is many to one relationship. For Ex. Many subjects belong to a student.

    class Subject extends AppModel {
               public $belongsTo = array(
                     'Student' => array(
                           'className' => 'Student',
                           'foreignKey' => 'student_id'
                                   )
                         );
                  }

Has and Belongs To Many:
It is many to many relationship. For Ex. Subjects have, and belong to many chapters.

    class Subject extends AppModel {
                public $hasMany = array(
                             'Chapter' => array(
                                   'className' => 'Chapter',
                                   )
                             );
                public $hasAndBelongsToMany = array(
                          'ChapterName' => array(
                                        'className' => 'ChapterName',
                                         'joinTable' => 'Chapter',
                                         'foreignKey' => 'student_id',
                                         'associationForeignKey' => 'chapter_id',
                                          'fields'=>''
                                           )
                                       );
                                 }

2 Responses to Model Association in Cake PHP

  1. sandy says:

    Good Article on Model association. Keep it up
    I really find it useful.

  2. I definitely did not know that. Learnt some thing new right now! Thanks for that.
    canada canadian canadian goose camp hoody navy http://www.sicanta.com/coloris.aspx?4

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

Author:Lavlesh Mishra