<?php
/**
 * @link      http://www.u-bo.com
 * @copyright 南京友博网络科技有限公司 
 * @license   http://www.u-bo.com/license/
 */

namespace qcbase\actions;

use Yii;

/**
 * 后台公用的新增动作类
 * 
 * @author legendjw http://blog.u-bo.com
 * @since 0.1
 */
class Create extends \yii\base\Action
{
	/**
	 * @var class 当前操作所属模型
	 */
    public $model; 
	/**
	 * @var string 当前操作视图名称
	 */
    public $view = 'create';
	/**
	 * @var string 模型的场景 
	 */
	public $scenario = 'default';

 
    public function run()
    {
		$this->model === null && $this->model = $this->controller->model;
		$this->model = new $this->model(['scenario' => $this->scenario]);

		if ($this->model->load(Yii::$app->request->post())) {
             
			if ($this->model->save()) {
               
				Yii::$app->getSession()->setFlash('success', '新增成功');
				return $this->controller->flash(['url' => [$this->controller->id.'/create']]);
			}
            
//			elseif($this->model->hasErrors()) {
//               
//				$firstError = reset($this->model->getFirstErrors());
//				Yii::$app->getSession()->setFlash('error', $firstError);
//                return $this->controller->flash();
//			}
		
		}

        return $this->controller->render($this->view, [
			'model' => $this->model,
			'action' => [$this->controller->id.'/create'],
		]);
    } 
		
}
