如何在 CakePHP 3 中使用另一个 table 更新一个 table
How to update one table using another table in CakePHP 3
我有两个 table,例如 status_report 和 假期。我想更新 status_report table 使用 holiday table。我想使用 CakePHP 生成以下 SQL。
UPDATE AttendanceReports s, holiday h
SET s.Status = h.description
WHERE s.work_date = h.work_date and
h.work_date between '2015-05-05' and '2015-12-31';
如何在 CakePHP 中编写此查询?(我正在使用 CakePHP 3.x)
我尝试了以下方法,但我不知道如何使用另一个 table 的日期设置状态字段。
$this->loadModel('Usermgmt.AttendanceReports');
$dateStart = date('Y-m-01') ;
$dateEnd = date('Y-m-t');
$q = $this->AttendanceReports->query()
->update('AttendanceReports s, Holidays h')
->set(['s.status = h.description'])
->where(['s.work_date = h.work_date','h.work_date BETWEEN :start AND :end'])
->bind(':start', $dateStart)
->bind(':end', $dateEnd)
->execute();
这将生成像这样的正确输出
UPDATE AttendanceReports s, Holidays h
SET s.status = h.description
WHERE (s.work_date = h.work_date AND
h.work_date BETWEEN '2016-01-01' AND '2016-01-31')
但它给出如下错误:
Error: SQLSTATE[42S02]: Base table or view not found: 1146 Table 'ollo_hrm.attendancereports' doesn't exist
我目前无法尝试该代码,但像这样的东西应该可以工作
$q = $this->StatusReports->query()
->update('status_report s, holiday h')
->set(['s.Status = h.Ocassion'])
->where(function ($exp, $q) {
return $exp->add('s.Workdate = h.holiday_date')
->between('h.holiday_date','2015-05-05', '2015-12-31');
}
);
我有两个 table,例如 status_report 和 假期。我想更新 status_report table 使用 holiday table。我想使用 CakePHP 生成以下 SQL。
UPDATE AttendanceReports s, holiday h
SET s.Status = h.description
WHERE s.work_date = h.work_date and
h.work_date between '2015-05-05' and '2015-12-31';
如何在 CakePHP 中编写此查询?(我正在使用 CakePHP 3.x)
我尝试了以下方法,但我不知道如何使用另一个 table 的日期设置状态字段。
$this->loadModel('Usermgmt.AttendanceReports');
$dateStart = date('Y-m-01') ;
$dateEnd = date('Y-m-t');
$q = $this->AttendanceReports->query()
->update('AttendanceReports s, Holidays h')
->set(['s.status = h.description'])
->where(['s.work_date = h.work_date','h.work_date BETWEEN :start AND :end'])
->bind(':start', $dateStart)
->bind(':end', $dateEnd)
->execute();
这将生成像这样的正确输出
UPDATE AttendanceReports s, Holidays h
SET s.status = h.description
WHERE (s.work_date = h.work_date AND
h.work_date BETWEEN '2016-01-01' AND '2016-01-31')
但它给出如下错误:
Error: SQLSTATE[42S02]: Base table or view not found: 1146 Table 'ollo_hrm.attendancereports' doesn't exist
我目前无法尝试该代码,但像这样的东西应该可以工作
$q = $this->StatusReports->query()
->update('status_report s, holiday h')
->set(['s.Status = h.Ocassion'])
->where(function ($exp, $q) {
return $exp->add('s.Workdate = h.holiday_date')
->between('h.holiday_date','2015-05-05', '2015-12-31');
}
);