首页 > 活着 > YII 通用CURD外部Action 适合单表

YII 通用CURD外部Action 适合单表

用YII创建表的时候出现没主键的表,你可以在AR primaryKey()函数里手工声明主键 。

Java代码
1.主键:CActiveRecord::model($modelClass)->tableSchema->primaryKey;
2.库名:CActiveRecord::model($modelClass)->tableSchema->schemaName;
3.表名:CActiveRecord::model($modelClass)->tableSchema->name;
主键:CActiveRecord::model($modelClass)->tableSchema->primaryKey;
库名:CActiveRecord::model($modelClass)->tableSchema->schemaName;
表名:CActiveRecord::model($modelClass)->tableSchema->name;保存后得到主键值

Java代码
1.Yii::app()->db->getLastInsertID();
2.$model->save();
3.$model->id;
Yii::app()->db->getLastInsertID();
$model->save();
$model->id; 为什么用外部Action,可以提高业务的重用性 。如DeleteAction可以同时处理post,user,…删除。

制器通常有一个默认的动作。当用户的请求未指定要执行的动作时,默认动作将被执行。默认情况下,默认的动作名为index, 它可以通过设置 CController::defaultAction 修改。

Java代码
1.public $defaultAction=’index';
public $defaultAction=’index';

1.创建DeleteAction.php在protected/actions目录下,用于删除一条记录

Java代码
1. 2.class DeleteAction extends CAction{
3. public $param = 'id';
4. public $modelClass;
5. public $redirectTo = array('index');
6. public $deleteAttribute = array(); //$deleteAttribute = array('is_actived'=>1);
7. function run(){
8. $pk = Yii::app()->request->getParam($this->param);
9. if(empty($pk))
10. throw new CHttpException(404);
11. $model = CActiveRecord::model($this->modelClass)->findByPk($pk);
12. if(!$model)
13. throw new CHttpException(404);
14.
15. if (empty($this->deleteAttribute)) {//物理删除
16. if($model->delete())
17. $this->getController()->redirect($this->redirectTo);
18. }else{
19. $attr = key($this->deleteAttribute);
20. $model->{$attr} = $this->deleteAttribute[$attr];
21. if($model->save())
22. $this->getController()->redirect($this->redirectTo);
23. }
24. throw new CHttpException(500);
25. }
26.}
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目录下, 用于显示一条明细

Java代码
1. 2.class ViewAction extends CAction{
3. public $param = 'id';
4. public $renderTo = 'view';
5. public $modelClass;
6. function run(){
7. $pk = Yii::app()->request->getParam($this->param);
8. if(empty($pk))
9. throw new CHttpException(404);
10.
11. $model = CActiveRecord::model($this->modelClass)->findByPk($pk);
12. if(!$model)
13. throw new CHttpException(404);
14.
15. $this->getController()->render($this->renderTo, array(‘model’=>$model));
16. }
17.}
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目录下, 用于修改一条明细

Java代码
1. 2.class UpdateAction extends CAction{
3. public $param = 'id';
4. public $renderTo = 'update';
5. public $successRedirect = 'view';
6. public $modelClass;
7. function run(){
8. $pk = Yii::app()->request->getParam($this->param);
9. if(empty($pk))
10. throw new CHttpException(404);
11.
12. $model = CActiveRecord::model($this->modelClass)->findByPk($pk);
13. if(!$model)
14. throw new CHttpException(404);
15. $model->scenario = ‘update';
16.
17. if(isset($_POST[$this->modelClass]))
18. {
19. $model->attributes=$_POST[$this->modelClass];
20. if($model->save())
21. $this->getController()->redirect( array($this->successRedirect, ‘id’=>$model->{$model->tableSchema->primaryKey}) );
22. }
23.
24. $this->getController()->render($this->renderTo, array(‘model’=>$model));
25. }
26.}
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目录下, 用于新增一条记录

Java代码
1. 2.class CreateAction extends CAction{
3. public $renderTo = 'create';
4. public $successRedirect = 'view';
5. public $modelClass;
6. function run(){
7. $model = new $this->modelClass;
8. $model->scenario = ‘new';
9. if(isset($_POST[$this->modelClass]))
10. {
11. $model->attributes=$_POST[$this->modelClass];
12. if($model->save())
13. $this->getController()->redirect( array($this->successRedirect, ‘id’=>$model->{$model->tableSchema->primaryKey}) );
14. }
15.
16. $this->getController()->render($this->renderTo, array(‘model’=>$model));
17. }
18.}
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目录下,用于查询列表管理

Java代码
1. 2.class ListAction extends CAction{
3. public $renderTo = 'admin';
4. public $modelClass;
5. public $paramsArr = array();
6. function run(){
7. /* //需修改model中的search()来接收页面的搜索条件
8. $dataProvider = CActiveRecord::model($this->modelClass)->search();
9. $params = array(‘dataProvider’=>$dataProvider);*/
10.
11. /*
12. renderPartial(‘_search’,array(
13. ‘model’=>$model,
14. ));
15.
16. $this->widget(‘zii.widgets.grid.CGridView’, array(
17. ‘dataProvider’=>$model->search(),
18. ‘columns’=>array(
19. ‘id’,
20. ),
21. ));*/
22. $model = new $this->modelClass(‘search’);
23. $model->unsetAttributes(); // clear any default values
24. if(isset($_GET[$this->modelClass]))
25. $model->attributes=$_GET[$this->modelClass];
26. $params = array(‘model’=>$model);
27.
28. if(!empty($this->paramsArr)){
29. $params = array_merge($params, $this->paramsArr);
30. }
31. $this->getController()->render($this->renderTo, $params);
32. }
33.}
class ListAction extends CAction{
public $renderTo = 'admin';
public $modelClass;
public $paramsArr = array();
function run(){
/* //需修改model中的search()来接收页面的搜索条件
$dataProvider = CActiveRecord::model($this->modelClass)->search();
$params = array(‘dataProvider’=>$dataProvider);*/

/*
renderPartial(‘_search’,array(
‘model’=>$model,
));

$this->widget(‘zii.widgets.grid.CGridView’, array(
‘dataProvider’=>$model->search(),
‘columns’=>array(
‘id’,
),
));*/
$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

Java代码
1. 2.class PostController extends Controller
3.{
4. function actions(){
5. return array(
6. 'create' => array(
7. ‘class’ => ‘application.actions.CreateAction’,
8. ‘modelClass’ => ‘Post’,
9. ),
10. ‘view’ => array(
11. ‘class’ => ‘application.actions.ViewAction’,
12. ‘param’ => ‘Postid’, //getParam(‘param’);
13. ‘modelClass’ => ‘Post’,
14. ),
15. ‘update’ => array(
16. ‘class’ => ‘application.actions.UpdateAction’,
17. ‘modelClass’ => ‘Post’,
18. ),
19. ‘delete’ => array(
20. ‘class’ => ‘application.actions.DeleteAction’,
21. ‘modelClass’ => ‘Post’,
22. ),
23. ‘index’ => array(
24. ‘class’ => ‘application.actions.ListAction’,
25. ‘modelClass’ => ‘Post’,
26. ),
27. );
28. }
29.}
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,访问时

Java代码
1.index.php?r=post/remove?Post_id=1
index.php?r=post/remove?Post_id=1修改系统gii的Crud Generator模板,可以快速的构建项目 http://hudeyong926.iteye.com/blog/1273561

用YII创建表的时候出现没主键的表,你可以在AR primaryKey()函数里手工声明主键 。 Java代码 […]

  1. 还没有评论
评论提交中, 请稍候...

留言

(Spamcheck Enabled)

Trackbacks & Pingbacks ( 0 )
  1. 还没有 trackbacks