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