We have moved our forum to GitHub Discussions. For questions about Phalcon v3/v4/v5 you can visit here and for Phalcon v6 here.

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?



5.0k

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']);


25.7k
edited Jan '15

https://docs.phalcon.io/en/latest/reference/models.html#aliasing-relationships

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