Home > Backend Development > PHP Tutorial > Yii2 ActiveRecord sets default value for property

Yii2 ActiveRecord sets default value for property

WBOY
Release: 2016-07-29 09:06:14
Original
1076 people have browsed it

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);
	}
Copy after login

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.

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template