由于updated_at表的模糊性,雄辩的update()失败 [英] Eloquent update() failing due to updated_at table ambiguity

查看:606
本文介绍了由于updated_at表的模糊性,雄辩的update()失败的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

好的,这个问题来自于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语句是

 更新`child_school_years` 
内部连接`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其中`exit_date`为null)

除了自动添加 updated_at 字段存在于child_school_years和school_years表中,因此Laravel添加的字段会触发Exception Integrity约束违规:1052字段列表中的列updated_at是不明确的



任何关于如何将更新的家庭化的建议?我很高兴有更新的字段,但如果有必要,我可以生活,如果有可能消除它。

解决方案

没有办法改变雄辩的行为,即使调整 UPDATED_AT 列也不会有帮助,所以你需要使用简单的 Query\建筑师,像已经建议的,或下面的方法之一,我觉得好一点:

  //最简单的方法
ChildSchoolYear :: whereNull('exit_date')
- > join('school_years','child_school_years.school_year_id','=','school_years.id')
- > getQuery()//获取底层的基础Query\Builder
- > update(
数组(
'child_school_years.exit_date'=>'school_years`.`end_date '',
'child_school_years.editor_id'=> $ userId,
'child_school_years.updated_at'=> Carbon\Carbon :: now(),

) ;

//也可以工作,临时关闭自动时间戳
($ model = new ChildSchoolYear) - > timestamps = false;

//以上是相同的:
// $ model = new ChildSchoolYear;
// $ model-> timestamps = false;

$ model-> 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,
'child_school_years.updated_at'=> Carbon\Carbon :: now(),

);


Ok, this question stems from a Laravel 4.1.23 install. I'm attempting to update multiple records using the Eloquent update() method on a query that includes a join:

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 is generating the correct SQL for the query content I'm providing above, but the full SQL statement generated is

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) 

This would work except that the automatically added updated_at field exists in both the child_school_years and school_years tables, so the addition of the field by Laravel triggers the Exception Integrity constraint violation: 1052 Column 'updated_at' in field list is ambiguous.

Any suggestions on how to domesticate the updated_at piece? I'd be happy to have the field updated, but I'll live without it if necessary should it be possible to eliminate it.

解决方案

There is no way to alter Eloquent behaviour, even adjusting UPDATED_AT column won't help, so you need to use either simple Query\Builder, like already suggested, or one of the methods below, that I find a bit better:

// easiest way
ChildSchoolYear::whereNull('exit_date')
  ->join('school_years', 'child_school_years.school_year_id','=','school_years.id')
  ->getQuery()  // get underlying base Query\Builder
  ->update(
    array(
      'child_school_years.exit_date'  => '`school_years`.`end_date`',
      'child_school_years.editor_id'  => $userId,
      'child_school_years.updated_at' => Carbon\Carbon::now(),
    )
  );

// also would work, temporary turn off auto timestamps
with($model = new ChildSchoolYear)->timestamps = false;

// above is the same as:
// $model = new ChildSchoolYear;
// $model->timestamps = false;

$model->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,
      'child_school_years.updated_at' => Carbon\Carbon::now(),
    )
  );

这篇关于由于updated_at表的模糊性,雄辩的update()失败的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆