DataTable - 在控制器中按客户 ID 限制视图
Posted
技术标签:
【中文标题】DataTable - 在控制器中按客户 ID 限制视图【英文标题】:DataTable - limit view by customer ID in controller 【发布时间】:2017-08-02 05:23:18 【问题描述】:我正在尝试解决客户的问题。
他有一个在 Codeigniter 框架上运行的应用程序。 在这个应用程序中,他正在通过 DataTables 查看作业列表。
在应用程序中,存在三种不同的访问级别:Admin = 1、Employee = 2、Customer = 3
我试图限制分配列表,只显示属于单个客户的分配。我试图在控制器中为分配列表添加功能,但它对我不起作用。
我做错了什么?
public function assignlist( $status = "" )
$status1 = "Aktiv";
if($status!="")
$status1 = $status;
/* by Kenn */
$log_in = $this->session->userdata('logged_in');
$roleid = $log_in["role_id"];
$CustomerID = $log_in["CustomerID"];
/**/
$this->datatables->select('er.EventId,er.EventName,cu.CustomerName as CustomerId,DATE_FORMAT(er.EventDate, "%d/%m/%Y") AS EventDate,er.TimeFrom,er.TimeTo,er.NoofGuardRequired,er.Remarks,emp.fullname as InsertBy,emp1.fullname as ModifiedBy,er.Status,er.Status as st,cu12.CustomerName as cuInsertBy, cu1.CustomerName as cuModifiedBy');
$this->datatables->from('eventregistration er');
$this->datatables->join('customer cu', 'cu.CustomerId = er.CustomerId', 'left');
$this->datatables->join('employee emp', 'emp.emp_id = er.InsertBy', 'left');
$this->datatables->join('employee emp1', 'emp1.emp_id = er.ModifiedBy', 'left');
$this->datatables->join('customer cu12', 'cu12.CustomerId = er.InsertBy', 'left');
$this->datatables->join('customer cu1', 'cu1.CustomerId = er.ModifiedBy', 'left');
$this->datatables->where('er.Status',$status1);
/* by Kenn */
if ($roleid == "3")
$this->datatables->where('cu.CustomerID',$CustomerID);
/**/
echo $this->datatables->generate();
【问题讨论】:
【参考方案1】:删除同一张表上的多个联接可能会解决您的问题。
public function assignlist( $status = "" )
$status1 = "Aktiv";
if($status!="")
$status1 = $status;
/* by Kenn */
$log_in = $this->session->userdata('logged_in');
$roleid = $log_in["role_id"];
$CustomerID = $log_in["CustomerID"];
/**/
$this->datatables->select('er.EventId,er.EventName,cu.CustomerName as CustomerId,DATE_FORMAT(er.EventDate, "%d/%m/%Y") AS EventDate,er.TimeFrom,er.TimeTo,er.NoofGuardRequired,er.Remarks,emp.fullname as InsertBy,emp.fullname as ModifiedBy,er.Status,er.Status as st,cu.CustomerName as cuInsertBy, cu.CustomerName as cuModifiedBy');
$this->datatables->from('eventregistration er');
$this->datatables->join('customer cu', 'cu.CustomerId = er.CustomerId AND cu.CustomerId = er.InsertBy AND cu.CustomerId = er.ModifiedBy', 'left');
$this->datatables->join('employee emp', 'emp.emp_id = er.InsertBy AND emp.emp_id = er.ModifiedBy', 'left');
$this->datatables->where('er.Status',$status1);
/* by Kenn */
if ($roleid == "3")
$this->datatables->where('cu.CustomerID',$CustomerID);
/**/
echo $this->datatables->generate();
【讨论】:
感谢您的反馈!我只是尝试过,但它没有成功。我的代码导致 DataTable 写入“表中没有可用数据”。 有趣的是,如果我强制查看例如CustomerID = 21,它可以工作:if ($roleid == "3") $this->datatables->where('cu.CustomerID', "21");
这就像变量存储客户ID不同?
你应该写 $this->datatables->where('cu.CustomerId', "21");
而不是 $this->datatables->where('cu.CustomerID', "21");
。请将cu.CustomerID
替换为cu.CustomerId
,因为cu.CustomerID
和cu.CustomerId
是两个不同的东西
就像我昨天发布的答案一样! :) 但是感谢您的评论!【参考方案2】:
我已经成功找到并修复了这个错误。我想这是一个错误 40!
我审查所有字符,包括。区分大小写,当我从数组中获取值时发现 CustomerID 应该是 CustomerId。
public function assignlist( $status = "" )
$status1 = "Aktiv";
if($status!="")
$status1 = $status;
/* by Kenn */
$log_in = $this->session->userdata('logged_in');
$roleid = $log_in["role_id"];
$CustomerId = $log_in["CustomerId"];
/**/
$this->datatables->select('er.EventId,er.EventName,cu.CustomerName as CustomerId,DATE_FORMAT(er.EventDate, "%d/%m/%Y") AS EventDate,er.TimeFrom,er.TimeTo,er.NoofGuardRequired,er.Remarks,emp.fullname as InsertBy,emp.fullname as ModifiedBy,er.Status,er.Status as st,cu.CustomerName as cuInsertBy, cu.CustomerName as cuModifiedBy');
$this->datatables->from('eventregistration er');
$this->datatables->join('customer cu', 'cu.CustomerId = er.CustomerId AND cu.CustomerId = er.InsertBy AND cu.CustomerId = er.ModifiedBy', 'left');
$this->datatables->join('employee emp', 'emp.emp_id = er.InsertBy AND emp.emp_id = er.ModifiedBy', 'left');
$this->datatables->where('er.Status',$status1);
/* by Kenn */
if ($roleid == "3")
$this->datatables->where('cu.CustomerId',$CustomerId);
/**/
echo $this->datatables->generate();
【讨论】:
以上是关于DataTable - 在控制器中按客户 ID 限制视图的主要内容,如果未能解决你的问题,请参考以下文章