CakePHP - Customer has multiple Country fields. How do I link the Country model to Customer?

StackOverflow https://stackoverflow.com/questions/11546540

  •  21-06-2021
  •  | 
  •  

Frage

The countries table looks like this:

iso | name
--------------------------
AD  | Andorra
AE  | United Arab Emirates
AF  | Afghanistan

...etc

My customers table has the following fields which all store a country code:

id | country_origin | current_country_study | address_country
--------------------------------------------------------
54 | BE             | GB                    | GB

I'm not sure how to link the Country model so that I can retrieve the name of the countries when I am doing a find on the Customer model. If I had one field called country_id I would be okay, but not sure how to do it with multiple fields.

War es hilfreich?

Lösung

Aliases should do the trick:

public $belongsTo = array(
    'CountryOrigin' => array(
        'className'    => 'Country',
        'foreignKey'    => 'country_origin_id'
    ),
    'CurrentCountry' => array(
        'className'    => 'Country',
        'foreignKey'    => 'current_country_id'
    ),
    ....
);

You would need to update your customers table to get the new foreign key fields.

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top