Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • Model Association in Cakephp

    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 322
    Comment on it

    Defining relations between different objects in our application, for instance, a state have many cities, and belongs to a country , is termed as associations. Association establish a way to represent the database table relationship inside Cakephp. It helps in managing these associations easily.

     

    Association types in Cakephp:

    1. One to one - hasOne
    2. One to many - has Many
    3. Many to one - belongsTo
    4. Many to many - hasAndBelongsToMany

     

    Create Models for Country, State and City and paste the following code accordingly.

    class Country extends Model {
    	public $hasMany = array('State');
    }
    class State extends Model {
    	public $belongsTo = array(
    			'Country'
    	);
    	public $hasMany = array(
    			'City'
    	);	
    }
    class City extends Model {
    	public $belongsTo = array(
    			'State'
    	);	
    }
    

     

    Next, create view and paste the follwing code:

    code to fetch list of cities on the basis of state id:

    <?php
    	
    	echo $this->Form->input('Cities', array(
                        'options' => $city,
                        'empty' => '-- Select cities --',
                        "placeholder" => "-- Select cities --",
    					'id' => 'show_cities',
    					'div'=>false,
    					'label'=>false,
                        "name" => "data[User][city_id]"
                    ));
    ?>

    code to fetch list of states on the basis of country id:

    <?php
    	echo $this->Form->input('States', array(
                        'options' => $state,
                        'empty' => '-- Select State --',
                        "placeholder" => "-- Select State --",
    					'onchange' => 'getstates();',
    					'div'=>false,
    					'label'=>false,
    					'id'=>'states',
                        "name" => "data[User][state_id]"
                    ));
    
    ?>
    

     

    Create Controller:

     

    code to call the cities function:

    public function getCities() { 
    
    		$this->layout = null;
    
    		if ($this->request->is('post'))
    		{
    			extract($this->request->data);
    
    			$cities = $this->City->find('list', array('fields' => array('id','name'),'conditions' => array('City.state_id' => $state)));
    			//print_r($cities);die;
    			$this->set('city', $cities);
    			$this->render('/Elements/getCities');
    			
    
    		}		
    	}

    code to call the states function:

    public function getStates() {
    
    		$this->layout = null;
    
    		if ($this->request->is('post'))
    		{
    			extract($this->request->data);
    			$states = $this->State->find('list', array('fields' => array('id','State.name'),'conditions' => array('State.country_id' => $country)));
    			
    			$this->set('state', $states);
    			$this->render('/Elements/getData');
    
    		}		
    	}
    

     

 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: