【发布时间】:2014-08-15 02:14:01
【问题描述】:
好的,这个问题源于 Laravel 4.1.23 安装。我正在尝试在包含连接的查询上使用 Eloquent update() 方法更新多条记录:
ChildSchoolYear::whereNull('exit_date')->
join('school_years', 'child_school_years.school_year_id','=','school_years.id')->
update(array('child_school_years.exit_date'=>'`school_years`.`end_date`',
'child_school_years.editor_id'=>$userId))
Laravel 正在为我上面提供的查询内容生成正确的 SQL,但生成的完整 SQL 语句是
update `child_school_years`
inner join `school_years` on `child_school_years`.`school_year_id` = `school_years`.`id`
set `child_school_years`.`exit_date` = `school_years`.`end_date`,
`child_school_years`.`editor_id` = 2,
`updated_at` = 2014-08-15 02:00:33 where `exit_date` is null)
除了在 child_school_years 和 school_years 表中都存在自动添加的 updated_at 字段之外,这将起作用,因此 Laravel 添加该字段会触发异常 Integrity constraint violation: 1052 Column 'updated_at' in field list is ambiguous。
关于如何驯化 updated_at 片段有什么建议吗?我很乐意更新该字段,但如果有必要,如果可以消除它,我会不使用它。
【问题讨论】:
-
一种解决方法是使用查询生成器而不是 eloquent。请参考以下链接forumsarchive.laravel.io/viewtopic.php?id=9680