Love Fuel?    Donate

FuelPHP Forums

Ask your question about FuelPHP in the appropriate forum, or help others by answering their questions.
Many to Many relationships with additional attributes
  • Hi I couldn't find a clear example of what the code is required in the classes when modelling Many-To-Many relationships where the relationship also contains attributes. For example, the 3 tables I have are: User -- User_Role -- Role User_Role is a Many to Many table. I can write the appropriate code in the class files that defines this relationship and fetch as required, so long as User_Role has no additional attributes. As I want to have additional attributes, the documentation says at
    http://docs.fuelphp.com/packages/orm/relations/many_many.html#/many_many 'If you need to save values with the relationship table you don't want to use ManyMany but create a model in between instead which belongs to both sides of what you would otherwise call the ManyMany relationship. It is described under "Usage with nested relationships how you can fetch all this.'
    I have created a class User_Role (as directed in the doc), however what do I need to put inside this class file that defines the relationship to the User and Role tables? What is also needed in the User and Role classes to define their relationship to User_Role ... this is what I am finding unclear in the documentation. Following through to "Usage with nested relationships" (as directed by the doc, to see a further description)
    http://docs.fuelphp.com/packages/orm/relations/intro.html#/usage_nested_rels
    .... there are examples of how to fetch, but no explicit examples about what needs to be configured in the class files. Any assistance is greatly appreciated
    Thanks
  • So, in his case, would he need to create a User_Role model?
  • Yes, on the relations table.
  • They are no longer many-many relations. The relations are: User HasMany User_Roles
    User_Role Belongs_To User
    User_Role Belongs_To Role
    Role HasMany User_Roles

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

In this Discussion