Model->Save() Not Working in Yii2

model-save() Not Working In Yii2

It could be a problem related with your validation rules.

Try, as a test, to save the model without any validation in this way:

$model->save(false);

If the model is saved you have conflict with your validation rules. Try selectively removing your validation rule(s) to find the validation conflict.

If you have redefined the value present in active record you don't assign the value to the var for db but for this new var and then are not save.

Try removing the duplicated var.. (only the vars non mapped to db should be declared here.)

Yii2 active record model not saving data

Okay, I figured it out. Turns out that if you declare public attributes in your ActiveRecord model, they obscure the automatic attributes that are created by AR. Data gets assigned to your obscuring attributes but doesn't get sent into the database.

The correct AR model should have been simply this:

<?php

namespace app\models;

use Yii;
use yii\db\ActiveRecord;

class Prompt extends ActiveRecord
{
/**
* @return string the name of the table associated with this ActiveRecord class.
*/
public static function tableName()
{
return 'prompt';
}
}

Yii2 cannot save model data in controller

You are saving model before assigning value to translation_drive_title

$model = new Translation();
if ($model->load(Yii::$app->request->post())) {
$model->translation_drive_title = Yii::$app->request->post('Translation')['translation_title'];
$model->save();
}

Yii2 - Model is not saving in foreach loop in Yii2

I encountered exactly same problem and got perfect solution. This is tested.

$tags = ['#first_Tag','#second_tag','#third_tag','#fourth_Tag'];
foreach ($tags as $t) :

$model = new Tags;

$model->tag_name = $t;

$model->save(); //yii2

unset($model);

endforeach;

This is when you make a new variable with the same name of existing one, it overwrite its value. Here you don't need to make new attribute or set id to null; just unset() $model before the end of foreach loop.

Yii can't save data - unknown method save()

Your Client class extends Model, which does not support saving data in database, thus save() method is not defined. If you want to work with database record, your model should extend ActiveRecord:

class Client extends ActiveRecord {

public static function tableName() {
return 'clients';
}

public function rules() {
return [
[['name', 'lastname', 'birthday'], 'required'],
];
}

public function attributeLabels() {
return [
'id' => 'Id',
'name' => 'Name',
'lastname' => 'Last Name',
];
}
}


Related Topics



Leave a reply



Submit