简体   繁体   中英

Listing from multiple tables in grocery crud

I just started exploring grocery crud. I want list the values from multiple tables linked. As per their guide/tutorials I tried to figure out the solution, but couldn;t able to find solution. Expected query and solution is on this link http://sqlfiddle.com/#!9/0c5faf/3

$crud = $this->generate_crud('admin_users_groups');
$crud->where('group_id','3');
$crud->columns('user_id','DistID');
$crud->display_as('user_id','user');
$crud->set_relation('id', 'dist_info', 'ref_idkey');
//$crud->set_relation_n_n('groups', 'users_groups','groups','user_id','group_id','description');
//$crud->set_relation('user_id', 'admin_users', '{username} - {first_name}{last_name}');
//$crud->set_relation_n_n('DistID', 'admin_users','dist_info','id','id','ref_idkey');
//$crud->set_relation('user_id', 'admin_users', 'email');    
//$crud->set_relation_n_n('id', 'admin_users_groups', 'admin_users', 'id','dist_id', 'name');
//$crud->set_relation_n_n('groups1', 'admin_users_groups', 'admin_groups','user_id', 'group_id', 'name');`

Can any one help me to reach to desired solution?

You're just missing some columns in your table. But the real trick is you basically want to display the id column twice, first to show the id and then in the set_relation call, but you can't do that as obviously the library won't know which one is supposed to go where. You have two choices, create a custom model to allow a join of the two tables or use a callback to manually go get the data you need. The second choice should be much easier:

$crud = $this->generate_crud('admin_users_groups');
$crud->where('group_id','3');
$crud->columns('id, 'group_id, 'user_id','DistID')
$crud->display_as('user_id','user');
$crud->callback_column('DistID', array($this, 'callback_distID');
...
}

public function callback_distID($value, $row) {
    //Load your model for the 'dist_info'
    $this->load->model('Dist_info_model);
    $dist = $this->Dist_info_model->get_dist_info($row->id);
    return $dist['ref_idkey'];

Then in your model:

public function get_dist_info($id) {
    return $this->db->getwhere('Dist_info', array('recid'=>$id))->row_array; 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM