简体   繁体   中英

Cakephp join query Belongs to

hello I have three tables in my database. Country , State ,and City . In state table there is a country_id as foreign key and in city table there is state_id as foreign key . The problem is I want to get the country name when I am querying in the city table. I'll share the code so you can fully understand

Country.php

class Country extends AppModel{


    public $useTable = 'countries';
 }

State.php

class City extends AppModel{


    public $useTable = 'cities';

    public $belongsTo = array(
        'State' => array(
            'className' => 'State',
            'foreignKey' => 'state_id',
            'fields' => array('state.id','state.name')

        )
    );

  }

City

class State extends AppModel{


    public $useTable = 'states';

    public $belongsTo = array(
        'Country' => array(
            'className' => 'Country',
            'foreignKey' => 'country_id',
            'fields' => array('Country.id','Country.name','Country.short_name')

        )
    );

okay here is the code. If I use this query

$this->State->find('all', array(
            'conditions' => array(
                'state.country_id' => $country_id
            ),

I can get the country name from country table. same is the case If I do this in city table like this

$this->City->find('all', array(
                'conditions' => array(
                    'city.state_id' => $state_id
                ),

I can get all the state names city names here. So Now in this same table in one query how I can get the country name as well ?

Try using the recursive property.

Setting $this->City->recursive = 2 gets associated data for the table and associated data on the associated tables.

$this->City->recursive = 2;
$this->City->find('all', array(
            'conditions' => array(
                'city.state_id' => $state_id
            ),
            //other stuff you use in this find
));

You can also using the "joins" flag.

$this->City->find('all', array(
    'conditions' => array(
        'City.state_id' => $state_id
    ),
    'joins' => array(
        array(
            'table' => 'states',
            'alias' => 'State',
            'type' => 'left',
            'conditions' => 'State.id = City.state_id'
        ),
        array(
            'table' => 'countries',
            'alias' => 'Country',
            'type' => 'left',
            'conditions' => 'Country.id = State.country_id'
        )
    ),
    'fields' => array('City.id', 'State.name', 'Country.name', 
         //include all the fields you need
    )
));

See: http://book.cakephp.org/2.0/en/models/model-attributes.html#recursive

In addition to the first answer, make sure to contain the foreign key for the Country .

class City extends AppModel{

    public $belongsTo = array(
        'State' => array(
            'className' => 'State',
            'foreignKey' => 'state_id',
            'fields' => array('State.id','State.name', 'State.country_id'), // Make sure to contain the foreign key for the `Country`.
        )
    );
}

For your information, you can also use ContainableBehavior instead of recursive .

http://book.cakephp.org/2.0/en/core-libraries/behaviors/containable.html

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM