雄辩的模型质量更新

问题描述 投票:33回答:4

如果我错了,请纠正我,但我认为在Eloquent模型中没有大规模更新这样的东西。

有没有办法在数据库表上进行批量更新而不为每一行发出查询?

例如,是否有静态方法,如

User::updateWhere(
    array('age', '<', '18'),
    array(
        'under_18' => 1 
        [, ...]
    )
);

(是的,这是一个愚蠢的例子,但你得到的图片...)

为什么没有实现这样的功能?如果这样的事情出现,我是唯一一个会非常高兴的人吗?

我(开发人员),不喜欢实现它:

DB::table('users')->where('age', '<', '18')->update(array('under_18' => 1));

因为随着项目的增长,我们可能会要求程序员将来更改表名,他们无法搜索和替换表名!

是否有这样的静态方法来执行此操作?如果没有,我们可以扩展Illuminate\Database\Eloquent\Model类来完成这样的事情吗?

php laravel laravel-4 eloquent
4个回答
44
投票

对于大规模更新/插入功能,它被要求,但Taylor Otwell(Laravel作者)建议用户应该使用查询生成器。 https://github.com/laravel/framework/issues/1295

您的模型通常应扩展Illuminate \ Database \ Eloquent \ Model。然后你访问实体本身,例如,如果你有这个:

<?php
Use Illuminate\Database\Eloquent\Model;

class User extends Model {

    // table name defaults to "users" anyway, so this definition is only for
    // demonstration on how you can set a custom one
    protected $table = 'users';
    // ... code omited ...

更新#2

您必须求助于查询构建器。要涵盖表命名问题,您可以通过getTable()方法动态获取它。唯一的限制是在使用此功能之前需要初始化用户类。您的查询如下:

$userTable = (new User())->getTable();
DB::table($userTable)->where('age', '<', 18)->update(array('under_18' => 1));

这样,您的表名称就是User模型中的控制器(如上例所示)。

更新#1

其他方式(在您的情况下效率不高)将是:

$users = User::where('age', '<', 18)->get();
foreach ($users as $user) {
    $user->field = value;
    $user->save();
}

这样,表名保存在用户类中,您的开发人员不必担心它。


50
投票

也许几年前这是不可能的,但在Laravel的最新版本中你绝对可以做到:

User::where('age', '<', 18)->update(['under_18' => 1]);

值得注意的是,在调用update之前需要where方法。


5
投票

使用数据库事务更新批量中的多个实体。更新功能完成后将提交事务,如果发生异常,则会回滚事务。

https://laravel.com/docs/5.4/database#database-transactions

例如,这是我在单个批量更新中为文章重新生成物化路径段塞(https://communities.bmc.com/docs/DOC-9902)的方法:

public function regenerateDescendantsSlugs(Model $parent, $old_parent_slug)
    {
        $children = $parent->where('full_slug', 'like', "%/$old_parent_slug/%")->get();

        \DB::transaction(function () use ($children, $parent, $old_parent_slug) {
            /** @var Model $child */
            foreach ($children as $child) {
                $new_full_slug  = $this->regenerateSlug($parent, $child);
                $new_full_title = $this->regenerateTitle($parent, $child);

                \DB::table($parent->getTable())
                    ->where('full_slug', '=', $child->full_slug)
                    ->update([
                        'full_slug' => $new_full_slug,
                        'full_title' => $new_full_title,
                    ]);
            }
        });
    }

4
投票

对@metamaker回答的一点修改:

DB::beginTransaction();
     // do all your updates here

        foreach ($users as $user) {

            $new_value = rand(1,10) // use your own criteria

            DB::table('users')
                ->where('id', '=', $user->id)
                ->update([
                    'status' => $new_value  // update your field(s) here
                ]);
        }
    // when done commit
DB::commit();

现在,您可以在一个数据库事务中拥有100万个不同的更新


0
投票

laravel 5.8你可以像这样完成大规模更新:

User::where('id', 24)->update (dataAssociativeArray) ;
© www.soinside.com 2019 - 2024. All rights reserved.