2011-06-15 00:19:12 0 评论 Yii 1.0 Boy.Lee

Yii 1.0中为模型设置场景

无论新建模型还是更新模型都可以轻松的添加场景,之后在验证中使用场景。

//create new
$user =  new User();
$user->setScenario('create');

//load exist
$user = $this->loadModel($id);
$user->setScenario('updatePass');


//load Model
/**
 * Returns the data model based on the primary key given in the GET variable.
 * If the data model is not found, an HTTP exception will be raised.
 * @param integer the ID of the model to be loaded
 */
public function loadModel($id)
{
	$model=User::model()->findByPk((int)$id);
	if($model===null)
		throw new CHttpException(404,'The requested page does not exist.');
	return $model;
}