Reputation: 2643
Is there a built-in way with Phalcon to manage Polymorphic Associations - through behaviors or natively - other than manually handling them?
Thanks!
Upvotes: 4
Views: 435
Reputation: 557
you can use my library for doing this. but be careful. this library is in development and the stable release will come as soon as I test it in a real project: https://github.com/IVIR3zaM/PhalconModelMiddleware
Upvotes: 1
Reputation: 13240
Polymorphic associations are commonly misunderstood because there is many ways to use this antipattern.
That said, if I got it right, you want a model that has value which may refer to one kind of model or another, right?!
Theoretically speaking it's possible, so feel free to try it out and tell me if you succeed...
Well, as I said before, there is many ways to achieve this with Phalcon and one that I can imagine for now is the following:
The MainModel
should have two columns of course (ie 'assoc_id', 'assoc_type'). Then you set up in Phalcon one relationship to a special model, in this example called AssocModel
.
AssocModel
has no real table, but has the method getSource
overridden and returns the respective table name based on 'assoc_type'.
If these associable models share a common set o columns you may create an interface IAssociable
or something, so you can keep the definition of your associable models consistent.
However, if you really need the actual model to grab some exclusive data from that kind of association, unfortunately a second model instance will be required to fetch the remaining columns/properties. For this, just like getSource
returns the correct table name, you may create another method to instantiate the correct model class with the respective ID.
Not sure if this help much, but share with me any progress on this so I we can go further on this...
Upvotes: 1