Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • Linking models in CakePhp

    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 331
    Comment on it

    CakePHP provides one of the most important features. It is the ability of the model to link relational mapping. With the help of association we link between the models in CakePHP.

    We can define relations between different objects in our own application in CakePhp it should be a natural process. For example: in a customer database, a customer may have many products, products have a product list, and product list have items may have many products. By defining the way these relations work allows you to access your data in an good and powerful way.

    With the help  of this section you can show how to plan for, define, and utilize associations between models in CakePHP.

    We know that the data can come from a variety of sources, a relational database is the most common form of storage in web applications.

    Relationship Types:

    Relationship Association Type Example
    one to one hasOne A user has one profile.
    one to many hasMany A user can have multiple products.
    many to one belongsTo Many product belong to a user.
    many to many hasAndBelongsToMany Products have, and belong to, many users.

     

    The class variable can be as complex as a multidimensional array used to define association specifics can but sometimes be as simple as a string, .Associations can be  define by creating a class variable named after the association.

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

    In the above example, the first instance of the word ‘Recipe’ is what is termed an ‘Alias’ is an identifier for the relationship, and can be anything you choose.  Usually, you choose the same name as the class that it references. However, aliases for each model must be unique across the app. For example:

    class User extends AppModel {
        public $hasMany = array(
            'MyProducts' => array(
                'className' => 'Product',
            )
        );
        public $hasAndBelongsToMany = array(
            'MemberOf' => array(
                'className' => 'Group',
            )
        );
    }
    
    class Group extends AppModel {
        public $hasMany = array(
            'MyProducts' => array(
                'className' => 'Product',
            )
        );
        public $hasAndBelongsToMany = array(
            'Member' => array(
                'className' => 'User',
            )
        );
    }

    but the following will not work well in all conditions.

    class User extends AppModel {
        public $hasMany = array(
            'MyProducts' => array(
                'className' => 'Products',
            )
        );
        public $hasAndBelongsToMany = array(
            'Member' => array(
                'className' => 'Group',
            )
        );
    }
    
    class Group extends AppModel {
        public $hasMany = array(
            'MyProducts' => array(
                'className' => 'Products',
            )
        );
        public $hasAndBelongsToMany = array(
            'Member' => array(
                'className' => 'User',
            )
        );
    }

    hasOne

    You have to put this code in model in /app/Model/Product.php 

    class User extends AppModel {
        public $hasOne = array(
            'Profile' => array(
                'className' => 'Profile',
                'conditions' => array('Profile.published' => '1'),
                'dependent' => true
            )
        );
    }

    When the association is done then the array is created like this:

    Array
    (
        [User] => Array
            (
                [id] => 121
                [name] =>Megha
                [created] => 2007-05-01 10:31:01
            )
        [Profile] => Array
            (
                [id] => 12
                [user_id] => 121
                [skill] => html
                [created] => 2007-05-01 10:31:01
            )
    )

     In this way you can link your models.

     

 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: