relation n_n missing when unset field
- Single Page
Posted 23 October 2012 - 19:17 PM
I think this is a bug.
I have a relation_n_n working fine in view mode.
I need to edit the record but with no relation_n_n changes, so I use unset_fields or unset_edit_fields to avoid the relation modification in edit mode but when changes are applied the relation records are deleted so after that i can not see the records in the view mode.
Of course, if i dont use unset_fields, i can see the relation in edit mode, and when i save it, everything works fine.
any solution?
Posted 23 October 2012 - 19:24 PM
Posted 23 October 2012 - 20:03 PM
If you can give me a track about where could be the issue, maybe i can try to help.
thanks!
Posted 23 October 2012 - 21:25 PM
So I guess that somewhere I added something like:
if (!isset($_POST['test']))
$field = array(); //an empty array so we will delete everything
So I didn't consider that you can have unset_field there. So I will also check if the field is unset or something similar.
For now a quick work-around is that if you want to unset the field from update and insert you can do something like that:
$crud = new grocery_CRUD();
...
$state = $crud->getState();
if ($state != 'insert' && $state != 'update') {
$crud->set_relation_n_n(....);
}
...
$crud->render();
Posted 24 October 2012 - 09:27 AM
I fix the problem. I've sent you the pull request.
thanks!
Posted 24 October 2012 - 15:18 PM
The problem as I said is that we have 3 scenarios:
1) Insert data to mutliselect
2) Not insert data to multiselect
3) unset the multiselect
So we will have those three expected results:
1) we are expecting an array of data. For example: $_POST['field_name'] = array['12','45']
2) we are expecting empty data and the field_name is not even set. So in our case we will [b]not have[/b] $_POST['field_name'] = array(); but the field will not be setted (exactly the same as 3) ). If this happens then [b]we want [/b]to remove all the previous data for this primary key as the user unselect all the data from the form.
3) Exactly the same thing happens with the 2nd scenario with the only difference that we [b]don't want [/b]to delete the previous data for this primary key.
So in the case of grocery CRUD we have:
1) [b]expected[/b]
2) [b]expected[/b]
3) [b]not expected[/b]
and in your solution we have:
1) [b]expected[/b]
2) [b]not expected[/b]
3) [b]expected[/b]
Anyway the solution is something like this:
if(!in_array($field_name,$this->unset_add_fields) && !in_array($field_name,$this->unset_edit_fields) {
$this->db_relation_n_n_update($field_info, $relation_data ,$primary_key);
}
Be careful this is [b]not [/b]the solution. I will take a look for this when I will find some time. I just added this code for an example to understand what I mean.
Cheers
Johnny
Posted 25 October 2012 - 12:45 PM
Thanks for your explanation, I am just really begining with your amazing library and I don't understand the core very well yet.
I'll check it again to try to understand .
Thanks again!
Cheerss
David.
Posted 25 October 2012 - 13:11 PM
[php]if (isset( $post_data[$field_name] ) ){
// field is in the post. Update normaly.
$relation_data = $post_data[$field_name];
$this->db_relation_n_n_update($field_info, $relation_data ,$primary_key);
}else{
// field is NOT in the post. maybe due to unset or maybe delete. ?
if( !in_array($field_name,$this->unset_add_fields) && !in_array($field_name,$this->unset_edit_fields) )
{
// field needs to be empty because is not in the unset add or edit arrays.
$relation_data = array();
$this->db_relation_n_n_update($field_info, $relation_data ,$primary_key);
}
// if field is in one of the unset arrays. we keep the relation date. Do nothing.
}[/php]
Posted 28 October 2012 - 09:22 AM
Thanks for reporting the bug.