YII 通用CURD外部Action 适合单表
通用 适合 外部 Action yii 单表 CURD
2023-09-14 08:59:48 时间
主键:CActiveRecord::model($modelClass)- tableSchema- primaryKey;
库名:CActiveRecord::model($modelClass)- tableSchema- schemaName;
表名:CActiveRecord::model($modelClass)- tableSchema- name;
public $redirectTo = array(index); public $deleteAttribute = array(); //$deleteAttribute = array(is_actived= 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); }
$model = CActiveRecord::model($this- modelClass)- findByPk($pk); if(!$model) throw new CHttpException(404); $this- getController()- render($this- renderTo, array(model= $model)); }
$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)); }
$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)); }
$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); }
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, ), ); }
无需编程,基于PostgreSQL零代码生成CRUD增删改查RESTful API接口 采用抽象工厂设计模式,可以无缝切换不同类型的数据库,从crudapi 1.3.0版本开始,添加了对大象数据库PostgreSQL的支持。并且以学生对象为例,零代码实现了CRUD增删改查RESTful API,后续计划支持更多的数据库,比如Oracle,MSSQL Server,Mongodb等。
1.NetDh框架之数据库操作层--Dapper简单封装,可支持多库实例、多种数据库类型等(附源码和示例代码) 1.NetDh框架开始的需求场景 需求场景: 1.之前公司有不同.net项目组,有的项目是用SqlServer做数据库,有的项目是用Oracle,后面也有可能会用到Mysql等,而且要考虑后续扩展成主从库、多库的需求。
为什么用外部Action,可以提高业务的重用性 。如DeleteAction可以同时处理post,user,...删除。
制器通常有一个默认的动作。当用户的请求未指定要执行的动作时,默认动作将被执行。默认情况下,默认的动作名为index, 它可以通过设置 CController::defaultAction 修改。
public $redirectTo = array(index); public $deleteAttribute = array(); //$deleteAttribute = array(is_actived= 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); }
$model = CActiveRecord::model($this- modelClass)- findByPk($pk); if(!$model) throw new CHttpException(404); $this- getController()- render($this- renderTo, array(model= $model)); }
$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)); }
$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)); }
$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); }
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,访问时
无需编程,基于PostgreSQL零代码生成CRUD增删改查RESTful API接口 采用抽象工厂设计模式,可以无缝切换不同类型的数据库,从crudapi 1.3.0版本开始,添加了对大象数据库PostgreSQL的支持。并且以学生对象为例,零代码实现了CRUD增删改查RESTful API,后续计划支持更多的数据库,比如Oracle,MSSQL Server,Mongodb等。
1.NetDh框架之数据库操作层--Dapper简单封装,可支持多库实例、多种数据库类型等(附源码和示例代码) 1.NetDh框架开始的需求场景 需求场景: 1.之前公司有不同.net项目组,有的项目是用SqlServer做数据库,有的项目是用Oracle,后面也有可能会用到Mysql等,而且要考虑后续扩展成主从库、多库的需求。
相关文章
- 通用删除查询语句存储过程
- Effective Java -- 对于所有对象都通用的方法
- Python Django 基于通用视图实现图片的显示功能代码示例
- [转]手游运营后台通用需求
- Atitit 纯java项目的提升进度大法---通用json dsl接口
- Atitit GRASP(General Responsibility Assignment Software Patterns),中文名称为“通用职责分配软件模式”
- 通用的改进遗传算法求解带约束的优化问题(MATLAB代码)
- 【MATLAB】通信信号调制通用函数 — 带通滤波器
- 【第35篇】MOBILEVIT:轻量、通用和适用移动设备的Vision Transformer
- 内核通用链表-双向循环链表
- 通用大数据架构体系介绍
- ubuntu20.04/16.04下C++的通用Makefile模板解析