Qiang Xue
11 years ago
9 changed files with 256 additions and 254 deletions
@ -1,180 +1,152 @@
|
||||
<?php |
||||
|
||||
use yii\helpers\StringHelper; |
||||
|
||||
/** |
||||
* This is the template for generating a controller class file for CRUD feature. |
||||
* The following variables are available in this template: |
||||
* - $this: the CrudCode object |
||||
* This is the template for generating a CRUD controller class file. |
||||
* |
||||
* @var yii\base\View $this |
||||
* @var yii\gii\generators\crud\Generator $generator |
||||
*/ |
||||
|
||||
$pos = strrpos($generator->controllerClass, '\\'); |
||||
$ns = ltrim(substr($generator->controllerClass, 0, $pos), '\\'); |
||||
$controllerClass = substr($generator->controllerClass, $pos + 1); |
||||
$pos = strrpos($generator->modelClass, '\\'); |
||||
$modelClass = $pos === false ? $generator->modelClass : substr($generator->modelClass, $pos + 1); |
||||
|
||||
/** @var \yii\db\ActiveRecord $class */ |
||||
$class = $generator->modelClass; |
||||
$pks = $class::primaryKey(); |
||||
$schema = $class::getTableSchema(); |
||||
if (count($pks) === 1) { |
||||
$ids = '$id'; |
||||
$params = "array('id' => \$model->{$pks[0]})"; |
||||
$paramComments = '@param ' . $schema->columns[$pks[0]]->phpType . ' $id'; |
||||
} else { |
||||
$ids = '$' . implode(', $', $pks); |
||||
$params = array(); |
||||
$paramComments = array(); |
||||
foreach ($pks as $pk) { |
||||
$paramComments[] = '@param ' . $schema->columns[$pk]->phpType . ' $' . $pk; |
||||
$params[] = "'$pk' => \$model->$pk"; |
||||
} |
||||
$params = implode(', ', $params); |
||||
$paramComments = implode("\n\t * ", $paramComments); |
||||
} |
||||
|
||||
echo "<?php\n";
|
||||
?> |
||||
<?php echo "<?php\n"; ?> |
||||
|
||||
class <?php echo $this->controllerClass; ?> extends <?php echo $this->baseControllerClass."\n"; ?> |
||||
{ |
||||
/** |
||||
* @var string the default layout for the views. Defaults to '//layouts/column2', meaning |
||||
* using two-column layout. See 'protected/views/layouts/column2.php'. |
||||
*/ |
||||
public $layout='//layouts/column2'; |
||||
namespace <?php echo $ns; ?>;
|
||||
|
||||
/** |
||||
* @return array action filters |
||||
*/ |
||||
public function filters() |
||||
{ |
||||
return array( |
||||
'accessControl', // perform access control for CRUD operations |
||||
'postOnly + delete', // we only allow deletion via POST request |
||||
); |
||||
} |
||||
use <?php echo ltrim($generator->modelClass, '\\'); ?>;
|
||||
use yii\data\ActiveDataProvider; |
||||
use <?php echo ltrim($generator->baseControllerClass, '\\'); ?>;
|
||||
use yii\web\HttpException; |
||||
|
||||
/** |
||||
* Specifies the access control rules. |
||||
* This method is used by the 'accessControl' filter. |
||||
* @return array access control rules |
||||
/** |
||||
* <?php echo $controllerClass; ?> implements the CRUD actions for <?php echo $modelClass; ?> model.
|
||||
*/ |
||||
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('@'), |
||||
), |
||||
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('*'), |
||||
), |
||||
); |
||||
} |
||||
|
||||
class <?php echo $controllerClass; ?> extends <?php echo StringHelper::basename($generator->baseControllerClass) . "\n"; ?> |
||||
{ |
||||
/** |
||||
* Displays a particular model. |
||||
* @param integer $id the ID of the model to be displayed |
||||
* Displays a single <?php echo $modelClass; ?> model.
|
||||
* <?php echo $paramComments . "\n"; ?> |
||||
* @return mixed |
||||
*/ |
||||
public function actionView($id) |
||||
public function actionView(<?php echo $ids; ?>)
|
||||
{ |
||||
$this->render('view',array( |
||||
'model'=>$this->loadModel($id), |
||||
return $this->render('view', array( |
||||
'model' => $this->findModel(<?php echo $ids; ?>),
|
||||
)); |
||||
} |
||||
|
||||
/** |
||||
* Creates a new model. |
||||
* Creates a new <?php echo $modelClass; ?> model.
|
||||
* If creation is successful, the browser will be redirected to the 'view' page. |
||||
* @return mixed |
||||
*/ |
||||
public function actionCreate() |
||||
{ |
||||
$model=new <?php echo $this->modelClass; ?>;
|
||||
|
||||
// Uncomment the following line if AJAX validation is needed |
||||
// $this->performAjaxValidation($model); |
||||
|
||||
if(isset($_POST['<?php echo $this->modelClass; ?>']))
|
||||
{ |
||||
$model->attributes=$_POST['<?php echo $this->modelClass; ?>'];
|
||||
if($model->save()) |
||||
$this->redirect(array('view','id'=>$model-><?php echo $this->tableSchema->primaryKey; ?>));
|
||||
} |
||||
$model = new <?php echo $modelClass; ?>;
|
||||
|
||||
$this->render('create',array( |
||||
'model'=>$model, |
||||
if ($model->load($_POST) && $model->save()) { |
||||
return $this->redirect(array('view', <?php echo $params; ?>));
|
||||
} else { |
||||
return $this->render('create', array( |
||||
'model' => $model, |
||||
)); |
||||
} |
||||
} |
||||
|
||||
/** |
||||
* Updates a particular model. |
||||
* Updates an existing <?php echo $modelClass; ?> 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 |
||||
* <?php echo $paramComments . "\n"; ?> |
||||
* @return mixed |
||||
*/ |
||||
public function actionUpdate($id) |
||||
public function actionUpdate(<?php echo $ids; ?>)
|
||||
{ |
||||
$model=$this->loadModel($id); |
||||
$model = $this->findModel(<?php echo $ids; ?>);
|
||||
|
||||
// Uncomment the following line if AJAX validation is needed |
||||
// $this->performAjaxValidation($model); |
||||
|
||||
if(isset($_POST['<?php echo $this->modelClass; ?>']))
|
||||
{ |
||||
$model->attributes=$_POST['<?php echo $this->modelClass; ?>'];
|
||||
if($model->save()) |
||||
$this->redirect(array('view','id'=>$model-><?php echo $this->tableSchema->primaryKey; ?>));
|
||||
} |
||||
|
||||
$this->render('update',array( |
||||
'model'=>$model, |
||||
if ($model->load($_POST) && $model->save()) { |
||||
return $this->redirect(array('view', <?php echo $params; ?>));
|
||||
} else { |
||||
return $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 |
||||
* Deletes an existing <?php echo $modelClass; ?> model.
|
||||
* If deletion is successful, the browser will be redirected to the 'index' page. |
||||
* <?php echo $paramComments . "\n"; ?> |
||||
* @return mixed |
||||
*/ |
||||
public function actionDelete($id) |
||||
public function actionDelete(<?php echo $ids; ?>)
|
||||
{ |
||||
$this->loadModel($id)->delete(); |
||||
|
||||
// if AJAX request (triggered by deletion via admin grid view), we should not redirect the browser |
||||
if(!isset($_GET['ajax'])) |
||||
$this->redirect(isset($_POST['returnUrl']) ? $_POST['returnUrl'] : array('admin')); |
||||
$this->findModel(<?php echo $ids; ?>)->delete();
|
||||
return $this->redirect(array('index')); |
||||
} |
||||
|
||||
/** |
||||
* Lists all models. |
||||
* @return mixed |
||||
*/ |
||||
public function actionIndex() |
||||
{ |
||||
$dataProvider=new CActiveDataProvider('<?php echo $this->modelClass; ?>');
|
||||
$this->render('index',array( |
||||
'dataProvider'=>$dataProvider, |
||||
$dataProvider = new ActiveDataProvider('<?php echo $modelClass; ?>');
|
||||
return $this->render('index', array( |
||||
'dataProvider' => $dataProvider, |
||||
)); |
||||
} |
||||
|
||||
/** |
||||
* Manages all models. |
||||
* Returns the data model based on its primary key value. |
||||
* If the data model is not found, a 404 HTTP exception will be thrown. |
||||
* <?php echo $paramComments . "\n"; ?> |
||||
* @return <?php echo $modelClass; ?> the loaded model
|
||||
* @throws HttpException if the model cannot be found |
||||
*/ |
||||
public function actionAdmin() |
||||
protected function findModel(<?php echo $ids; ?>)
|
||||
{ |
||||
$model=new <?php echo $this->modelClass; ?>('search');
|
||||
$model->unsetAttributes(); // clear any default values |
||||
if(isset($_GET['<?php echo $this->modelClass; ?>']))
|
||||
$model->attributes=$_GET['<?php echo $this->modelClass; ?>'];
|
||||
|
||||
$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 <?php echo $this->modelClass; ?> the loaded model
|
||||
* @throws CHttpException |
||||
*/ |
||||
public function loadModel($id) |
||||
{ |
||||
$model=<?php echo $this->modelClass; ?>::model()->findByPk($id);
|
||||
if($model===null) |
||||
throw new CHttpException(404,'The requested page does not exist.'); |
||||
return $model; |
||||
<?php |
||||
if (count($pks) === 1) { |
||||
$condition = '$id'; |
||||
} else { |
||||
$condition = array(); |
||||
foreach ($pks as $pk) { |
||||
$condition[] = "'$pk' => \$$pk"; |
||||
} |
||||
|
||||
/** |
||||
* Performs the AJAX validation. |
||||
* @param <?php echo $this->modelClass; ?> $model the model to be validated
|
||||
*/ |
||||
protected function performAjaxValidation($model) |
||||
{ |
||||
if(isset($_POST['ajax']) && $_POST['ajax']==='<?php echo $this->class2id($this->modelClass); ?>-form')
|
||||
{ |
||||
echo CActiveForm::validate($model); |
||||
Yii::app()->end(); |
||||
$condition = 'array(' . implode(', ', $condition) . ')'; |
||||
} |
||||
?> |
||||
$model = <?php echo $modelClass; ?>::find(<?php echo $condition; ?>);
|
||||
if ($model === null) { |
||||
throw new HttpException(404, 'The requested page does not exist.'); |
||||
} |
||||
return $model; |
||||
} |
||||
} |
||||
|
@ -1,27 +1,31 @@
|
||||
<?php |
||||
|
||||
use yii\helpers\Inflector; |
||||
use yii\helpers\StringHelper; |
||||
|
||||
/** |
||||
* The following variables are available in this template: |
||||
* - $this: the CrudCode object |
||||
* @var yii\base\View $this |
||||
* @var yii\gii\generators\crud\Generator $generator |
||||
*/ |
||||
?> |
||||
<?php echo "<?php\n"; ?> |
||||
/* @var $this <?php echo $this->getControllerClass(); ?> */
|
||||
/* @var $model <?php echo $this->getModelClass(); ?> */
|
||||
|
||||
<?php |
||||
$label=$this->pluralize($this->class2name($this->modelClass)); |
||||
echo "\$this->breadcrumbs=array( |
||||
'$label'=>array('index'), |
||||
'Create', |
||||
);\n"; |
||||
echo "<?php\n";
|
||||
?> |
||||
|
||||
$this->menu=array( |
||||
array('label'=>'List <?php echo $this->modelClass; ?>', 'url'=>array('index')),
|
||||
array('label'=>'Manage <?php echo $this->modelClass; ?>', 'url'=>array('admin')),
|
||||
); |
||||
use yii\helpers\Html; |
||||
|
||||
/** |
||||
* @var yii\base\View $this |
||||
* @var <?php echo ltrim($generator->modelClass, '\\'); ?> $model
|
||||
*/ |
||||
|
||||
$this->title = 'Create <?php echo Inflector::camel2words(StringHelper::basename($generator->modelClass)); ?>';
|
||||
?> |
||||
<div class="<?php echo Inflector::camel2id(StringHelper::basename($generator->modelClass)); ?>-create">
|
||||
|
||||
<h1><?php echo "<?php"; ?> echo Html::encode($this->title); ?></h1>
|
||||
|
||||
<h1>Create <?php echo $this->modelClass; ?></h1>
|
||||
<?php echo "<?php"; ?> echo $this->render('_form', array(
|
||||
'model' => $model, |
||||
)); ?> |
||||
|
||||
<?php echo "<?php \$this->renderPartial('_form', array('model'=>\$model)); ?>"; ?> |
||||
</div> |
||||
|
@ -1,31 +1,31 @@
|
||||
<?php |
||||
|
||||
use yii\helpers\Inflector; |
||||
use yii\helpers\StringHelper; |
||||
|
||||
/** |
||||
* The following variables are available in this template: |
||||
* - $this: the CrudCode object |
||||
* @var yii\base\View $this |
||||
* @var yii\gii\generators\crud\Generator $generator |
||||
*/ |
||||
?> |
||||
<?php echo "<?php\n"; ?> |
||||
/* @var $this <?php echo $this->getControllerClass(); ?> */
|
||||
/* @var $model <?php echo $this->getModelClass(); ?> */
|
||||
|
||||
<?php |
||||
$nameColumn=$this->guessNameColumn($this->tableSchema->columns); |
||||
$label=$this->pluralize($this->class2name($this->modelClass)); |
||||
echo "\$this->breadcrumbs=array( |
||||
'$label'=>array('index'), |
||||
\$model->{$nameColumn}=>array('view','id'=>\$model->{$this->tableSchema->primaryKey}), |
||||
'Update', |
||||
);\n"; |
||||
echo "<?php\n";
|
||||
?> |
||||
|
||||
$this->menu=array( |
||||
array('label'=>'List <?php echo $this->modelClass; ?>', 'url'=>array('index')),
|
||||
array('label'=>'Create <?php echo $this->modelClass; ?>', 'url'=>array('create')),
|
||||
array('label'=>'View <?php echo $this->modelClass; ?>', 'url'=>array('view', 'id'=>$model-><?php echo $this->tableSchema->primaryKey; ?>)),
|
||||
array('label'=>'Manage <?php echo $this->modelClass; ?>', 'url'=>array('admin')),
|
||||
); |
||||
use yii\helpers\Html; |
||||
|
||||
/** |
||||
* @var yii\base\View $this |
||||
* @var <?php echo ltrim($generator->modelClass, '\\'); ?> $model
|
||||
*/ |
||||
|
||||
$this->title = 'Modify <?php echo Inflector::camel2words(StringHelper::basename($generator->modelClass)); ?>: ' . $model-><?php echo $generator->getNameAttribute(); ?>;
|
||||
?> |
||||
<div class="<?php echo Inflector::camel2id(StringHelper::basename($generator->modelClass)); ?>-update">
|
||||
|
||||
<h1><?php echo "<?php"; ?> echo Html::encode($this->title); ?></h1>
|
||||
|
||||
<h1>Update <?php echo $this->modelClass." <?php echo \$model->{$this->tableSchema->primaryKey}; ?>"; ?></h1>
|
||||
<?php echo "<?php"; ?> echo $this->render('_form', array(
|
||||
'model' => $model, |
||||
)); ?> |
||||
|
||||
<?php echo "<?php \$this->renderPartial('_form', array('model'=>\$model)); ?>"; ?> |
||||
</div> |
||||
|
@ -1,39 +1,27 @@
|
||||
<?php |
||||
|
||||
use yii\helpers\Inflector; |
||||
use yii\helpers\StringHelper; |
||||
|
||||
/** |
||||
* The following variables are available in this template: |
||||
* - $this: the CrudCode object |
||||
* @var yii\base\View $this |
||||
* @var yii\gii\generators\crud\Generator $generator |
||||
*/ |
||||
?> |
||||
<?php echo "<?php\n"; ?> |
||||
/* @var $this <?php echo $this->getControllerClass(); ?> */
|
||||
/* @var $model <?php echo $this->getModelClass(); ?> */
|
||||
|
||||
<?php |
||||
$nameColumn=$this->guessNameColumn($this->tableSchema->columns); |
||||
$label=$this->pluralize($this->class2name($this->modelClass)); |
||||
echo "\$this->breadcrumbs=array( |
||||
'$label'=>array('index'), |
||||
\$model->{$nameColumn}, |
||||
);\n"; |
||||
echo "<?php\n";
|
||||
?> |
||||
|
||||
$this->menu=array( |
||||
array('label'=>'List <?php echo $this->modelClass; ?>', 'url'=>array('index')),
|
||||
array('label'=>'Create <?php echo $this->modelClass; ?>', 'url'=>array('create')),
|
||||
array('label'=>'Update <?php echo $this->modelClass; ?>', 'url'=>array('update', 'id'=>$model-><?php echo $this->tableSchema->primaryKey; ?>)),
|
||||
array('label'=>'Delete <?php echo $this->modelClass; ?>', 'url'=>'#', 'linkOptions'=>array('submit'=>array('delete','id'=>$model-><?php echo $this->tableSchema->primaryKey; ?>),'confirm'=>'Are you sure you want to delete this item?')),
|
||||
array('label'=>'Manage <?php echo $this->modelClass; ?>', 'url'=>array('admin')),
|
||||
); |
||||
?> |
||||
use yii\helpers\Html; |
||||
|
||||
<h1>View <?php echo $this->modelClass." #<?php echo \$model->{$this->tableSchema->primaryKey}; ?>"; ?></h1>
|
||||
/** |
||||
* @var yii\base\View $this |
||||
* @var <?php echo ltrim($generator->modelClass, '\\'); ?> $model
|
||||
*/ |
||||
|
||||
<?php echo "<?php"; ?> $this->widget('zii.widgets.CDetailView', array(
|
||||
'data'=>$model, |
||||
'attributes'=>array( |
||||
<?php |
||||
foreach($this->tableSchema->columns as $column) |
||||
echo "\t\t'".$column->name."',\n"; |
||||
$this->title = $model-><?php echo $generator->getNameAttribute(); ?>;
|
||||
?> |
||||
), |
||||
)); ?> |
||||
<div class="<?php echo Inflector::camel2id(StringHelper::basename($generator->modelClass)); ?>-view">
|
||||
|
||||
<h1><?php echo "<?php"; ?> echo Html::encode($this->title); ?></h1>
|
||||
|
||||
</div> |
||||
|
Loading…
Reference in new issue