Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

what can cause $model->attributes to not get correct values in Yii?

Tags:

php

yii

I have these lines in my actionCreate:

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

which return this:

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

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

like image 450
Mohammad Ali Akbari Avatar asked Dec 20 '25 11:12

Mohammad Ali Akbari


2 Answers

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-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.

like image 167
Mohammad Ali Akbari Avatar answered Dec 22 '25 01:12

Mohammad Ali Akbari


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. http://www.jili.ir

like image 44
user3012172 Avatar answered Dec 22 '25 02:12

user3012172



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!