使用Cakephp选择id不在另一个表中的所有行

时间:2015-10-01 15:01:08

标签: php mysql join cakephp-2.3

请帮我在Cakephp中解决这个问题, 我有两个名为employeesfees的表。 Th employees表仅用于员工详细信息和 fees表用于存储员工每月的附加费用条目。

我希望从employees表中获取所有未在fees表格中输入特定月费的员工。

简单地说,获取所有未支付选定月份费用的在职员工。

请帮我在cakephp中解决这个问题

表结构如下所列

员工

+--------------+------------+----------+
| EmployeeId   | name       | active   |    
+--------------+------------+----------+
|      6       |    Mike    |    1     |  
|      7       |    Ethen   |    0     |
|      9       |    Sony    |    1     | 
+--------------+------------+----------+

+--------+-------------+-------------+
| FeeId  | EmployeeId  | monthYear   |    
+--------+-------------+-------------+
|   1    |     6       |  2015-09    |  
|   4    |     7       |  2015-09    |
|   6    |     6       |  2015-08    | 
+--------+-------------+-------------+

我试过这个

$searchFromDate = date('Y-m',strtotime($this->data['searchFromDate']));     
$this->Paginator->settings = array('fields'=>array('Employee.name'),
                'joins'=>array(
                    array(
                        'alias'=>'Fee',
                        'table'=>'fees',
                        'conditions' =>array('Fee.monthYear = '.$searchFromDate)
                    )
                ),
                'conditions' => array(
                    'NOT' => array(
                                'Employee.EmployeeId' => 'Fee.EmployeeId'
                            )),
                'order' => array('Employee.name' => 'ASC') );
$resultData = $this->Paginator->paginate('Employee');

1 个答案:

答案 0 :(得分:0)

将您的查找条件更改为:

'joins'=>array(
    array(
        'alias'=>'Fee',
        'table'=>'fees',
        'conditions' =>array(
            'Fee.monthYear = $searchFromDate',
            'Employee.EmployeeId = Fee.EmployeeId'
         )
    )
),
'conditions' => array(
    'Fee.FeeId' => null
)),