Qiang Xue
11 years ago
9 changed files with 256 additions and 254 deletions
@ -1,180 +1,152 @@ |
|||||||
<?php |
<?php |
||||||
|
|
||||||
|
use yii\helpers\StringHelper; |
||||||
|
|
||||||
/** |
/** |
||||||
* This is the template for generating a controller class file for CRUD feature. |
* This is the template for generating a CRUD controller class file. |
||||||
* 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"; ?> |
|
||||||
|
|
||||||
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'; |
|
||||||
|
|
||||||
/** |
$pos = strrpos($generator->controllerClass, '\\'); |
||||||
* @return array action filters |
$ns = ltrim(substr($generator->controllerClass, 0, $pos), '\\'); |
||||||
*/ |
$controllerClass = substr($generator->controllerClass, $pos + 1); |
||||||
public function filters() |
$pos = strrpos($generator->modelClass, '\\'); |
||||||
{ |
$modelClass = $pos === false ? $generator->modelClass : substr($generator->modelClass, $pos + 1); |
||||||
return array( |
|
||||||
'accessControl', // perform access control for CRUD operations |
/** @var \yii\db\ActiveRecord $class */ |
||||||
'postOnly + delete', // we only allow deletion via POST request |
$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";
|
||||||
* Specifies the access control rules. |
?> |
||||||
* This method is used by the 'accessControl' filter. |
|
||||||
* @return array access control rules |
namespace <?php echo $ns; ?>;
|
||||||
*/ |
|
||||||
public function accessRules() |
use <?php echo ltrim($generator->modelClass, '\\'); ?>;
|
||||||
{ |
use yii\data\ActiveDataProvider; |
||||||
return array( |
use <?php echo ltrim($generator->baseControllerClass, '\\'); ?>;
|
||||||
array('allow', // allow all users to perform 'index' and 'view' actions |
use yii\web\HttpException; |
||||||
'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('*'), |
|
||||||
), |
|
||||||
); |
|
||||||
} |
|
||||||
|
|
||||||
|
/** |
||||||
|
* <?php echo $controllerClass; ?> implements the CRUD actions for <?php echo $modelClass; ?> model.
|
||||||
|
*/ |
||||||
|
class <?php echo $controllerClass; ?> extends <?php echo StringHelper::basename($generator->baseControllerClass) . "\n"; ?> |
||||||
|
{ |
||||||
/** |
/** |
||||||
* Displays a particular model. |
* Displays a single <?php echo $modelClass; ?> model.
|
||||||
* @param integer $id the ID of the model to be displayed |
* <?php echo $paramComments . "\n"; ?> |
||||||
|
* @return mixed |
||||||
*/ |
*/ |
||||||
public function actionView($id) |
public function actionView(<?php echo $ids; ?>)
|
||||||
{ |
{ |
||||||
$this->render('view',array( |
return $this->render('view', array( |
||||||
'model'=>$this->loadModel($id), |
'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. |
* If creation is successful, the browser will be redirected to the 'view' page. |
||||||
|
* @return mixed |
||||||
*/ |
*/ |
||||||
public function actionCreate() |
public function actionCreate() |
||||||
{ |
{ |
||||||
$model=new <?php echo $this->modelClass; ?>;
|
$model = new <?php echo $modelClass; ?>;
|
||||||
|
|
||||||
// Uncomment the following line if AJAX validation is needed |
if ($model->load($_POST) && $model->save()) { |
||||||
// $this->performAjaxValidation($model); |
return $this->redirect(array('view', <?php echo $params; ?>));
|
||||||
|
} else { |
||||||
if(isset($_POST['<?php echo $this->modelClass; ?>']))
|
return $this->render('create', array( |
||||||
{ |
'model' => $model, |
||||||
$model->attributes=$_POST['<?php echo $this->modelClass; ?>'];
|
)); |
||||||
if($model->save()) |
|
||||||
$this->redirect(array('view','id'=>$model-><?php echo $this->tableSchema->primaryKey; ?>));
|
|
||||||
} |
} |
||||||
|
|
||||||
$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. |
* 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 |
if ($model->load($_POST) && $model->save()) { |
||||||
// $this->performAjaxValidation($model); |
return $this->redirect(array('view', <?php echo $params; ?>));
|
||||||
|
} else { |
||||||
if(isset($_POST['<?php echo $this->modelClass; ?>']))
|
return $this->render('update', array( |
||||||
{ |
'model' => $model, |
||||||
$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, |
|
||||||
)); |
|
||||||
} |
} |
||||||
|
|
||||||
/** |
/** |
||||||
* Deletes a particular model. |
* Deletes an existing <?php echo $modelClass; ?> model.
|
||||||
* If deletion is successful, the browser will be redirected to the 'admin' page. |
* If deletion is successful, the browser will be redirected to the 'index' page. |
||||||
* @param integer $id the ID of the model to be deleted |
* <?php echo $paramComments . "\n"; ?> |
||||||
|
* @return mixed |
||||||
*/ |
*/ |
||||||
public function actionDelete($id) |
public function actionDelete(<?php echo $ids; ?>)
|
||||||
{ |
{ |
||||||
$this->loadModel($id)->delete(); |
$this->findModel(<?php echo $ids; ?>)->delete();
|
||||||
|
return $this->redirect(array('index')); |
||||||
// 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')); |
|
||||||
} |
} |
||||||
|
|
||||||
/** |
/** |
||||||
* Lists all models. |
* Lists all models. |
||||||
|
* @return mixed |
||||||
*/ |
*/ |
||||||
public function actionIndex() |
public function actionIndex() |
||||||
{ |
{ |
||||||
$dataProvider=new CActiveDataProvider('<?php echo $this->modelClass; ?>');
|
$dataProvider = new ActiveDataProvider('<?php echo $modelClass; ?>');
|
||||||
$this->render('index',array( |
return $this->render('index', array( |
||||||
'dataProvider'=>$dataProvider, |
'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');
|
<?php |
||||||
$model->unsetAttributes(); // clear any default values |
if (count($pks) === 1) { |
||||||
if(isset($_GET['<?php echo $this->modelClass; ?>']))
|
$condition = '$id'; |
||||||
$model->attributes=$_GET['<?php echo $this->modelClass; ?>'];
|
} else { |
||||||
|
$condition = array(); |
||||||
$this->render('admin',array( |
foreach ($pks as $pk) { |
||||||
'model'=>$model, |
$condition[] = "'$pk' => \$$pk"; |
||||||
)); |
|
||||||
} |
|
||||||
|
|
||||||
/** |
|
||||||
* 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; |
|
||||||
} |
} |
||||||
|
$condition = 'array(' . implode(', ', $condition) . ')'; |
||||||
/** |
} |
||||||
* Performs the AJAX validation. |
?> |
||||||
* @param <?php echo $this->modelClass; ?> $model the model to be validated
|
$model = <?php echo $modelClass; ?>::find(<?php echo $condition; ?>);
|
||||||
*/ |
if ($model === null) { |
||||||
protected function performAjaxValidation($model) |
throw new HttpException(404, 'The requested page does not exist.'); |
||||||
{ |
|
||||||
if(isset($_POST['ajax']) && $_POST['ajax']==='<?php echo $this->class2id($this->modelClass); ?>-form')
|
|
||||||
{ |
|
||||||
echo CActiveForm::validate($model); |
|
||||||
Yii::app()->end(); |
|
||||||
} |
} |
||||||
|
return $model; |
||||||
} |
} |
||||||
} |
} |
||||||
|
@ -1,27 +1,31 @@ |
|||||||
<?php |
<?php |
||||||
|
|
||||||
|
use yii\helpers\Inflector; |
||||||
|
use yii\helpers\StringHelper; |
||||||
|
|
||||||
/** |
/** |
||||||
* The following variables are available in this template: |
* @var yii\base\View $this |
||||||
* - $this: the CrudCode object |
* @var yii\gii\generators\crud\Generator $generator |
||||||
*/ |
*/ |
||||||
?> |
|
||||||
<?php echo "<?php\n"; ?> |
|
||||||
/* @var $this <?php echo $this->getControllerClass(); ?> */
|
|
||||||
/* @var $model <?php echo $this->getModelClass(); ?> */
|
|
||||||
|
|
||||||
<?php |
echo "<?php\n";
|
||||||
$label=$this->pluralize($this->class2name($this->modelClass)); |
|
||||||
echo "\$this->breadcrumbs=array( |
|
||||||
'$label'=>array('index'), |
|
||||||
'Create', |
|
||||||
);\n"; |
|
||||||
?> |
?> |
||||||
|
|
||||||
$this->menu=array( |
use yii\helpers\Html; |
||||||
array('label'=>'List <?php echo $this->modelClass; ?>', 'url'=>array('index')),
|
|
||||||
array('label'=>'Manage <?php echo $this->modelClass; ?>', 'url'=>array('admin')),
|
/** |
||||||
); |
* @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 |
<?php |
||||||
|
|
||||||
|
use yii\helpers\Inflector; |
||||||
|
use yii\helpers\StringHelper; |
||||||
|
|
||||||
/** |
/** |
||||||
* The following variables are available in this template: |
* @var yii\base\View $this |
||||||
* - $this: the CrudCode object |
* @var yii\gii\generators\crud\Generator $generator |
||||||
*/ |
*/ |
||||||
?> |
|
||||||
<?php echo "<?php\n"; ?> |
|
||||||
/* @var $this <?php echo $this->getControllerClass(); ?> */
|
|
||||||
/* @var $model <?php echo $this->getModelClass(); ?> */
|
|
||||||
|
|
||||||
<?php |
echo "<?php\n";
|
||||||
$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"; |
|
||||||
?> |
?> |
||||||
|
|
||||||
$this->menu=array( |
use yii\helpers\Html; |
||||||
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; ?>)),
|
* @var yii\base\View $this |
||||||
array('label'=>'Manage <?php echo $this->modelClass; ?>', 'url'=>array('admin')),
|
* @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 |
<?php |
||||||
|
|
||||||
|
use yii\helpers\Inflector; |
||||||
|
use yii\helpers\StringHelper; |
||||||
|
|
||||||
/** |
/** |
||||||
* The following variables are available in this template: |
* @var yii\base\View $this |
||||||
* - $this: the CrudCode object |
* @var yii\gii\generators\crud\Generator $generator |
||||||
*/ |
*/ |
||||||
?> |
|
||||||
<?php echo "<?php\n"; ?> |
|
||||||
/* @var $this <?php echo $this->getControllerClass(); ?> */
|
|
||||||
/* @var $model <?php echo $this->getModelClass(); ?> */
|
|
||||||
|
|
||||||
<?php |
echo "<?php\n";
|
||||||
$nameColumn=$this->guessNameColumn($this->tableSchema->columns); |
|
||||||
$label=$this->pluralize($this->class2name($this->modelClass)); |
|
||||||
echo "\$this->breadcrumbs=array( |
|
||||||
'$label'=>array('index'), |
|
||||||
\$model->{$nameColumn}, |
|
||||||
);\n"; |
|
||||||
?> |
?> |
||||||
|
|
||||||
$this->menu=array( |
use yii\helpers\Html; |
||||||
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')),
|
|
||||||
); |
|
||||||
?> |
|
||||||
|
|
||||||
<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(
|
$this->title = $model-><?php echo $generator->getNameAttribute(); ?>;
|
||||||
'data'=>$model, |
|
||||||
'attributes'=>array( |
|
||||||
<?php |
|
||||||
foreach($this->tableSchema->columns as $column) |
|
||||||
echo "\t\t'".$column->name."',\n"; |
|
||||||
?> |
?> |
||||||
), |
<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