Love Fuel?    Donate

FuelPHP Forums

Ask your question about FuelPHP in the appropriate forum, or help others by answering their questions.
Saving ManyToMany relationships : Is this correct?
  • hello,

    I have two models: Item, List_Locations.
    When I assign locations to a new Item through the form's multi select, the list_location_id array is sent to the item's create_action method.

    in it, the way I am able to save the selected choices are:

    foreach(Input::post('list_locations') as $location) {
         $item->list_location[] = Model_List_Location::find($location);
    }


    Is this correct?
    It would seem that there should be an easier way to assign the input array of location to the item-> side 
    OR
    at least do a $item->list_location[] = Model_List_Location::find(Input::post('list_locations'));

    Any advice would be great.
    Thanks.

  • HarroHarro
    Accepted Answer
    You could fetch them all in one go using

    Model_List_Location::query()->where('id', 'IN', Input::post('list_locations'))->get();

    assuming that you have validated the input first.
  • Why would this NOT work:

        $item = Model_Item::forge(array(
                  'name' => Input::post('name'),
                  'list_locations' => Model_List_Location::query()->where('id', 'IN', Input::post('list_location_id', array()))->get(),
          ));

    and this does:

        $item = Model_Item::forge(array(
                  'name' => Input::post('name'),
          ));
        $item->list_locations = Model_List_Location::query()->where('id', 'IN', Input::post('list_location_id', array()))->get(),

    Does forge only work on the listed '$_properties'? 'list_locations' is a _many_many property
  • forge() does not support loading relations at the moment. Why? Never implemented.

    If you want this, please create a feature request for it at https://github.com/fuel/orm/issues.
  • Thanks.
    No big deal, just wanted to make sure it wanst something I was doing wrong.

Howdy, Stranger!

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

In this Discussion