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 this relationship

I have 2 entities, company and school, I am about to add tags for them. So I added a relation model of ItemTags, it has item_id, item_type (this is either company or school),

I wondering how do I add relationship for itemTags? if there is only one entity, I could just go in itemTags,

public function initialize()
    {
        $this->belongsTo('school_id', 'Models\School', 'id');
        $this->belongsTo('tags_id', 'Models\Tags', 'id');
    }

but where do I fit in if I have item_id and item_type?

Hi, Iam not sure what you wont. Please, can you copy-paste all 3 models?



15.2k
edited May '15

Thanks , It is ok, I have changed my database design.

Basically, I was thinking to have 2 entitie tables of company and school, both of them have relations to tags. I want to store the relationship in one table, which is items_tags, and use a field 'type' to tell if it is related to company or school.

What I changed was to have 2 different relation tables, company_tags and school_tags

Hi, Iam not sure what you wont. Please, can you copy-paste all 3 models?