Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • Creating association between tables using bindModel method in cakephp

    • 0
    • 1
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 215
    Comment on it

    Sometime we need to create association between models .we need this because ,we want that all associated data came on the first level of recursion.

    In cakephp creation of association is done by using cakephp bindModel() method.

    In below code we are creating association between country model and state model.

    public function bind(){
            $this->Country->bindModel(
            array(
                'hasMany'=>array(
                    'State' =>array(
                        'className' => 'State',
                        'foreignKey' => 'country_id',
                        )
                    )
                )
            );
            
            $data = $this->Country->find("all", array('limit'=>4));
    }

    when we debug above code byusing below method:-

    debug($data);

    It will gives us data of each country which is store in database with it associated state data.

 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: