Regarding model defining relationship

Can I define relationship for a field multiple times?

Let's say there are 3 tables: personal_infos, customers, and staffs; 1 row of personal_infos is serving only 1 row of customers or only 1 row of staffs. customers has a field id__personal_infos and staffs also has a field id__personal_infos. In personal_infos model, can I define

$this->hasOne('id', 'customers', 'id__personal_infos');

$this->hasOne('id', 'staffs', 'id__personal_infos');

at the same time?



2.9k

My suggestion is if you know about foreign keys in mysql create relationship using that .. Then using phalcon dev tool run below cmd:

phalcon all-models --relations --fk --force --validations

edited Jan '15

Just use an alias

$this->hasOne('id', 'customers', 'id__personal_infos', ['alias' => 'customer']);
$this->hasOne('id', 'staffs', 'id__personal_infos', ['alias' => 'staff']);


13.6k
edited Jan '15

http://docs.phalconphp.com/en/latest/reference/models.html#aliasing-relationships

I think the alias is to solve the problem when the table names are the same.