雄辩的模型质量更新 [英] Eloquent model mass update

查看:68
本文介绍了雄辩的模型质量更新的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果我错了,请更正我,但是我有一个雄辩的模型中没有大量更新的东西。



有没有办法让大众在DB表上更新,而不会为每一行发出查询?



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


$ b $
数组(
'under_18')
数组(
$ b数组('年龄',' => 1
[,...]

);

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



为什么没有这样的功能实现?
我是唯一一个会喜欢这样的人会很高兴的人吗?



我(开发人员),不想像这样实现:

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

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



有没有这样一个静态方法来执行这个操作?如果没有,可以扩展 Illuminate\Database\Eloquent\Model Class来完成这样的事情吗?



Tia,

解决方案

对于大规模更新/插入功能,被要求,但泰勒·奥特维尔(Laravel author)建议用户应该使用查询生成器。 https://github.com/laravel/framework/issues/1295



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

  使用Illuminate\ Database\Eloquent\Model; 

类用户扩展模型{

//表名默认为用户,所以这个定义只适用于
//演示如何设置一个定制的一个
保护$ table ='users';
// ...代码omited ...

更新#2



您必须诉诸查询构建器。为了覆盖表命名问题,您可以通过getTable()方法动态获取它。唯一的限制是,您需要将用户类初始化,然后才能使用此功能。您的查询将如下所示:

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

这样你的表名就是用户模型中的控制器(如上例所示) p>

更新#1



其他方式做到这一点(在你的情况下效率不高)将是:

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

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


Please correct me if I am wrong, but I thing there is no such thing as mass update in a eloquent model.

Is there a way to make a mass update on the DB table without issuing a query for every row?

For example, is there a static method, something like

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

(yes, it is a silly example but you get the picture...)

Why isn't there such a feature implemented? Am I the only one who would be very happy if something like this comes up?

I (the developers), wouldn't like to implement it like:

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

because as the project grows, we may require the programmers to change the table name in the future and they cannot search and replace for the table name!

Is there such a static method to perform this operation? And if there is not, can we extend the Illuminate\Database\Eloquent\Model Class to accomplish such a thing?

Tia,

解决方案

For mass update/insert features, it was requested but Taylor Otwell (Laravel author) suggest that users should use Query Builder instead. https://github.com/laravel/framework/issues/1295

Your models should generally extend Illuminate\Database\Eloquent\Model. Then you access the entity iself, for example if you have this:

<?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 ...

Update #2

You have to resort to query builder. To cover table naming issue, you could get it dynamically via getTable() method. The only limitation of this is that you need your user class initialized before you can use this function. Your query would be as follows:

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

This way your table name is controller in User model (as shown in the example above).

Update #1

Other way to do this (not efficient in your situation) would be:

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

This way the table name is kept in users class and your developers don't have to worry about it.

这篇关于雄辩的模型质量更新的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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