Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • Discussion on bindModel and unbindModel function in CakePHP 2.x

    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 1.30k
    Comment on it

    Hi Reader's,
    Welcome to FindNerd,today we are going to discuss bindModel and unbindModel function in CakePHP 2.X.

    If we are making any web application in CakePHP then sometimes we have to use bindModel and unbindModel at a runtime. So CakePHP provides inbuilt function of  bindModel() and unbindModel. This function will bind Model at the runtime and also help to fetch data with association.

    Let explain one by one

    1-bindModel():- This function is used for creating association between models as per our need in our CakePHP  web application.

    In below example we can see there are 3 model Country, State and city and we want to fetch all state data based on country because a country has many state and also we want all city based on a state because a state has many city.

    So our code will like below:

     $this->Country->bindModel(
                            array(
                            'hasMany'=>array(
                                'State' =>array(
                                
                                    'className' => 'State',
                                    'foreignKey'=> 'country_id',
                                                )
                                            )
                                )
                );
                $this->Country->State->bindModel(
                            array(
                            'belongsTo'=>array(
                                'Country' =>array(
                                
                                    'className' => 'Country',
                                    'foreignKey'=> 'country_id',
                                                 )
                                             )
                                )
                );
                $this->Country->State->bindModel(
                            array(
                            'hasMany'=>array(
                                'City' =>array(
                                
                                    'className' => 'City',
                                    'foreignKey'=> 'state_id',
                                                )
                                            )
                                )
                );
    

    2:-UnbindModel()- This function is used for destroying the created association if we have no longer required.
    Suppose we are making a web application in CakePHP and we have many tables in our web application and we have a lot of association between them for fetching data from multiple tables. But Sometimes we don't need that data longer so for destroying association we will use unBind function for unbinding models.

    We can see below example how destroy the association

    $this->Country->unBindModel(array('hasMany'=>array('State')));

    In the above code we will get destroy association between Country Model and Country State Model.

     

 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: