简体   繁体   English

如何使用杂货店 set_relation 函数在不同列中显示来自两个不同表的内容

[英]How to use grocery set_relation function to display content from two different tables in different columns

I have to tables with these schema:我必须使用这些架构的表:

users(id, name, email) 
user_details (id, user_id, physical_address, otherinfo)

I would like to display all contents of both tables in one grid using grocery crud when i try to use set relation on the first table as shown: Note: I have reducted the part that does the rendering of the view;当我尝试在第一个表上使用 set 关系时,我想在一个网格中显示两个表的所有内容,如图所示: 注意:我已经减少了渲染视图的部分;

$crud = new grocery_CRUD();
$crud->set_table('users');
$crud->set_relation('id', 'user_details', '{physical_address} + {otherinfo}');

the values of the id field as well as the referenced table don't appear in the grid, so it does not seem to work when using primary keys. id 字段的值以及引用的表没有出现在网格中,因此在使用主键时它似乎不起作用。

So I decided to start with the contents of the second table like so:所以我决定从第二个表的内容开始,如下所示:

$crud = new grocery_CRUD();
$crud->set_table('user_details');
$crud->set_relation('user_id', 'users', '{name} + {email}');

This works, but the problem is that the values appear in one column in the grid.这可行,但问题是这些值出现在网格的一列中。 I would like to know how i can separate them to different columns and make them editable in separate input fields.我想知道如何将它们分成不同的列,并使它们在单独的输入字段中可编辑。

my way kinda ridiculous,我的方式有点可笑,
if i were really need the field to be separated, i might use callback_column then use active records to return the value using $row->user_id , and callback each record如果我真的需要分隔字段,我可能会使用callback_column然后使用活动记录使用$row->user_id返回值,并回调每个记录

just like:就像:
return $this->some_model->get_name($row->user_id); for name field对于名称字段
and
return $this->some_model->get_name($row->user_id); for email field对于电子邮件字段

but, as i tested with some dummy records, it can't be sorted, (i dont know if anyone could)但是,当我用一些虚拟记录测试时,它无法排序,(我不知道是否有人可以)

There is no direct way to show multiple columns but i found an work around for this problem.没有直接的方法来显示多列,但我找到了解决这个问题的方法。 here is how set name column as follows.这里是如何设置名称列如下。

$crud->columns('name','email'); // define all required columns
$crud->callback_column(unique_field_name('user_id'),array($this,'callback_set_user_name'));

function unique_field_name($field_name) 
{
    return 's'.substr(md5($field_name),0,8); 
}

public function callback_set_user_name($value, $row)
    {
        $row->full_name = $row->s447d3092; // before getting to know s447d3092 key return json_encode($row); you will key pair with {name} + {email} ex: {"s447d3092":"shiva + shiv@gmail.com"}
        return explode("+",$value)[0]; // here you will have only name
    }

$crud->callback_column('email',array($this,'callback_set_email'));

public function callback_set_email($value, $row)
    {
      return explode("+",$row->full_name)[1];
    }

I am 100% sure this will help you我 100% 确定这会对您有所帮助

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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