⚠ 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

Is this possible with grocery crud? And how (N:N relations)



Johnny S

Johnny S
  • profile picture
  • Member

Posted 24 April 2013 - 17:24 PM

Hi again.

I tried several times to find answer on my question searching in this forum. But I don't know, how exactly call the relation, which I want to create. I just created some snapshot, how I imagine, it should look like and part of MWB model for better imagination.

 

1. Is this possible with grocery crud?

2. Automatically or with creating own model or another trick? (which I don't know, yet)

 

Thank you for reply.

 

Johnny

 

 

gcrud_ask.png


mavershim

mavershim
  • profile picture
  • Member

Posted 25 April 2013 - 00:02 AM

take  a look here http://www.grocerycrud.com/documentation/options_functions/set_relation_n_n


Johnny S

Johnny S
  • profile picture
  • Member

Posted 25 April 2013 - 09:55 AM

Ok, I have tried to resolve this with callback functions.

[I writing it in hope, that someone with similar problem will find out this thread as helpful...]

 

What I have done:

1. added callbacks for view customized part of add/edit form and callbacks after submit forms:



$crud->callback_add_field('properties', array($this,"_admin_properties_list"));
$crud->callback_edit_field('properties', array($this,"_admin_properties_list"));
$crud->callback_after_insert(array($this,"_admin_properties_save"));
$crud->callback_after_update(array($this,"_admin_properties_save"));

 

2. Method _admin_properties_list will get list of properties from Properties table (with JOIN ON server_properties table - because I wanna fill  existing values during edit). 

 

3. Then fill and return simple template (will appear in grocery crud in Properties line:



<?php foreach($properties as $idx=>$params): ?>
    <input type="text" name="properties[<?= $params['id_property'] ?>]" value="<?= (isset($params['value']) ? $params['value'] : "") ?>" title="<?= $params['label'] ?>" placeholder="<?= $params['label'] ?>">
    <br>
<?php endforeach; ?>


 

Then it looks like this:

 

gcrud_result1.jpg

When creating new property, it will appear as empty and can be filled in all items[servers] without creating new property for every single item [server]... Win :)


davidoster

davidoster
  • profile picture
  • Member

Posted 26 April 2013 - 06:55 AM

Even though I am not sure what exactly you are trying to do I have a question for you.

What happens with the delete in all cases for your system?


Johnny S

Johnny S
  • profile picture
  • Member

Posted 29 April 2013 - 04:51 AM

Even though I am not sure what exactly you are trying to do I have a question for you.

What happens with the delete in all cases for your system?

 

Ok, "delete" is good point. Isn't resolved, yet in presented solution. I'm developing this on the fly, so I will edit my post, if I find something bad.

 

And what I trying to do:

It's hard to describe in my mother-language, so in english is more difficult. I will write example:

 

- I have database of gaming servers - table "servers"

- I have self-defined properties for them - in table "properties" (for example: IP, name, several server configuration parameters... - I have not exact count for now and I don't want to rebuild database after each add of new parameter)

- I have N:N table for join it in table "server_properties"

- BUT DIFFERENCE between classic N:N relation and this relation is, that I set properties for every servers, but not every property is needed to fill. VALUE of property is NOT in "properties" table, but in "server_properties" table. For example:  I have property "IP" I want it for every server, so every server get only ID from properties table, but I don't want ID, I want join value to it - so I put value, for example "127.0.0.1" in "server_properties" table in field "value".

 

servers

item:  id_server = 1

...

properties

item:  id_property = 1 | label = "IP" | codename = "ip"

...

server_properties

item:  id_server_property = 1 | id_server = 1 | id_property = 1 | value = "127.0.0.1"

 

- Now I know, that I should divide this form into 2 forms (cruds) - For servers (with categories, etc) itself and for properties and join them (link from server item to properties crud) - maybe I will rebuild it in future, now I like the idea have "all in one"