Search code examples
grocery-crud

Using relational table field in grocery crud list view


I have a tables user, user_dealer and dealer. user.id and dealer.id is stored in user_dealer as user_id and dealer_id (many 2 many).

Using this code in Grocery Crud:

$crud->set_relation_n_n('Dealer', config_item('db_prefix') . 'user_dealer', config_item('db_prefix') . 'dealer', 'user_id', 'dealer_id', 'dealer');
$crud->columns('firstname', 'lastname', 'username', 'email', 'dealer_code', 'active',' lockdate');

The dealer_code field is in the dealer table. Since it is a many 2 many relationship, how do I display the dealer_code in the list view? It can be any one of the dealer codes if there are multiple ones.


Solution

  • In your case you will do something like this:

    $crud->set_relation_n_n('dealer', config_item('db_prefix') . 'user_dealer', config_item('db_prefix') . 'dealer', 'user_id', 'dealer_id', 'dealer');
    $crud->columns('firstname', 'lastname', 'username', 'email', 'dealer', 'active',' lockdate');
    

    So in your case we just renamed the "Dealer" to 'dealer' and we did call it at the columns as a 'dealer' and now you can actually call the dealer names and they will be separated by comma.

    If still this is not what are you looking for and you need to show the dealer_id separated with commas and not the names then you will need to use the set_model at: http://www.grocerycrud.com/documentation/options_functions/set_model . As this is a known issue and set_model was firstly created for relation_n_n to do more complicated stuff, there is also an example at the page that you can see and help you.