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

Problem with hasMany composite key

Good morning people,

I'm trying to make a relationship with hasMany and it is not working properly, Model: Filial, Related Model: FilialContato

$this->hasMany(['filial_id','ImobiliariaId'], FilialContato::class, ['filial_id','imobiliaria_id'], ['alias' => 'FilialContato']);

I need both relationships to connect through "filial_id" and "ImobiliariaId" In the log, it constructs the query correctly: SELECT .... FROM .... WHERE filial_contato.filial_id = :APR0 AND filial_contato.imobiliaria_id = :APR1{

But the past variables are in trouble, the primary key of FilialContate filial_id works, but ImobiliariaId does not, it returns NULL value, even if it is filled, and this happens with any other column I put there.

{"APR0":"10","APR1":null}

I've tried everything and I can not fix it, my version of Phalcon is 2.0.8

My code for the first post is weird, follow the pictures


HasMany


Schema

edited Jun '17

Okay, now i have weird question, what's the difference between Id and filial_id in filial table?

I feel it should be like:

$this->hasMany(['Id','ImobiliariaId'], FilialContato::class, ['filialid','imobiliaria_id'], ['alias' => 'FilialContato']);

Or decide to using underscore or camelCase, dont use both in database.

You sure that in table it's not null?

This database is very old and has passed through the hands of 15 developers, the new tables normalize to underscore, the old ones are so messed up that it's not worth the effort (there is a legacy application using this database)

This "Id" is a key per client, so there may be several clients with the same ID, but differentiated by "imobiliaria_id".

I did the manual query in sqlyog and it works correctly, and if I only do the relationship for ImobiliariaId it works too, ie it is not the field that is empty, it seems that it can not get the value of" ImobiliariaId "to pass in Query that brings filial_contato