Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • CakePHP Associations / Relationship Types Examples

    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 203
    Comment on it

    There are 4 Relationship / Associations types in CakePHP:
    Relationship      Association Type                        Example
    one to one         hasOne                                        A user has one profile.
    one to many      hasMany                                     A user can have multiple post.
    many to one      belongsTo                                   Many post belong to a user.
    many to many   hasAndBelongsToMany              Post have, and belong to many tags.

    CakePHP relationship types examples.

    Eg: hasOne-> A user has one profile

    class User extends AppModel
    {
        public $hasOne = 'Profile';
    }


    Example: hasMany-> A user can have multiple post.

    class User extends AppModel {
        public $hasMany = array(
            'Post' => array(
                'className' => 'Post',
                'conditions' => array('Post.approved' => '1'),
                'order' => 'Recipe.created DESC'
            )
        );
    }

    Example: belongsTo -> Many post belong to a user.
     

    class Post extends AppModel {
        public $belongsTo = array(
            'User' => array(
                'className' => 'User',
            )
        );
    }
    
    

     

 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: