After Model inherits ActiveRecord, if you want to set default values for attributes, you can override the constructor. The code is as follows:
/** * 构造函数 * * @param array $config */ public function __construct ($config = []) { $this->setAttribute('is_parent', 1); $this->setAttribute('take_rate', 0); $this->setAttribute('show_virtual', 0); $this->setAttribute('cat_sort', 255); parent::__construct($config); }
The above introduces Yii2 ActiveRecord to set default values for properties, including aspects of the content. I hope it will be helpful to friends who are interested in PHP tutorials.