Solved thread

This post is marked as solved. If you think the information contained on this thread must be part of the official documentation, please contribute submitting a pull request to its repository.

Multiple relations between same models

Hi all,

after some headhakes i've finally findout how Phalcon names the whole objects referenced by relations: (From PHQL doc). "Scalars are mapped as properties of each “row”, while complete objects are mapped as properties with the name of its related model."

This works well until you have just a relation between two models, but how can i get access to multiple relations objects?

If table A has 2 or more columns with FK to the same table/model, how can i set/get the two ojbects if they share the same model name? Is there a way to setup a relation name during initialize to obtain the relative obj?

Thanks for your help! Gianks

edited Oct '14

This is my admins model

$this->belongsTo('region_id','Mallex\Mvc\Models\Regions','id',array('alias' => 'Region'));
$this->belongsTo('role_id','Mallex\Mvc\Models\Roles','id',array('alias' => 'Role'));
$this->hasMany('id','Mallex\Mvc\Models\Logs','admin_id',array('alias' => 'Logs'));

You can use alias


Thank you very much, you saved my day, and my head! :D

But i suppose my following question will sound natural: where is the reference of this? Which parameters can i pass to a relation to setup it?

I've found Phalcon's documentation very complete and immediate, but IMHO the relations chapter need to be expanded a lot to give more examples and a complete reference of available parameters.

Regards Gianluca

edited Jul '15

Stupid question but, is there a way to set and array with aliases ?

$this->hasMany('genre_id', '\Modules\Engine\Models\Genres', 'id', array(
        'alias' => array('genre', 'genres'),
        'foreignKey' => true

Instead to set 2 same relations with different aliases :

$this->hasMany('genre_id', '\Modules\Engine\Models\Genres', 'id', array(
        'alias' => 'genre',
        'foreignKey' => true

    $this->hasMany('genre_id', '\Modules\Engine\Models\Genres', 'id', array(
        'alias' => 'genres',
        'foreignKey' => true