You can not select more than 25 topics
			Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
		
		
		
		
			
				
					158 lines
				
				4.2 KiB
			
		
		
			
		
	
	
					158 lines
				
				4.2 KiB
			| 
											12 years ago
										 | <?php
 | ||
| 
											12 years ago
										 | 
 | ||
| 
											12 years ago
										 | use yii\db\ActiveRecordInterface;
 | ||
| 
											12 years ago
										 | use yii\helpers\StringHelper;
 | ||
|  | 
 | ||
| 
											12 years ago
										 | /**
 | ||
| 
											12 years ago
										 |  * This is the template for generating a CRUD controller class file.
 | ||
|  |  *
 | ||
| 
											12 years ago
										 |  * @var yii\web\View $this
 | ||
| 
											12 years ago
										 |  * @var yii\gii\generators\crud\Generator $generator
 | ||
| 
											12 years ago
										 |  */
 | ||
| 
											12 years ago
										 | 
 | ||
| 
											12 years ago
										 | $controllerClass = StringHelper::basename($generator->controllerClass);
 | ||
|  | $modelClass = StringHelper::basename($generator->modelClass);
 | ||
|  | $searchModelClass = StringHelper::basename($generator->searchModelClass);
 | ||
| 
											12 years ago
										 | if ($modelClass === $searchModelClass) {
 | ||
|  | 	$searchModelAlias = $searchModelClass.'Search';
 | ||
|  | }
 | ||
| 
											12 years ago
										 | 
 | ||
| 
											12 years ago
										 | /** @var ActiveRecordInterface $class */
 | ||
|  | $class = $generator->modelClass;
 | ||
|  | $pks = $class::primaryKey();
 | ||
| 
											12 years ago
										 | $urlParams = $generator->generateUrlParams();
 | ||
|  | $actionParams = $generator->generateActionParams();
 | ||
|  | $actionParamComments = $generator->generateActionParamComments();
 | ||
| 
											12 years ago
										 | 
 | ||
| 
											12 years ago
										 | echo "<?php\n";
 | ||
|  | ?>
 | ||
|  | 
 | ||
| 
											12 years ago
										 | namespace <?= StringHelper::dirname(ltrim($generator->controllerClass, '\\')) ?>;
 | ||
| 
											12 years ago
										 | 
 | ||
| 
											12 years ago
										 | use <?= ltrim($generator->modelClass, '\\') ?>;
 | ||
| 
											12 years ago
										 | use <?= ltrim($generator->searchModelClass, '\\') ?><?php if (isset($searchModelAlias)):?> as <?= $searchModelAlias ?><?php endif ?>;
 | ||
| 
											12 years ago
										 | use <?= ltrim($generator->baseControllerClass, '\\') ?>;
 | ||
| 
											12 years ago
										 | use yii\web\NotFoundHttpException;
 | ||
| 
											12 years ago
										 | use yii\web\VerbFilter;
 | ||
| 
											12 years ago
										 | 
 | ||
| 
											12 years ago
										 | /**
 | ||
| 
											12 years ago
										 |  * <?= $controllerClass ?> implements the CRUD actions for <?= $modelClass ?> model.
 | ||
| 
											12 years ago
										 |  */
 | ||
| 
											12 years ago
										 | class <?= $controllerClass ?> extends <?= StringHelper::basename($generator->baseControllerClass) . "\n" ?>
 | ||
| 
											12 years ago
										 | {
 | ||
| 
											12 years ago
										 | 	public function behaviors()
 | ||
|  | 	{
 | ||
| 
											12 years ago
										 | 		return [
 | ||
|  | 			'verbs' => [
 | ||
| 
											12 years ago
										 | 				'class' => VerbFilter::className(),
 | ||
| 
											12 years ago
										 | 				'actions' => [
 | ||
|  | 					'delete' => ['post'],
 | ||
|  | 				],
 | ||
|  | 			],
 | ||
|  | 		];
 | ||
| 
											12 years ago
										 | 	}
 | ||
|  | 
 | ||
| 
											12 years ago
										 | 	/**
 | ||
| 
											12 years ago
										 | 	 * Lists all <?= $modelClass ?> models.
 | ||
| 
											12 years ago
										 | 	 * @return mixed
 | ||
|  | 	 */
 | ||
|  | 	public function actionIndex()
 | ||
|  | 	{
 | ||
| 
											12 years ago
										 | 		$searchModel = new <?= isset($searchModelAlias) ? $searchModelAlias : $searchModelClass ?>;
 | ||
| 
											12 years ago
										 | 		$dataProvider = $searchModel->search($_GET);
 | ||
|  | 
 | ||
| 
											12 years ago
										 | 		return $this->render('index', [
 | ||
| 
											12 years ago
										 | 			'dataProvider' => $dataProvider,
 | ||
| 
											12 years ago
										 | 			'searchModel' => $searchModel,
 | ||
| 
											12 years ago
										 | 		]);
 | ||
| 
											12 years ago
										 | 	}
 | ||
|  | 
 | ||
|  | 	/**
 | ||
| 
											12 years ago
										 | 	 * Displays a single <?= $modelClass ?> model.
 | ||
|  | 	 * <?= implode("\n\t * ", $actionParamComments) . "\n" ?>
 | ||
| 
											12 years ago
										 | 	 * @return mixed
 | ||
| 
											12 years ago
										 | 	 */
 | ||
| 
											12 years ago
										 | 	public function actionView(<?= $actionParams ?>)
 | ||
| 
											12 years ago
										 | 	{
 | ||
| 
											12 years ago
										 | 		return $this->render('view', [
 | ||
| 
											12 years ago
										 | 			'model' => $this->findModel(<?= $actionParams ?>),
 | ||
| 
											12 years ago
										 | 		]);
 | ||
| 
											12 years ago
										 | 	}
 | ||
|  | 
 | ||
|  | 	/**
 | ||
| 
											12 years ago
										 | 	 * Creates a new <?= $modelClass ?> model.
 | ||
| 
											12 years ago
										 | 	 * If creation is successful, the browser will be redirected to the 'view' page.
 | ||
| 
											12 years ago
										 | 	 * @return mixed
 | ||
| 
											12 years ago
										 | 	 */
 | ||
|  | 	public function actionCreate()
 | ||
|  | 	{
 | ||
| 
											12 years ago
										 | 		$model = new <?= $modelClass ?>;
 | ||
| 
											12 years ago
										 | 
 | ||
|  | 		if ($model->load($_POST) && $model->save()) {
 | ||
| 
											12 years ago
										 | 			return $this->redirect(['view', <?= $urlParams ?>]);
 | ||
| 
											12 years ago
										 | 		} else {
 | ||
| 
											12 years ago
										 | 			return $this->render('create', [
 | ||
| 
											12 years ago
										 | 				'model' => $model,
 | ||
| 
											12 years ago
										 | 			]);
 | ||
| 
											12 years ago
										 | 		}
 | ||
|  | 	}
 | ||
|  | 
 | ||
|  | 	/**
 | ||
| 
											12 years ago
										 | 	 * Updates an existing <?= $modelClass ?> model.
 | ||
| 
											12 years ago
										 | 	 * If update is successful, the browser will be redirected to the 'view' page.
 | ||
| 
											12 years ago
										 | 	 * <?= implode("\n\t * ", $actionParamComments) . "\n" ?>
 | ||
| 
											12 years ago
										 | 	 * @return mixed
 | ||
| 
											12 years ago
										 | 	 */
 | ||
| 
											12 years ago
										 | 	public function actionUpdate(<?= $actionParams ?>)
 | ||
| 
											12 years ago
										 | 	{
 | ||
| 
											12 years ago
										 | 		$model = $this->findModel(<?= $actionParams ?>);
 | ||
| 
											12 years ago
										 | 
 | ||
|  | 		if ($model->load($_POST) && $model->save()) {
 | ||
| 
											12 years ago
										 | 			return $this->redirect(['view', <?= $urlParams ?>]);
 | ||
| 
											12 years ago
										 | 		} else {
 | ||
| 
											12 years ago
										 | 			return $this->render('update', [
 | ||
| 
											12 years ago
										 | 				'model' => $model,
 | ||
| 
											12 years ago
										 | 			]);
 | ||
| 
											12 years ago
										 | 		}
 | ||
|  | 	}
 | ||
|  | 
 | ||
|  | 	/**
 | ||
| 
											12 years ago
										 | 	 * Deletes an existing <?= $modelClass ?> model.
 | ||
| 
											12 years ago
										 | 	 * If deletion is successful, the browser will be redirected to the 'index' page.
 | ||
| 
											12 years ago
										 | 	 * <?= implode("\n\t * ", $actionParamComments) . "\n" ?>
 | ||
| 
											12 years ago
										 | 	 * @return mixed
 | ||
| 
											12 years ago
										 | 	 */
 | ||
| 
											12 years ago
										 | 	public function actionDelete(<?= $actionParams ?>)
 | ||
| 
											12 years ago
										 | 	{
 | ||
| 
											12 years ago
										 | 		$this->findModel(<?= $actionParams ?>)->delete();
 | ||
| 
											12 years ago
										 | 		return $this->redirect(['index']);
 | ||
| 
											12 years ago
										 | 	}
 | ||
|  | 
 | ||
|  | 	/**
 | ||
| 
											12 years ago
										 | 	 * Finds the <?= $modelClass ?> model based on its primary key value.
 | ||
| 
											12 years ago
										 | 	 * If the model is not found, a 404 HTTP exception will be thrown.
 | ||
| 
											12 years ago
										 | 	 * <?= implode("\n\t * ", $actionParamComments) . "\n" ?>
 | ||
|  | 	 * @return <?= $modelClass ?> the loaded model
 | ||
| 
											12 years ago
										 | 	 * @throws NotFoundHttpException if the model cannot be found
 | ||
| 
											12 years ago
										 | 	 */
 | ||
| 
											12 years ago
										 | 	protected function findModel(<?= $actionParams ?>)
 | ||
| 
											12 years ago
										 | 	{
 | ||
| 
											12 years ago
										 | <?php
 | ||
|  | if (count($pks) === 1) {
 | ||
|  | 	$condition = '$id';
 | ||
|  | } else {
 | ||
| 
											12 years ago
										 | 	$condition = [];
 | ||
| 
											12 years ago
										 | 	foreach ($pks as $pk) {
 | ||
|  | 		$condition[] = "'$pk' => \$$pk";
 | ||
| 
											12 years ago
										 | 	}
 | ||
| 
											12 years ago
										 | 	$condition = '[' . implode(', ', $condition) . ']';
 | ||
| 
											12 years ago
										 | }
 | ||
|  | ?>
 | ||
| 
											12 years ago
										 | 		if (($model = <?= $modelClass ?>::find(<?= $condition ?>)) !== null) {
 | ||
| 
											12 years ago
										 | 			return $model;
 | ||
|  | 		} else {
 | ||
| 
											12 years ago
										 | 			throw new NotFoundHttpException('The requested page does not exist.');
 | ||
| 
											12 years ago
										 | 		}
 | ||
|  | 	}
 | ||
|  | }
 |