简体   繁体   English

Yii中$ model-> attributes和$ model-> setAttributes()之间的区别

[英]Difference between $model->attributes and $model->setAttributes() in Yii

Whenever I use $model->attributes=$_POST['Users'] ,it saves Data from User form. 每当我使用$model->attributes=$_POST['Users'] ,它都会保存用户表单中的数据。

When I use $model->setAttributes($_POST['Users']) ,it also saves Data from User form. 当我使用$model->setAttributes($_POST['Users']) ,它还会保存用户表单中的数据。

So please can anyone clarify the difference between the two codes ? 那么请任何人澄清两个代码之间的区别吗?

With $this->setAttributes() you can assign unsafe attributes, using $this->attributes you cant. 使用$this->setAttributes()您可以使用$this->attributes来分配不安全的属性。

Assigning unsafe attributes: 分配不安全的属性:

$this->setAttributes($_POST['Model'], false);

More info in: http://www.yiiframework.com/doc/api/1.1/CModel/#setAttributes-detail 更多信息在: http//www.yiiframework.com/doc/api/1.1/CModel/#setAttributes-detail

As stated in the Yii wiki, you can use any of these. 如Yii wiki所述,您可以使用其中任何一种。 With $model->attributes you set the variable directly. 使用$model->attributes可以直接设置变量。 With $model->setAttributes() you set the variable through a so called 'setter method'. 使用$model->setAttributes()您可以通过所谓的“setter方法”设置变量。

http://www.yiiframework.com/wiki/167/understanding-virtual-attributes-and-get-set-methods/#hh1 http://www.yiiframework.com/wiki/167/understanding-virtual-attributes-and-get-set-methods/#hh1

I would use the setter method instead of directly calling the variable, as you can add a line in your setter method, and it would apply to all of its calls, and it would save you from a lot of headache in the future. 我会使用setter方法而不是直接调用变量,因为你可以在你的setter方法中添加一行,它将适用于它的所有调用,并且它可以避免你将来的很多麻烦。

Example: 例:

class Model {
  public $attributes;

  public function setAttributes($attributes) {
    $this->attributes = $attributes;
  }
  public function getAttributes() {
    return $this->attributes;
  }
}

$model = new Model();
$model->setAttributes("Foo");
echo $model->getAttributes();
$model->setAttributes("Bar");
echo $model->getAttributes();

So, now if you would like to make an additional operation on the attribute, you could add it to the setAttributes() method, and instead of changing two lines of code, you could change only one. 所以,现在如果你想对属性进行额外的操作,你可以将它添加到setAttributes()方法,而不是改变两行代码,你只能改变一行。

Example: 例:

class Model {
  public $attributes;

  public function setAttributes($attributes) {
    $this->attributes = $attributes . "-Bar";
  }
  public function getAttributes() {
    return $this->attributes;
  }
}

$model = new Model();
$model->setAttributes("Foo");
echo $model->getAttributes();
$model->setAttributes("Bar");
echo $model->getAttributes();

Now scale this up to a level, when it would be inconvenient to change thousands of lines of code, instead of changing a couple of setter methods. 现在将其扩展到一个级别,当不方便更改数千行代码时,而不是更改几个setter方法。

There is absolutely no difference. 绝对没有区别。

When you try to assign a property that is not defined as a PHP class property (such as attributes here) on a component , Yii by convention calls the similarly-named setter method setAttributes instead. 当您尝试没有被定义为一个PHP类属性(如属性分配attributes一个在这里) 组成 ,通过Yii中调用约定的类似名称的setter方法setAttributes代替。 If no such method exists an exception is thrown. 如果不存在此类方法,则抛出异常。 Since a Yii model is a component and models do not have an attributes property, the setter method is called even when you use the first form. 由于Yii模型是一个组件而模型没有attributes属性,因此即使使用第一个表单也会调用setter方法。

All of this is also explained in detail in the manual. 所有这些也在手册中详细解释

$model->attributes=$_POST['Users']// means setting value of property directly while
$model->setAttributes($_POST['Users']) //is method or function which is indirectly set value of $model->attributes property;  

Lets take an example 让我们举个例子

  class Model{
        public $attributes;


         public function setAttributes($att){
             $this->attributes=$att;

        }


  }

            //Now the value of $attributes can be set by two way


 $model = new Model();
 $model->attributes=$value; // 1st way
 $model->setAttributes($value); //2nd way

There is no difference. 没有区别。 array_merge used to merge attributes, if set later array_merge用于合并属性,如果稍后设置的话

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM