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

How to get foreign table if there are two foreign key to same table?

This is the Model, and I want get both user's data


    class PersonalMessage extends BaseModel
    {

        public $fromUserId;
        public $toUserId;

        public function initialize()
        {
            $this->belongsTo('fromUserId', 'User', 'id', array('foreignKey' => true));
            $this->belongsTo('toUserId', 'User', 'id', array('foreignKey' => true));
        }
    }

and I use this to get user

    {{personalMessage.user.username}}

But I can only get the data which linked to toUserId

How can I get the other user?



33.8k
Accepted
answer
$this->belongsTo('fromUserId', 'User', 'id', array('alias' => 'fromUser'));
$this->belongsTo('toUserId', 'User', 'id', array('alias' => 'toUser'));