Relations with composite keys on Models

Hi guys, I am new at Phalcon and I have a question.

How can I create a model relation with a composite primary key? I have tables in my schema with composite primary keys. The relations BelongsTo and HasMany seems not to have this feature.

I am doing this way:

public function initialize() { $this->belongsTo('idUser', 'Fortec\Models\Form', 'idUser', array ('alias' => 'FormUsers')); $this->belongsTo('idForm', 'Fortec\Models\Form', 'id', array ('alias' => 'Forms')); $this->hasMany('idForm', 'Fortec\Models\BrFormImage', 'idForm', array ('alias' => 'BrFormImages')); $this->hasMany('idUser', 'Fortec\Models\BrFormImage', 'idUser', array ('alias' => 'BrFormImageUsers')); $this->hasMany('idForm', 'Fortec\Models\BrFormCorrection', 'idForm', array ('alias' => 'BrFormCorrections')); $this->hasMany('idUser', 'Fortec\Models\BrFormCorrection', 'idUser', array ('alias' => 'BrFormCorrectionUsers'));
}

Thanks.



82.7k

You can pass an array of fields instead of a single field:

$this->belongsTo(array('field1', 'field2'), 'Model', array('field1', 'field2'));


10.6k

Hi Phalcon,

while trying to use an array as you suggested in a hasMany relation i get a "Not Implemented Exception" while trying to set the related Object via relation alias. The approch you suggested works fine in reading since i can count the elements, but i can't update the value of the relation.

A sample code here: baggages table has a composite PK so array of cols has been used in Person Model relation initialization. `` $Person->Baggages = $passengerBags; //array of Baggages `` This causes an exception with the message "Not Implemented". How can i deal with this? Is this a bug?

Thanks



142
$this->hasMany(array('field1','field2'), 'Model', array('field1','field2'), array(
    'foreignKey' => array(
        'message' => 'this field can\'t be deleted because have relation with another object.'
    )
));

Hi Phalcon, This code is working , but the result is incorrect. The result of this relation:

$this->belongsTo(array('field1', 'field2'), 'Model', array('field1', 'field2'))

Is diferente of this :

$this->belongsTo('field1', 'Model', 'field1'); $this->belongsTo('field2', 'Model', 'field2');

In my case the seconde is what give the right result .