简体   繁体   中英

cakephp hasmany with condition

I am having trouble getting records from a third table when i put a condition on it. I get the error missing column MonthlyReturns missing. But if I take away the MonthlyReturns condition I get all 3 associated tables.

The tables are Companies, Employees and MonthlyReturns

My find code in the MonthlyReturnsController.php:

$conditions = array("Employee.active" => true, "Employee.start" => $start, "NOT" => array("MonthlyReturn.month" => $date));
$empl = $this->MonthlyReturn->Employee->find('all', array('conditions' => $conditions));

Employee Model:

/**
* belongsTo associations
*
* @var array
*/
public $belongsTo = array(
    'Company' => array(
        'className' => 'Company',
        'foreignKey' => 'company_id',
        'conditions' => '',
        'fields' => '',
        'order' => ''
    )
);

/**
* hasMany associations
*
* @var array
*/
public $hasMany = array(
    'MonthlyReturn' => array(
        'className' => 'MonthlyReturn',
        'foreignKey' => 'employee_id',
        'dependent' => false,
        'conditions' => '',
        'fields' => '',
        'order' => '',
        'limit' => '',
        'offset' => '',
        'exclusive' => '',
        'finderQuery' => '',
        'counterQuery' => ''
    )
);

UPDATE:

I have tried to do a left join. Now I get all records back and is not showing employees that do not have a record in MonthlyReturns for the date.

The sql I get is

[query] => SELECT `Employee`.`id`, `Employee`.`company_id`,`Employee`.`p45_issued`, `Company`.`id`, `Company`.`member_number`
FROM `employees` AS `Employee` 
left JOIN `monthly_returns` AS `MonthlyReturn` ON (`MonthlyReturn`.`employee_id` = `Employee`.`id`) 
LEFT JOIN `companies` AS `Company` ON (`Employee`.`company_id` = `Company`.`id`) 
WHERE NOT (`MonthlyReturn`.`month` = '2013-03-01')

$options['joins'] = array(
    array('table' => 'monthly_returns',
        'alias' => 'MonthlyReturn',
        'type' => 'left',
        'conditions' => array(
            'MonthlyReturn.employee_id = Employee.id'
        )
    ),
);

$options['conditions'] = array("Employee.p45_issued" => false, "NOT" => array("MonthlyReturn.month" => $last_month));


$empl = $this->MonthlyReturn->Employee->find('all', $options);

UPDATE 2

I have managed to get the records I want by using sql diectly. I dont like doing this though so if someone knows the correct cakephp way?

    $emps_overdue = $this->Employee->query("SELECT employees.*, companies.`name`, companies.`id`

    FROM employees INNER JOIN companies ON employees.company_id = companies.id

    LEFT JOIN monthly_returns ON monthly_returns.employee_id = employees.id

    AND monthly_returns.month = '$last_month'

    WHERE monthly_returns.id IS NULL AND employees.p45_issued = false");

尝试研究可遏制的行为,这会使您的任务更轻松。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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