繁体   English   中英

GroceryCrud 使用 where 子句设置 nn 关系

[英]GroceryCrud set a n-n relation with where clause

我有三个表(简化),它们是:

在此处输入图片说明

我必须为每个用户显示所有房屋。
在我的控制器中,我有一个这样的功能:

public function create_houses_table($usr_id)
{
  $crud = new grocery_CRUD();

  $crud->set_language("italian");

  $crud->set_theme('datatables');
  $crud->set_subject('Casette');
  $crud->set_table('tbl_houses');

  $crud->set_relation_n_n('Casette', 
                          'tbl_users_houses', 
                          'tbl_users', 
                          'house_id', 
                          'user_id', 
                          'usr_name',
                          NULL,
                          array('user_id' => $usr_id));
...
}

我得到的是:

在此处输入图片说明

每次我从组合中选择一个用户时,我都需要刷新我对 usr_id 的列表过滤...但我总是得到所有的房子

我错了什么?

这不是 set_relation_n_n 的预期用途(它将在用户行内的一个字段中显示所有用户房屋)。

您想要的可以更好地从 tbl_users_houses 列表中完成,通过客户端使用 $crud->where() 进行过滤,并通过两个简单的关系与其他表链接。

如果我理解正确,您正在尝试仅获取登录用户的记录...并且您每个房子都有多个用户,因此是 nn 关系。

我也遇到了这个问题,这就是我所做的。

        $myprojects = $this->admin_model->get_employee_projects($this->user_id);
        $myprojectids = array_column($myprojects, 'id');
        //get only one column from the multi-dimensional array
        $crud->where("`projects`.id IN", "(" . implode(",", $myprojectids) . ")", false); 
        // the false disables escaping

        $crud->set_relation_n_n('assigned_employees', 'project_employees', 'employees', 'project', 'employee', 'name');
        //Only so it also still shows the name of Users assigned

所以基本上这里的projects就像房子一样,我使用WHERE IN子句根据我从模型方法获得的项目过滤记录......

暂无
暂无

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

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