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 define bidirectional relationship in the same model

I am not able to add the bidirectional relationship in the same model:

$this->belongsTo('userid', 'Frontend\Models\Users', 'id', array(
    'alias' => 'user'
));

$this->addHasMany("Tutor\Models\Organization\Organization","userid",",Frontend\Models\Users","id" , array(
    'alias' => 'organization',
    'foreignKey' => array(
    'message' => 'User cannot be deleted because he/she has activity in the system'
)));


2.7k
edited Sep '14

My requirement is modular structure, I have defined a module and don't want to edit that module models and I need to add only from current module, I saw a abstract addHasMany but I do not know how to use it, I'm waiting for a reply

I added this line in initialize method

$this->addHasMany("Tutor\Models\Organization\Organization","userid",",Frontend\Models\Users","id" , array( 'alias' => 'organization', 'foreignKey' => array( 'message' => 'User cannot be deleted because he/she has activity in the system' )));

but getting error method not defined



33.8k

Ah, if is using models I can't help you.

edited Sep '14

When you say

but getting error method not defined

I'm assuming you're using the theoretically generater getter method?

Have you tried using

$model->getRelated('Namespace\Class');

?