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

Defining relationships with more than one column

Hello,

I am trying to setup a relationship that requires 2 columns to be referenced in my relationship definition.

I have a comments table that is used by many different models throughout the system and within the Comment table I reference master_object_id - defining the ID of the model, and also master_object_class - defining the model that 'owns' this comment.

Ideally i would like to say the following:

$this->hasMany(array('id', 'ClassName'), 'Comment', array('master_object_id', 'master_object_class'));

The first array element is the id of $this model and is mapped to 'master_object_id' of the comments table. Then also the ClassName (which I would like to be able to give the value in the hasMany call if thats possible, and not reference a table column) is mapped to 'master_object_class'.

Is this possible, or if not is there a way to approach this sort of relation using the Phalcon methods?

Read in the docs 'You could also use arrays to define multiple fields in the relationship.' but unclear how to go about this: https://docs.phalcon.io/en/latest/reference/models.html#defining-relationships

Thanks

Ok ..... so found this post which answers most of my question (should of searched harder): https://forum.phalcon.io/discussion/2707/does-hasmany-support-more-than-one-column-

$this->hasMany(array("id", "type"), "RobotsParts", array("robots_id", "robots_type"));

Is it possible to set type in the first array to a value instead of using a table column?

Cheers



2.1k

what you are looking for is scoped model/relationship

it is currently not supported