2016-11-09 59 views
0

我将一个数组传递给我的模型,我想获取不在该数组中的employee_id。 '位置'字段也应该等于'c'。codeigniter where_not_in不工作

以下是我的表:

emp_position table

以下是我的模型。 $ evdata是传递给模型以限制选择记录的数字。数组$ data也是json编码的数组。

公共职能get_camaramens($数据,$ evdata) {

$this->db->select('emp_position.employee_id'); 
$this->db->from('emp_position'); 
$this->db->where('emp_position.position','c'); 
$this->db->where_not_in('emp_position.employee_id',$data); 
$query=$this->db->get('',$evdata); 
return $query->result(); 

}

它没有给予正确的结果。所以我删除了以下部分并进行了检查。

$this->db->where_not_in('emp_position.employee_id',$data); 

这两次都给出了相同的结果。这似乎只是第一个条件工作的地方。

以下是两个时代的var_dump结果: Following is the var_dump result of both times.

添加代码

$data     = $this->emp_event_model->get_current_employees($date); 

$newData = array(); 
foreach($data as $row) { 
    $newData[] = $row['employee_id']; 
    } 
$data = $newData; 

后,给出了这样的错误:

enter image description here

+0

可你'的var_dump($的数据); '看看'$ data'的结果怎么样? – Beginner

+0

array(size = 5)
0 => 对象(stdClass的)[26] 公共 'EMPLOYEE_ID'=>字符串 '2'(长度= 1) 1 => 对象(stdClass的)[27] 公共 'EMPLOYEE_ID'=>字符串“3 '(长度= 1) 2 => 对象(stdClass的)[28] 公共 'EMPLOYEE_ID'=>字符串 '7'(长度= 1) 3 => 对象(stdClass的)[29] 公共' EMPLOYEE_ID '=> string'8'(length = 1) 4 => object(stdClass)[30] public'employee_id'=> string'9'(length = 1) – Christeen

+0

你是$ data数组必须在'array(1,2,3)'format not'array('employee_id'=> 1,'employee_id'=> 2,等等......)' – Beginner

回答

0

$data数组必须是这种格式

array(2, 3, 7, 8, 9) 

但你的阵列是

array (size=5) 
0 => object(stdClass)[26] public 'employee_id' => string '2' (length=1) 
1 => object(stdClass)[27] public 'employee_id' => string '3' (length=1) 
2 => object(stdClass)[28] public 'employee_id' => string '7' (length=1) 
3 => object(stdClass)[29] public 'employee_id' => string '8' (length=1) 
4 => object(stdClass)[30] public 'employee_id' => string '9' (length=1) 

所以调整你的阵列控制器

$newData = array(); 
foreach($data as $row) { 
    $newData[] = $row->employee_id; 
} 
$data = $newData; 

内部,这将成为

array(2, 3, 7, 8, 9) 
+0

它给这个数组致命错误:不能使用stdClass类型的对象作为数组 – Christeen

+0

@Christeen它给了什么?你应该应该如何重组阵列的代码的屏幕截图 – Beginner

+1

我认为它应该是$ newData [] = $ row-> employee_id;因为它是一个对象不是数组 –