Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • Use of bindModel and unbindModel function

    • 0
    • 2
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 392
    Comment on it

    bindModel and unbindModel:

    bindModel() function is used to create association between models when needed and unbindModel() function is used to destroy the created association when it is no longer required. Association are used when we need data from more than one model then association comes handy to fetch data from multiple models

    Code to create association using bindModel() function:

    $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',
    										)
    									)
    						)
    		);

    In the above code we are creating association between Country Model, State Model and City Model.

    Code to destroy association using unbindModel() function:

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

    The above code will destroy association between State Model and Country 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: