Home > Article > Backend Development > Yii2 ActiveRecord sets default value for property
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); }