【问题标题】:How to update one table using another table in CakePHP 3如何在 CakePHP 3 中使用另一个表更新一个表
【发布时间】:2016-01-05 06:58:33
【问题描述】:

我有两个表格,例如 status_reportholiday。我想使用 holiday 表更新 status_report 表。我想使用 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)

我尝试了以下方法,但我不知道如何使用另一个表的日期设置状态字段。

$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')

但它给出如下错误:

错误: SQLSTATE[42S02]:未找到基表或视图:1146 表 'ollo_hrm.attendancereports' 不存在

【问题讨论】:

  • $offday = $this->loadModel('StatusReports'); $dateStart=''; $dateStart =''; $query_offday = $offday->query(); $query_offday->update() ->set(['status' => $status]) ->where(['work_date BETWEEN :start AND :end']) ->bind(':start', $dateStart) - >bind(':end', $dateStart) ->execute();我正在尝试这个,但我不知道如何使用另一个表数据设置状态字段。
  • 请通过编辑将 php 代码添加到您的问题中。
  • 您真的有一个名为 AttendanceReports 的表吗?或者是出席报告
  • 是的,表名是出席报告。现在它的工作。非常感谢@arilia

标签: php mysql cakephp cakephp-3.0 cakephp-3.x


【解决方案1】:

我目前无法尝试代码,但类似这样的东西应该可以工作

$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');
        }
    );

【讨论】:

  • 它不工作。我尝试了以下方法,但它给出了如下所示的 error.code $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();
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-09-09
  • 2016-10-02
  • 2019-12-27
相关资源
最近更新 更多