⚠ In case you've missed it, we have migrated to our new website, with a brand new forum. For more details about the migration you can read our blog post for website migration. This is an archived forum. ⚠

  •     

profile picture

Bugs when updating only a n_n_relation value



pacio

pacio
  • profile picture
  • Member

Posted 17 October 2016 - 09:15 AM

I came across a bug today when trying to update what was essentially a table with only column that was a n_n_relation.
The update would actually save but after it saves the code continues and tries to update the rest of the table, only that in my case there was no 'rest of the table' and would naturally generate an error ('You must use the "set" method').

 

So, I created a little workaround:

 

First at the top of the db_update method in the Grocery_CRUD library I declare:

$n_n_update_only = false;

Then in the section that persists the n_n_relation I run the check to see if there are non-n_n_relation updates to be made, changing the value of the variable declared above. The call to Grocery_crud_model then is put inside an if to only run when $n_n_update_only is set to false:

				if(!empty($this->relation_n_n))
				{
					foreach($this->relation_n_n as $field_name => $field_info)
					{
						if (   $this->unset_edit_fields !== null
							&& is_array($this->unset_edit_fields)
							&& in_array($field_name,$this->unset_edit_fields)
						) {
								continue;
						}

						$relation_data = isset( $post_data[$field_name] ) ? $post_data[$field_name] : array() ;

                                                //My changes start here:
						if(empty($update_data)) {
                                                      $n_n_update_only = true;
                                                      $this->db_relation_n_n_update($field_info, $relation_data ,$primary_key);
                            
                        } else {
                            $this->db_relation_n_n_update($field_info, $relation_data ,$primary_key);
                        }
					}
				}

                if($n_n_update_only === false) {
                    if($this->basic_model->db_update($update_data, $primary_key) === false)
                    {
                        return false;
                    }
                }

*Sorry for the bad indenting ^_^

 

P.S. On a completely unrelated note, much of the javascript in the texteditor isn't working in my chrome browser today (e.g. code tags, bold tags), had to open up firefox to make this post