简体   繁体   English

GroceryCrud 使用 where 子句设置 nn 关系

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

I have three tables (simplified) which are:我有三个表(简化),它们是:

在此处输入图片说明

And I have to display all houses for each user.我必须为每个用户显示所有房屋。
In my controller I have a function like this:在我的控制器中,我有一个这样的功能:

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

and what I get is this:我得到的是:

在此处输入图片说明

Every time I select a user from the combo I need to refresh my list filtering on usr_id... but I get always all the houses .每次我从组合中选择一个用户时,我都需要刷新我对 usr_id 的列表过滤...但我总是得到所有的房子

What I'm wrong?我错了什么?

This is not the intended usage for set_relation_n_n (it will show all the user houses in one field inside the user row).这不是 set_relation_n_n 的预期用途(它将在用户行内的一个字段中显示所有用户房屋)。

What you want can be better done listing from tbl_users_houses, filtering by client with $crud->where() and linking with the other tables with two simple relations.您想要的可以更好地从 tbl_users_houses 列表中完成,通过客户端使用 $crud->where() 进行过滤,并通过两个简单的关系与其他表链接。

If I understand correctly you are trying to fetch only the records for the logged in User... and u have multiple users per house , hence the nn relation.如果我理解正确,您正在尝试仅获取登录用户的记录...并且您每个房子都有多个用户,因此是 nn 关系。

I also faced this problem and here's what I did.我也遇到了这个问题,这就是我所做的。

        $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

So basically projects here is like houses, and I am using the WHERE IN clause to filter the records based on the projects I get from my model method...所以基本上这里的projects就像房子一样,我使用WHERE IN子句根据我从模型方法获得的项目过滤记录......

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

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