本文介绍了什么会导致 $model->attributes 在 Yii 中无法获得正确的值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的 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] => 
)

description 字段会发生什么?Yii 这个作业有什么问题吗?

what happens for description field? is there any thing about this assignment is Yii?

推荐答案

我发现 yii 中有一个术语围绕这种类型的赋值: Massive Assignment .所以我应该明确地为每个赋值定义验证进行大规模分配的字段.

I found that there is a term in yii around this type of assignments: Massive Assignment .So I should explicitly define validation for each fields to make Massive Assignment.

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

http://www.yiiframework.com/wiki/161/understanding-安全验证规则/#hh2

对于某些字段,无需验证,对吗?

错误:仅分配用户明确指定的字段值说有资格复制到 $model,这限制了恶作剧一个试图污染模型的坏人.

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.

即使一个字段没有特定的数据格式验证,我们仍然必须告诉 Yii 我们希望在 Massive 期间复制该属性任务.这是通过安全"验证器完成的.

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.

这篇关于什么会导致 $model->attributes 在 Yii 中无法获得正确的值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

11-01 21:17