用YII创建表的时候出现没主键的表,你可以在AR primaryKey()函数里手工声明主键 。
- 主键:CActiveRecord::model($modelClass)->tableSchema->primaryKey;
- 库名:CActiveRecord::model($modelClass)->tableSchema->schemaName;
- 表名:CActiveRecord::model($modelClass)->tableSchema->name;
保存后得到主键值
- Yii::app()->db->getLastInsertID();
- $model->save();
- $model->id;
为什么用外部Action,可以提高业务的重用性 。如DeleteAction可以同时处理post,user,...删除。
制器通常有一个默认的动作。当用户的请求未指定要执行的动作时,默认动作将被执行。默认情况下,默认的动作名为index,
它可以通过设置 CController::defaultAction 修改。
- public $defaultAction='index';
1.创建DeleteAction.php在protected/actions目录下,用于删除一条记录
- <?php
- class DeleteAction extends CAction{
- public $param = 'id';
- public $modelClass;
- public $redirectTo = array('index');
- public $deleteAttribute = array(); //$deleteAttribute = array('is_actived'=>1);
- function run(){
- $pk = Yii::app()->request->getParam($this->param);
- if(empty($pk))
- throw new CHttpException(404);
- $model = CActiveRecord::model($this->modelClass)->findByPk($pk);
- if(!$model)
- throw new CHttpException(404);
- if (empty($this->deleteAttribute)) {//物理删除
- if($model->delete())
- $this->getController()->redirect($this->redirectTo);
- }else{
- $attr = key($this->deleteAttribute);
- $model->{$attr} = $this->deleteAttribute[$attr];
- if($model->save())
- $this->getController()->redirect($this->redirectTo);
- }
- throw new CHttpException(500);
- }
- }
创建ViewAction.php在protected/actions目录下, 用于显示一条明细
- <?php
- class ViewAction extends CAction{
- public $param = 'id';
- public $renderTo = 'view';
- public $modelClass;
- function run(){
- $pk = Yii::app()->request->getParam($this->param);
- if(empty($pk))
- throw new CHttpException(404);
- $model = CActiveRecord::model($this->modelClass)->findByPk($pk);
- if(!$model)
- throw new CHttpException(404);
- $this->getController()->render($this->renderTo, array('model'=>$model));
- }
- }
创建UpdateAction.php在protected/actions目录下, 用于修改一条明细
- <?php
- class UpdateAction extends CAction{
- public $param = 'id';
- public $renderTo = 'update';
- public $successRedirect = 'view';
- public $modelClass;
- function run(){
- $pk = Yii::app()->request->getParam($this->param);
- if(empty($pk))
- throw new CHttpException(404);
- $model = CActiveRecord::model($this->modelClass)->findByPk($pk);
- if(!$model)
- throw new CHttpException(404);
- $model->scenario = 'update';
- if(isset($_POST[$this->modelClass]))
- {
- $model->attributes=$_POST[$this->modelClass];
- if($model->save())
- $this->getController()->redirect( array($this->successRedirect, 'id'=>$model->{$model->tableSchema->primaryKey}) );
- }
- $this->getController()->render($this->renderTo, array('model'=>$model));
- }
- }
创建CreateAction.php在protected/actions目录下, 用于新增一条记录
- <?php
- class CreateAction extends CAction{
- public $renderTo = 'create';
- public $successRedirect = 'view';
- public $modelClass;
- function run(){
- $model = new $this->modelClass;
- $model->scenario = 'new';
- if(isset($_POST[$this->modelClass]))
- {
- $model->attributes=$_POST[$this->modelClass];
- if($model->save())
- $this->getController()->redirect( array($this->successRedirect, 'id'=>$model->{$model->tableSchema->primaryKey}) );
- }
- $this->getController()->render($this->renderTo, array('model'=>$model));
- }
- }
创建ListAction.php在protected/actions目录下,用于查询列表管理
- <?php
- class ListAction extends CAction{
- public $renderTo = 'admin';
- public $modelClass;
- public $paramsArr = array();
- function run(){
- $model = new $this->modelClass('search');
- $model->unsetAttributes(); // clear any default values
- if(isset($_GET[$this->modelClass]))
- $model->attributes=$_GET[$this->modelClass];
- $params = array('model'=>$model);
- if(!empty($this->paramsArr)){
- $params = array_merge($params, $this->paramsArr);
- }
- $this->getController()->render($this->renderTo, $params);
- }
- }
2.PostController.php
- <?php
- class PostController extends Controller
- {
- function actions(){
- return array(
- 'create' => array(
- 'class' => 'application.actions.CreateAction',
- 'modelClass' => 'Post',
- ),
- 'view' => array(
- 'class' => 'application.actions.ViewAction',
- 'param' => 'Postid', //getParam('param');
- 'modelClass' => 'Post',
- ),
- 'update' => array(
- 'class' => 'application.actions.UpdateAction',
- 'modelClass' => 'Post',
- ),
- 'delete' => array(
- 'class' => 'application.actions.DeleteAction',
- 'modelClass' => 'Post',
- ),
- 'index' => array(
- 'class' => 'application.actions.ListAction',
- 'modelClass' => 'Post',
- ),
- );
- }
- }
其中class是DeleteAction的路径,pk主键(默认id,可以不写),只需要修改actions,其他的model就可以共用DeleteAction.php,访问时
- index.php?r=post/remove?Post_id=1
修改系统gii的Crud Generator模板,可以快速的构建项目 http://hudeyong926.iteye.com/blog/1273561