問題描述
如果我錯(cuò)了,請糾正我,但我認(rèn)為 Eloquent 模型中沒有大規(guī)模更新這樣的東西.
Please correct me if I am wrong, but I think there is no such thing as mass update in an Eloquent model.
有沒有辦法在不為每一行發(fā)出查詢的情況下對數(shù)據(jù)庫表進(jìn)行批量更新?
Is there a way to make a mass update on the DB table without issuing a query for every row?
比如有沒有靜態(tài)方法,比如
For example, is there a static method, something like
User::updateWhere(
array('age', '<', '18'),
array(
'under_18' => 1
[, ...]
)
);
(是的,這是一個(gè)愚蠢的例子,但你得到了圖片......)
(yes, it is a silly example but you get the picture...)
為什么沒有實(shí)現(xiàn)這樣的功能?如果發(fā)生這樣的事情,我是唯一一個(gè)會很高興的人嗎?
Why isn't there such a feature implemented? Am I the only one who would be very happy if something like this comes up?
我(開發(fā)人員)不想像這樣實(shí)現(xiàn)它:
I (the developers), wouldn't like to implement it like:
DB::table('users')->where('age', '<', '18')->update(array('under_18' => 1));
因?yàn)殡S著項(xiàng)目的增長,我們以后可能會要求程序員更改表名,而他們無法搜索和替換表名!
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!
有沒有這樣的靜態(tài)方法來執(zhí)行這個(gè)操作?如果沒有,我們是否可以擴(kuò)展IlluminateDatabaseEloquentModel
類來完成這樣的事情?
Is there such a static method to perform this operation? And if there is not, can we extend the IlluminateDatabaseEloquentModel
class to accomplish such a thing?
推薦答案
對于大規(guī)模更新/插入功能,已被請求但 Taylor Otwell(Laravel 作者)建議用戶應(yīng)改用 Query Builder.https://github.com/laravel/framework/issues/1295
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
你的模型通常應(yīng)該擴(kuò)展 IlluminateDatabaseEloquentModel.然后你訪問實(shí)體本身,例如,如果你有這個(gè):
Your models should generally extend IlluminateDatabaseEloquentModel. Then you access the entity iself, for example if you have this:
<?php
Use IlluminateDatabaseEloquentModel;
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
您必須求助于查詢構(gòu)建器.為了解決表命名問題,您可以通過 getTable() 方法動態(tài)獲取它.唯一的限制是您需要先初始化用戶類,然后才能使用此功能.您的查詢?nèi)缦?
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).
更新 #1
執(zhí)行此操作的其他方法(在您的情況下效率不高)是:
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();
}
這樣表名就保存在用戶類中,你的開發(fā)人員不必?fù)?dān)心.
This way the table name is kept in users class and your developers don't have to worry about it.
這篇關(guān)于Eloquent 模型大規(guī)模更新的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網(wǎng)!