T
T
Theory Theory2020-09-02 07:52:36
Yii
Theory Theory, 2020-09-02 07:52:36

Field 'title' doesn't have a default value. Why?

Controller:

$model = new News();
    $model->title = "wow";
    $model->save();


Model:
<?php
namespace app\models;
use yii\db\ActiveRecord;

class News extends ActiveRecord
{
  public $title;
  
  public static function tableName ()
  {
    return '{{news}}';
  }
}

Structure title: varchar(65) - latin1_swedish_ci - null(Yes) - default value(no)

Result:
Field 'title' doesn't have a default value. If you set the default value to null. title accepts null.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
V
Vladislav, 2020-09-02
@Narbek

Nothing will be written to the database if there are no rules in the model.
More precisely, if the property is not described in the rules
Describe them:

public function rules()
    {
        return [
            [['title'], 'string'],
        ];
    }

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question