Home  >  Article  >  Backend Development  >  Yii2 ActiveRecord sets default value for property

Yii2 ActiveRecord sets default value for property

巴扎黑
巴扎黑Original
2016-11-09 14:14:021500browse

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);
}


Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn