加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 站长学院 > PHP教程 > 正文

php – 什么会导致$model->属性在Yii中无法获得正确的值?

发布时间:2020-12-13 22:03:53 所属栏目:PHP教程 来源:网络整理
导读:我在actionCreate中有这些行: if (isset($_POST['DpcioCbn'])) { print_r($_POST['DpcioCbn']); $model-attributes = $_POST['DpcioCbn']; print_r($model-attributes); die(); ...} 返回此: Array( [code] = 34324 [bn_fa] = dfsf [bn_en] = sdf [cbn_fa]
我在actionCreate中有这些行:

if (isset($_POST['DpcioCbn'])) {
  print_r($_POST['DpcioCbn']);
  $model->attributes = $_POST['DpcioCbn'];
  print_r($model->attributes);
  die();
  ...
}

返回此:

Array
(
    [code] => 34324
    [bn_fa] => dfsf
    [bn_en] => sdf
    [cbn_fa] => sdfds
    [cbn_en] => f
    [description] => dsfsdfsdf
    [update_at] => 1391-03-16
    [active] => 1
)
Array
(
    [active] => 1
    [code] => 34324
    [bn_fa] => dfsf
    [bn_en] => sdf
    [cbn_fa] => sdfds
    [cbn_en] => f
    [update_at] => 1391-03-16
    [id] => 
    [description] => 
)

描述字段会发生什么?这个任务的任何事情是Yii吗?

解决方法

我发现yii中有一个关于这种类型的赋值的术语:Massive Assignment.因此,我应该明确定义每个字段的验证以进行Massive Assignment.

public function rules() {
  return array(
      ...
      array('description','safe'),...
  );
}

http://www.yiiframework.com/wiki/161/understanding-safe-validation-rules/#hh2

For some fields,there’s nothing to validate,right?

Wrong: by only assigning field values that the user has explicitly
said are eligible for copying into $model,this limits shenanigans of
a bad guy trying to pollute a model.

Even if a field has no particular data-format validations,we still have to tell Yii we want the attribute copied during Massive Assignment. This is done with the ‘safe’ validator.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读