GroceryCrud 使用 where 子句设置 n-n 关系

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));
...
}

我得到的是:

每次我 select 来自组合的用户时,我需要刷新我的列表过滤usr_id...但我总是得到所有的房子.

我哪里错了?

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

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

如果我理解正确的话,你正试图只获取登录用户的记录...并且每个 house 有多个用户,因此是 n-n 关系。

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

        $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 子句根据我从模型方法中获得的项目过滤记录...