I study Yii2, and I think the problem is obvious, but not for me.
When saving data through a form, an empty line is entered in the database. Used as QueryBuilder and ActiveRecord . Removed validation from the model did not help.
Information, oddly enough in general zero in the CIS. In addition to. documentation, and its translations, nothing at all. I reviewed a bunch of videos, but apart from repeating the same thing, I did not recognize anything new.
Crutches do not want to fence, and anyhow how to stick in the information there is not the right way.
Used a standard widget in the views:

<?php $form = ActiveForm::begin(); ?> <?= $form->field($model, 'name')->label('Имя') ?> <?= $form->field($model, 'category')->label('Категория') ?> <?= Html::submitButton('Отправить', ['class' => 'btn btn-primary']) ?> <?php ActiveForm::end(); ?> 

Model

  namespace app\models; use yii\db\ActiveRecord; class Products extends ActiveRecord { public $name; public $category; public static function tableName() { return 'products'; } } 

The controller in the desired method:

  $model = new Products(); if ($model->load(Yii::$app->request->post())) $model->save(); 

Without validation, without checks, just saving if the form is sent by post.

PS I already hate this framework, through its documentation, although it is huge, but the details are not explained. Spent about a week on this problem ... Help though with ideas, maybe the problem is not directly in the framework.

  • Read about scripts and secure attributes here . You do not load the model, since you do not have safe attributes due to the fact that you have removed the validation rules. - Roman Grinyov
  • Are you sure you send in the View $ model? - Urmuz Tagizade
  • Drop the full action code and we will solve your problem. You forgot 100% to send $ model to View, so this is the problem - Urmuz Tagizade
  • @RomanGrinyov safe attributes (safe) are assigned by default. They have nothing to do with it. - Urmuz Tagizade
  • one
    @UrmuzTagizade, why is that? ... Массовое присвоение применяется только к так называемым безопасным атрибутам, которые являются атрибутами, перечисленными в scenarios() -> По умолчанию scenarios() будет возвращать все сценарии и атрибуты найденные в rules() -> the author says that the rules() masher -> therefore -> there are no safe attributes, since there are no rules() -> therefore -> by default, the attributes will be null , while load() will work (but idle) -> therefore -> that is why the model with confusing attributes (since they are all null ). - Roman Grinyov

1 answer 1

In Gii, I indicated the very model, that is, I duplicated it. And just the root of evil was in it. public $name and public $category without values, that is, they were equal to NULL . I do not know why Yii did not overwrite the data with $model->save (but I guess I need to know something about the attributes). I deleted the public properties and the code quietly worked, both with and without validation. In the model I have only validation and the necessary label:

 public function rules() { return [ [['name'], 'required', 'message'=> 'Lorem ipsum'], [['category'], 'required', 'message'=> 'Bla bla bla'] ]; } public static function tableName() { return 'products'; } 

In the controller, I know what is better through the mass assignment, but this is for myself, check for operability.

 public function actionIndex() { $model = new Products(); if ($model->load(Yii::$app->request->post()) && $model->validate()) { $model->name = Yii::$app->request->post('Products')['name']; $model->category = Yii::$app->request->post('Products')['category']; $model->save(); }else{ return $this->render('index', ['model'=> $model]); } } 

PS Instead of $model->name = Yii::$app->request->post('Products')['name']; write $model->name; when saving, or not to specify attributes at all. I am writing this because I have seen many people who appropriate it through Post.