FastAdmin控制器都必须继承自 application/common/controller/Backend.php的\app\common\controller\Backend 这个基类,这个基类application/admin/library/traits/Backend.php 里引入的 use \app\admin\library\traits\Backend;它有八个公共方法,
index/add/edit/del/multi/recyclebin/destroy/restore/ import
查看/添加/编辑/删除/批量更新/回收站/真实删除/还原/导入
common/controller/Backend里还有好多属性:
当两张相似的表意见curd时一个没有关联模型查询一个有时的区别:
- class Demo extends Backend
- {
-
- /**
- * Demo模型对象
- * @var \app\admin\model\Demo
- */
- protected $model = null;
-
- public function _initialize()
- {
- parent::_initialize();
- $this->model = new \app\admin\model\Demo;
-
- }
-
- /**
- * 默认生成的控制器所继承的父类中有index/add/edit/del/multi五个基础方法、destroy/restore/recyclebin三个回收站方法
- * 因此在当前控制器中可不用编写增删改查的代码,除非需要自己控制这部分逻辑
- * 需要将application/admin/library/traits/Backend.php中对应的方法复制到当前控制器,然后进行修改
- */
-
- }
复制代码
traits 的原idnex方法- trait Backend
- {
-
- /**
- * 查看
- */
- public function index()
- {
- //设置过滤方法
- $this->request->filter(['strip_tags']);
- if ($this->request->isAjax()) {
- //如果发送的来源是Selectpage,则转发到Selectpage
- if ($this->request->request('keyField')) {
- return $this->selectpage();
- }
- list($where, $sort, $order, $offset, $limit) = $this->buildparams();
- $total = $this->model
- ->where($where)
- ->order($sort, $order)
- ->count();
-
- $list = $this->model
- ->where($where)
- ->order($sort, $order)
- ->limit($offset, $limit)
- ->select();
-
- $list = collection($list)->toArray();
- $result = array("total" => $total, "rows" => $list);
-
- return json($result);
- }
- return $this->view->fetch();
- }
- //......
- }
复制代码
有关联时的控制器:
- class Dash extends Backend
- {
-
- /**
- * Dash模型对象
- * @var \app\admin\model\Dash
- */
- protected $model = null;
-
- public function _initialize()
- {
- parent::_initialize();
- $this->model = new \app\admin\model\Dash;
-
- }
-
- /**
- * 默认生成的控制器所继承的父类中有index/add/edit/del/multi五个基础方法、destroy/restore/recyclebin三个回收站方法
- * 因此在当前控制器中可不用编写增删改查的代码,除非需要自己控制这部分逻辑
- * 需要将application/admin/library/traits/Backend.php中对应的方法复制到当前控制器,然后进行修改
- */
-
- /**
- * 查看
- */
- public function index()
- {
- //当前是否为关联查询
- $this->relationSearch = true;
- //设置过滤方法
- $this->request->filter(['strip_tags']);
- if ($this->request->isAjax())
- {
- //如果发送的来源是Selectpage,则转发到Selectpage
- if ($this->request->request('keyField'))
- {
- return $this->selectpage();
- }
- list($where, $sort, $order, $offset, $limit) = $this->buildparams();
- $total = $this->model
- ->with(['category'])
- ->where($where)
- ->order($sort, $order)
- ->fetchSql(false)
- ->count();
-
- $list = $this->model
- ->with(['category'])
- ->where($where)
- ->order($sort, $order)
- ->limit($offset, $limit)
- ->fetchSql(false)
- ->select();
- //file_put_contents(__DIR__.'/../../../runtime/log/sql_'.time().'_log.txt', $total);
- //file_put_contents(__DIR__.'/../../../runtime/log/sql_'.time().'_log1.txt', $list);
- foreach ($list as $row) {
-
- }
- $list = collection($list)->toArray();
- $result = array("total" => $total, "rows" => $list);
-
- return json($result);
- }
- return $this->view->fetch();
- }
- }
复制代码
无关联时的model
- class Demo extends Model
- {
- // 表名
- protected $name = 'demo';
-
- // 自动写入时间戳字段
- protected $autoWriteTimestamp = false;
-
- // 定义时间戳字段名
- protected $createTime = false;
- protected $updateTime = false;
-
- // 追加属性
- protected $append = [
- ];
- }
复制代码
有关联时的model(多出了一个方法)
- class Dash extends Model
- {
- // 表名
- protected $name = 'dash';
-
- // 自动写入时间戳字段
- protected $autoWriteTimestamp = false;
-
- // 定义时间戳字段名
- protected $createTime = false;
- protected $updateTime = false;
-
- // 追加属性
- protected $append = [
-
- ];
-
- public function category()
- {
- return $this->belongsTo('Category', 'category_id', 'id', [], 'LEFT')->setEagerlyType(0);
- }
- }
复制代码
js
- define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefined, Backend, Table, Form) {
-
- var Controller = {
- index: function () {
- // 初始化表格参数配置
- Table.api.init({
- extend: {
- index_url: 'demo/index',
- add_url: 'demo/add',
- edit_url: 'demo/edit',
- del_url: 'demo/del',
- multi_url: 'demo/multi',
- table: 'demo',
- }
- });
-
- var table = $("#table");
-
- // 初始化表格
- table.bootstrapTable({
- url: $.fn.bootstrapTable.defaults.extend.index_url,
- pk: 'id',
- sortName: 'id',
- columns: [
- [
- {checkbox: true},
- {field: 'id', title: __('Id')},
- {field: 'name', title: __('Name')},
- {field: 'operate', title: __('Operate'), table: table, events: Table.api.events.operate, formatter: Table.api.formatter.operate}
- ]
- ]
- });
-
- // 为表格绑定事件
- Table.api.bindevent(table);
- },
- add: function () {
- Controller.api.bindevent();
- },
- edit: function () {
- Controller.api.bindevent();
- },
- api: {
- bindevent: function () {
- Form.api.bindevent($("form[role=form]"));
- }
- }
- };
- return Controller;
- });
复制代码
关联后的js
- define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefined, Backend, Table, Form) {
-
- var Controller = {
- index: function () {
- // 初始化表格参数配置
- Table.api.init({
- extend: {
- index_url: 'dash/index',
- add_url: 'dash/add',
- edit_url: 'dash/edit',
- del_url: 'dash/del',
- multi_url: 'dash/multi',
- table: 'dash',
- }
- });
-
- var table = $("#table");
-
- // 初始化表格
- table.bootstrapTable({
- url: $.fn.bootstrapTable.defaults.extend.index_url,
- pk: 'id',
- sortName: 'id',
- columns: [
- [
- {checkbox: true},
- {field: 'id', title: __('Id')},
- {field: 'name', title: __('Name')},
- {field: 'category_id', title: __('Category_id')},
- {field: 'category.id', title: __('Category.id')},
- {field: 'category.pid', title: __('Category.pid')},
- {field: 'category.type', title: __('Category.type')},
- {field: 'category.name', title: __('Category.name')},
- {field: 'category.nickname', title: __('Category.nickname')},
- {field: 'category.flag', title: __('Category.flag'), operate:'FIND_IN_SET', formatter: Table.api.formatter.label},
- {field: 'category.image', title: __('Category.image'), formatter: Table.api.formatter.image},
- {field: 'category.keywords', title: __('Category.keywords')},
- {field: 'category.description', title: __('Category.description')},
- {field: 'category.diyname', title: __('Category.diyname')},
- {field: 'category.createtime', title: __('Category.createtime'), operate:'RANGE', addclass:'datetimerange', formatter: Table.api.formatter.datetime},
- {field: 'category.updatetime', title: __('Category.updatetime'), operate:'RANGE', addclass:'datetimerange', formatter: Table.api.formatter.datetime},
- {field: 'category.weigh', title: __('Category.weigh')},
- {field: 'category.status', title: __('Category.status'), formatter: Table.api.formatter.status},
- {field: 'operate', title: __('Operate'), table: table, events: Table.api.events.operate, formatter: Table.api.formatter.operate}
- ]
- ]
- });
-
- // 为表格绑定事件
- Table.api.bindevent(table);
- },
- add: function () {
- Controller.api.bindevent();
- },
- edit: function () {
- Controller.api.bindevent();
- },
- api: {
- bindevent: function () {
- Form.api.bindevent($("form[role=form]"));
- }
- }
- };
- return Controller;
- });
复制代码
|
|