Linking 2 properties to the same table [3.3]


#1

Hello bakers, i’ve a noob question here, how can i link a table
(person).(born_in) => fk(country).(id) and (person).(living_in) => fk(country).(id)

so 1 country can be current living of many people and also birth place of some others. when i select country 1 i would like to see all people that live AND were born there


#2

You can assosiate the same table in multiple alias.
Something like this

    $this->belongsTo('BornCountries', [
        'className' => 'Countries',
        'foreignKey' => 'born_country_id',
        'joinType' => 'INNER'
    ]);
    $this->belongsTo('LivingCountries', [
        'className' => 'Countries',
        'foreignKey' => 'living_country_id',
        'joinType' => 'INNER'
    ]);

And then you would access it like $person->born_country and $person->living_country.
Note that you can’t simply contain Countries, you have to use LivingCountries or BornCountries


#3

Wow pal Thanks! this solved my problem! i was scratching my head to this!