php mass assignment是什么?中文怎么翻译呢
PHPz
PHPz 2017-04-10 15:47:13
[PHP讨论组]

php mass assignment是什么? mass assignment 的中文怎么翻译呢

PHPz
PHPz

学习是最好的投资!

全部回复(3)
PHP中文网

12年的时候,就有这个概念的了,最早是在Ror上面提出来的。之后Laravel也提出了这个概念。

意思是:在数据库里面创建一个条新的数据的时候,有一些不必要(不能)更新的数据在没有特别的代码过虑(filter)的情况下可能会被恶意更新(创建)。

我觉得可以翻译成:批量赋值

参考:
Ror上面的提问;
Laravel上面的回答;
StackOverFlow上面的回答;
专门的解释;

举例:

Mass assignment is when you send an array to the model creation, basically setting >a bunch of fields on the model in a single go, rather than one by one, something >like:

$user = new User(Input::all());
(This is instead of explicitly setting each value on the model separately.)

You can use fillable to protect which fields you want this to actually allow for >updating.

Let's say in your user table you have a field that is user_type and that can have >values of user / admin

Obviously, you don't want users to be able to update this value. In theory, if you >used the above code, someone could inject into a form a new field for user_type and >send 'admin' along with the other form data, and easily switch their account to an >admin account... bad news.

By adding:

$fillable = array('name', 'password', 'email');
You are ensuring that only those values can be updated using mass assignment

To be able to update the user_type value, you need to explicitly set it on the model and save it, like this:

$user->user_type = 'admin';
$user->save();

PHP中文网

mass:
adj. 群众的,民众的;大规模的,集中的
mass assignment翻译为集中赋值比较恰当吧。

p.s. LZ用的是Laravel框架吧?请参考:http://stackoverflow.com/questions/22279435/what-does-mass-assignment-...

PHP中文网

Laravel5 最近看了下。我一般不翻译。

热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责申明 意见反馈 讲师合作 广告合作 最新更新 English
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送
PHP中文网APP
随时随地碎片化学习

Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号