Relationship Table not updated on deletion of related item

edited April 2016 in Bug Reports
Hi,

I have a model A which has a has_many relationship to another model B. In the model B I defined the corresponding belongs_to relationship.

Now in the backend I have the form entry for model A. There I have some checkboxes which are getting populated via a custom method on model B

model A
$fields["fahrzeuge"]["order"] = 30; $fields["fahrzeuge"]["mode"] = "checkbox"; $fields["fahrzeuge"]["model"] = array('' => array('fahrzeuge' => 'get_mission_vehicle_list'));
model B
public function get_mission_vehicle_list() { $this->db->order_by('precedence asc'); $this->db->select('id, name'); $this->db->where(array('published' => 'yes', 'retired' => 'no', "ist_abrollbehaelter" => "no")); $query = $this->db->get('fahrzeuge'); return $query->result_assoc_array('id'); }

On activating a checkbox and saving the stuff everything gets saved correctly in the relationship table.

But now when deactivating a checkbox and saving I would expect the corresponding relation to be deleted.
Is there an error in my code, did I miss something or do I have to implement a hook to get the desired behaviour?

Comments

  • edited 11:54AM
    The issue may be because a checkbox or multi select that doesn't have anything selected will not send anything in the POST so FUEL doesn't know whether it should process it. So the solution is to setup a hidden field with the name of 'exists_{field_name}' (so an "exists_" prefix) with a value of 1.
    //... $fields["exists_fahrzeuge"] = array('type' => 'hidden', 'value' => 1);
  • edited 11:54AM
    My issue is another. Let's say I have 4 of 10 checkboxes ticked. Now I deactivate one of them and click on save. I would expect the unticked relation to be deleted.

    This doesn't happen.

    Before I updated the CMS to the most actual version with the new unique index in the database I had the issue that on saving everytime all the ticked checkboxes where added to the relationship table, so that at the end I had multiple duplicates. But this issue now cannot happen anymore because of the unique index.
  • edited 11:54AM
    I would try debugging in the MY_Model::process_relationships() method. You'll see the logic there that will first clear any related items and then save.
  • edited 11:54AM
    aaaah it was a pretty simple error... a RTFM error ;-)
    I somehow set $clear_related_on_save to FALSE. I cannot remember why I did that during development... but setting to true worked ;-)
  • edited 11:54AM
    So now with setting to true I got another issue... Now I rembember why I set it to false :-)

    I have 2 has_many relationships in my missions_model: mission_images and mission_vehicles.

    In my create and edit form I only want to display the vehicle relation. The images relation is maintained the opposite way via the mission_images model, because there are so many images that it wouldn't be practical to display them in the missions model.
    The vehicle relation is maintained via checkboxes in the missions model.

    mission_images is set to hidden via type => hidden.

    Now on saving a mission the relations to vehicle are deleted and newly inserted correctly becaus of the checkboxes.
    But the image relation is only deleted on not newly inserted... I think its because the old assignements aren't in the POST request.

    Is there an out-of-the-box way to handle this or do I have to implement some hook to prevent the mission_images relation from being deleted...?
  • edited 11:54AM
    What happens if you use unset($fields['mission_Images'); in your form_fields method which will just simply not create a form field element at all (instead of a hidden form field)?
  • edited 11:54AM
    same issue. relations gets deleted on save.
  • edited 11:54AM
    The $clear_related_on_save property looks in the post for a field name of "exists_{field_name}" to determine whether it should delete on save or not. Is there a hidden field with that name?
  • edited 11:54AM
    not for the mission_images relation. I added it for the fahrzeuge relation as you proposed above.
  • edited 11:54AM
    That field is automatically generated with any multi select field or select2. If you view source and look at the HTML of the page, is there an exists_mission_images? If not, I think I would go back to debugging the MY_Model::process_relationships() method.
Sign In or Register to comment.