⚠ 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

relation n_n missing when unset field



David Leiva

David Leiva
  • profile picture
  • Member

Posted 23 October 2012 - 19:17 PM

Hello,

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?

web-johnny

web-johnny
  • profile picture
  • Administrator
  • 1,166 posts

Posted 23 October 2012 - 19:24 PM

Hmmmmm :huh: Probably a bug. I will check it when I will have more time to have it fixed (if it is a bug) as for the next version. https://github.com/scoumbourdis/grocery-crud/issues/113

David Leiva

David Leiva
  • profile picture
  • Member

Posted 23 October 2012 - 20:03 PM

Thanks a lot web-jonhnny,

If you can give me a track about where could be the issue, maybe i can try to help.

thanks!

web-johnny

web-johnny
  • profile picture
  • Administrator
  • 1,166 posts

Posted 23 October 2012 - 21:25 PM

Well I haven't checked yet. The problem probably is that when you add an empty select the form doesn't even send the name. For example if we have a simple select with name="test" the "test" value is sended. But when the field is multiple select with name = "test" then if nothing is selected the form with not even send the $_POST['test'] = array().

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();

David Leiva

David Leiva
  • profile picture
  • Member

Posted 24 October 2012 - 09:27 AM

Exactly , like you told me.
I fix the problem. I've sent you the pull request.

thanks!

web-johnny

web-johnny
  • profile picture
  • Administrator
  • 1,166 posts

Posted 24 October 2012 - 15:18 PM

[member='David Leiva'] I really appreciate your help and thank you. However this is not a right fix and I am sorry. I will explain you why.

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

David Leiva

David Leiva
  • profile picture
  • Member

Posted 25 October 2012 - 12:45 PM

Hello Johnny,

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.

David Leiva

David Leiva
  • profile picture
  • Member

Posted 25 October 2012 - 13:11 PM

New aproach


[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]

web-johnny

web-johnny
  • profile picture
  • Administrator
  • 1,166 posts

Posted 28 October 2012 - 09:22 AM

Your logic is right but this is a much simpler approach: https://github.com/scoumbourdis/grocery-crud/commit/fbae20abef7f0f3e7728d8f801e66a9ef7262c5c :)

Thanks for reporting the bug.