model->attributes in Yii2 always has NULL value
Asked Answered
T

7

14

I have one temporary model as viewModel. In my CRUD actions (for example actionCreate) I want to get this viewModel data and assign that to a ActiveRecord model. I used below code but my model object atrribute always show NULL value for attributes:

$model = new _Users();
if ($model->load(Yii::$app->request->post())) {
    Yii::info($model->attributes,'test'); // NULL
    $attributesValue =[
            'title' => $_POST['_Users']['title'],
            'type' => $_POST['_Users']['type'],
        ];
    $model->attributes = $attributesValue;
    Yii::info($model->attributes,'test'); // NULL

    $dbModel = new Users();
    $dbModel->title = $model->title;
    $dbModel->type = $model->type . ' CYC'; // CYC is static type code
    Yii::info($dbModel->attributes,'test'); // NULL

    if ($dbModel->save()) {
            return $this->redirect(['view', 'id' => $dbModel->id]); // Page redirect to blank page
        }
}
else {
        return $this->render('create', [
            'model' => $model,
        ]);
}

I think $model->load(Yii::$app->request->post()) not working and object attribute being NULL. Is it Yii2 bug or my code is incorrect??

Topminnow answered 18/7, 2014 at 11:6 Comment(3)
You must set rules for this attributesConvoy
First... show me your form and modelConvoy
$model->attributes = $_POST['_Users'];Thoughtout
F
21

If there is no rule for your attribute the $model->load() will ignore those not in the rules of the model.

Add your attributes to the rules function

public function rules()
{
    return [
        ...
        [['attribute_name'], 'type'],
        ...
    ];
}
Feel answered 23/9, 2014 at 15:8 Comment(0)
Z
3

To fetch data for an individually attributes(db-fields) in yii2.0 then you should just do as:

echo $yourModel->getAttribute('email');
Zonazonal answered 15/10, 2015 at 7:25 Comment(0)
S
1

ActiveRecord $attributes is a private property Use $model->getAttribute(string)

Slaby answered 24/8, 2014 at 11:7 Comment(0)
G
1

You can use following codes:

$model = new _Users();
$model->attributes=Yii::$app->request->post('_Users');
$model->title= $model->title
$model->type = $model->type . ' CYC'; // CYC is static type code
#$model->sampleAttribute='Hello World';
Gaughan answered 24/4, 2016 at 2:15 Comment(0)
D
1

Declare attribute as private then

echo $yourModel->attribute 

work as expected

Deemphasize answered 21/4, 2021 at 10:37 Comment(0)
S
0

You must remove all public properties (title, type, etc.) in your _User model and $model->attributes = $post will work correctly.

Soloman answered 2/4, 2016 at 19:17 Comment(0)
L
0

I have also encountered the same problem, i Add my attributes to the rules function,but also error. And i found the reason for this problem. It is beause that the submit form's name in corresponding view file is not the same as the model's name which you use in controller

[controller file]:

$model=new SearchForm();

[view file]:

<input name="SearchForm[attribus]" ...

or 

[view file]:

<?= $form->field($model,'atrribus')->textInput()?>
Lice answered 23/2, 2017 at 9:13 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.