yiiapp/protected/controllers/TbUserController.php TbUser控制器模块

<?php

//使用GII自动生成的一个TbUserController
class TbUserController extends Controller
{
	/**
	 * @var string the default layout for the views. Defaults to '//layouts/column2', meaning
	 * using two-column layout. See 'protected/views/layouts/column2.php'.
	 * 
	 * @ VAR字符串的默认布局的意见。默认为'/ / layouts/column2',这意味着
     * 使用两栏布局。请参阅“protected/views/layouts/column2.php”。
	 */
	public $layout='//layouts/column2';

	/**
	 * @return array action filters 返回一个数且字段
	 */
	public function filters()
	{
		return array(
			'accessControl', // perform access control for CRUD operations
		    //执行CRUD操作的访问控制
		    
		
			'postOnly + delete', // we only allow deletion via POST request
		    //我们只允许通过POST请求删除
		);
	}

	/**
	 * Specifies the access control rules.
	 * This method is used by the 'accessControl' filter.
	 * @return array access control rules
	 * *指定的访问控制规则。
	 * 此方法用于由“AccessControl的”过滤器。
	 * @返回数组的访问控制规则
	 */
	public function accessRules()
	{
		return array(
			//允许所有用户进行“索引”和“视图”行动
			array('allow',  // allow all users to perform 'index' and 'view' actions
				'actions'=>array('index','view'),
				'users'=>array('*'),
			),
			//允许认证的用户执行“创造”和“更新”行动
			array('allow', // allow authenticated user to perform 'create' and 'update' actions
				'actions'=>array('create','update'),
				'users'=>array('@'),
			),
			//允许管理员用户执行“admin”和“删除”行动
			array('allow', // allow admin user to perform 'admin' and 'delete' actions
				'actions'=>array('admin','delete'),
				'users'=>array('admin'),
			),
			array('deny',  // deny all users
				'users'=>array('*'),
			),
		);
	}

	/**
	 * Displays a particular model.
	 * @param integer $id the ID of the model to be displayed
	 * *显示一个特定的模式。
	 * @参数整数ID的ID要显示的模型
	 * 
	 */
	public function actionView($id)
	{
		$this->render('view',array(
			'model'=>$this->loadModel($id),
		));
	}

	/**
	 * Creates a new model.
	 * If creation is successful, the browser will be redirected to the 'view' page.
	 * 创建一个新的模式。
     * 如果创建成功,浏览器将被重定向到“视图”页面。
	 */
	public function actionCreate()
	{
		$model=new TbUser; //对象

		// Uncomment the following line if AJAX validation is needed
		// 取消对以下行,如果需要AJAX验证
		// $this->performAjaxValidation($model);

		//如果是提交数据
		if(isset($_POST['TbUser']))
		{
			$model->attributes=$_POST['TbUser'];
			if($model->save())
				$this->redirect(array('view','id'=>$model->id));
		}

		//创建提交的表单信息
		$this->render('create',array(
			'model'=>$model,
		));
	}

	/**
	 * Updates a particular model.
	 * If update is successful, the browser will be redirected to the 'view' page.
	 * @param integer $id the ID of the model to be updated
	 * *更新一个特定的模式。
     * 如果更新成功,浏览器将被重定向到“视图”页面。
     * @参数整数ID的ID对模型进行更新
	 */
	public function actionUpdate($id)
	{
		$model=$this->loadModel($id);
		
		// Uncomment the following line if AJAX validation is needed
		// 取消对以下行,如果需要AJAX验证
		// $this->performAjaxValidation($model);

		if(isset($_POST['TbUser']))
		{
			$model->attributes=$_POST['TbUser'];
			if($model->save())
				$this->redirect(array('view','id'=>$model->id));
		}

		$this->render('update',array(
			'model'=>$model,
		));
	}

	/**
	 * Deletes a particular model.
	 * If deletion is successful, the browser will be redirected to the 'admin' page.
	 * 删除某型号。
     * 如果删除成功,浏览器将被重定向到“管理”页面。
	 * @param integer $id the ID of the model to be deleted
	 */
	public function actionDelete($id)
	{
		$this->loadModel($id)->delete();

		// if AJAX request (triggered by deletion via admin grid view), we should not redirect the browser
		// 如果AJAX请求(触发删除通过管理网格视图),我们不应该将浏览器重定向
		if(!isset($_GET['ajax']))
			$this->redirect(isset($_POST['returnUrl']) ? $_POST['returnUrl'] : array('admin'));
	}

	/**
	 * Lists all models.
	 * 自动生成的首页显示 列表显示
	 */
	public function actionIndex()
	{
		echo "<h1>actionIndex</h1>";
		//创建一个CActiveDataProvider()对象,并传入了数据表名
		$dataProvider=new CActiveDataProvider('TbUser');
		//var_dump($dataProvider);
		//这里就开始显示了,加载index
		$this->render('index',array(
			'dataProvider'=>$dataProvider,
		));
	}

	/**
	 * Manages all models. 管理所有Models
	 */
	public function actionAdmin()
	{
		echo "<h1>adminAdmin</h1>";
		
		$model=new TbUser('search'); //实例化一个model,暂时没弄懂这个参数search是如何传递给TbUser对象的
		//总算弄明白了,这里的search参数是指访问TbUser里面的一个search成员
		$model->unsetAttributes();  // clear any default values
		if(isset($_GET['TbUser']))
			$model->attributes=$_GET['TbUser'];

		$this->render('admin',array(
			'model'=>$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 $id the ID of the model to be loaded
	 * @return TbUser the loaded model
	 * @throws CHttpException
	 * *返回根据在GET变量主键的数据模型。
	 * 如果没有发现数据模型,HTTP,将引发异常。
	 * @参数整数ID的ID要加载的模型
	 * @返回TbUser加载模型
	 * @抛出CHttpException的
	 */
	public function loadModel($id)
	{
		//动态加载TbUser::model()动态加载指定类,访问他的findByPk($id)函数
		$model=TbUser::model()->findByPk($id);
		if($model===null)
			throw new CHttpException(404,'The requested page does not exist.请求的页面不存在');
		return $model;
	}

	/**
	 * Performs the AJAX validation.
	 * @param TbUser $model the model to be validated
	 * 执行AJAX验证。
     * 参数:TbUser - $模型的模型进行验证
	 */
	protected function performAjaxValidation($model)
	{
		if(isset($_POST['ajax']) && $_POST['ajax']==='tb-user-form')
		{
			echo CActiveForm::validate($model);
			Yii::app()->end();
		}
	}
}

  

posted @ 2013-06-22 17:34  简单--生活  阅读(304)  评论(0)    收藏  举报
简单--生活(CSDN)