Love Fuel?    Donate

FuelPHP Forums

Ask your question about FuelPHP in the appropriate forum, or help others by answering their questions.
Additional data in pivot table (many to many) ?
  • Hi;

    I have a many_to_many relation working well. 

    But now i want to add more data in pivot table, writable and readable.

    How can I do that? 

    Example:

    table [films_cinemas]

    -film_id (to ID of Films table)
    -cinema_id (to ID of Cinemas table)
    -timetable  

    In this example i want to read and modify the timetable column.

    How can i do that?

    Thanks.


  • HarroHarro
    Accepted Answer
    The standard many-to-many, with a defined through-table, does not support that.

    What you can do is split the relation into two one-to-many relations, by using a model for the through-table. You can use that in parallel to the current many-to-many, if you don't need the data in the through-table, you can still use the current relation.

    This allows you to do:

    $films = Model_Cinema::forge()->where('id', ,'=', 1)->related('viewing', 'viewing.film')->get();

    and get all films in cinema 1, including all time info (assuming that the relation to the pivot table is called 'viewing', and the relation from that table to the films table is called 'film').
  • Note that if you take this route, give the pivot table an id column.

    Don't use the combination of 'film_id' and 'cinema_id' as a compound primary key, the ORM does not support columns that are both primary key and foreign key at the same time, you'll get into a constraint deadlock when you try to delete a cinema or a film.
  • Thanks Harro.

    If I do how you says, I need and intermediate model, right? 

    In this case i will need a Model_Cinemas_Films, right?

  • HarroHarro
    Accepted Answer
    Correct.

    You can name the model anything you like, just use the _table_name property to define the correct table if the Model can not determine it from the model name automatically.

Howdy, Stranger!

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

In this Discussion