Yii2 ActiveRecord 添加额外属性

如果你的Form表单中的属性有部分不是在数据库中,而你的Mode又集成了ActiveRecord,这时想通过$model->load()加载那部分未在数据库定义的属性可以重写attributes()函数,例如:

/**
	 *
	 * {@inheritDoc}
	 *
	 * @see \common\db\ActiveRecord::attributes()
	 */
	public function attributes ()
	{
		$attributes = parent::attributes();
		$attributes[] = 'sync_take_rate';
		$attributes[] = 'sync_show_virtual';
		return $attributes;
	}

然后在attributeLabels()和attributeHints()中就和写其他属性设置label、hint一样了。

猜你喜欢

转载自niqingyang.iteye.com/blog/2271990