⚠ 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 - update returns bad indexes



Johnny S
  • profile picture
  • Member

Posted 12 April 2013 - 19:27 PM

Hello all!

 

I'm new in forums, so I'm sorry, if my topic is somehow incorrect. Also sorry for my worse english.

 

Few days ago, I tried to search my problem in forums, in google, in bug tracker, but didn't find any solution.

 

[Attaching my DB scheme with N:N relation.]

grocery_crud_bug_img1.jpg

 

My source code (N:N):

 

$crud->set_relation_n_n("categories", "categories_servers", "categories", "id_server", "id_category", "title", "id_category");

 

Symptomps:

- Creating/updating record (server item)

- Result is, that in N:N table apears new records and id_server is correct

- Problem is, that id_category row is always incorrect - not PK from table categories, but index from 0 to X (X = count of selected items in add/edit form)

 

I tried to debug grocery crud model and found working solution for my case. But I don't know if it's correct and won't break anything other.

 

 

My solution:

 

Replace variable $counter with $primary_key_value on line 406:

$where_array[$field_info->priority_field_relation_table] = $primary_key_value;

and line 412:

$this->db->update( $field_info->relation_table, array($field_info->priority_field_relation_table => $primary_key_value) , $where_array);

 

 

Hope, it will be helpful.


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

Posted 12 April 2013 - 20:03 PM

Hello [member='Johnny S'] and welcome to the forums,

 

Well as you are describing your code is wrong. It is probably my mistake as the field_name = 'priority' may confuse people. So I will explain you what the mistake is and what the solution is without changing the basic model of grocery CRUD.

 

Your mistake is the last field.

So in your case, this is wrong:

 

$crud->set_relation_n_n("categories", "categories_servers", "categories", "id_server", "id_category", "title", "id_category");

instead of that it should be:

 

$crud->set_relation_n_n("categories", "categories_servers", "categories", "id_server", "id_category", "title");

The problem is that the last argument (7th argument) is for the ordering. So that's means that it will order your values as well. In your case id_category is used in two different places that this is actually wrong.

 

Please correct me if I am wrong, but for your understanding I think the solution is pretty simple :)

 

Hope it helps


Regards

Johnny


Johnny S
  • profile picture
  • Member

Posted 13 April 2013 - 05:33 AM

Oh...

 

So I have wasted hours of searching and debuging inexisting bug. Great. :) I though, I have something non-standard in model and it causes problem.

 

Thank you for the answer, it works fine, now.

 

 

EDIT:  Ah-haa, NOW I understand the importance of 7th parameter. My bad, I'm sorry. :) First, i though, that 7th parameter is for "field which is used in ORDER BY clause"