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

Model relation with multi models

Hello {{my_first_post_btw}} :)

My question is about dynamic relation, I will try my best to explain what is that to me and if what I want is possible by default with phalcon.

I have a model (I will call it model A) with records in a similar sctructure like this:

id|model_name|record_id

1| model_B|123

2|model_C|456

3|model_B|789

Can I fetch records from model A doing relations (with HasOne()?) on fly with parameters from that same records?!

I'm using Phalcon 3.3.2

Cheers,

Vit

Hi,

I never tried, but you can :)

public $id;
public $model_name;
public $record_id;

public function afterFetch()
{
    $this->hasOne('record_id', $this->model_name, 'id');
}