fastadmin多个表crud连表操作步骤

1、crud命令

bash 复制代码
php think crud -t xq_user_credential  -u  1 -c credential  -i voucher_type,nickname,user_id,voucher_url,status,time  --force=true

2、修改控制器controller文件

bash 复制代码
<?php

namespace app\admin\controller;

use app\common\controller\Backend;

/**
 * 凭证信息
 *
 * @icon fa fa-circle-o
 */
class Credential extends Backend
{

    /**
     * Credential模型对象
     * @var \app\admin\model\Credential
     */
    protected $model = null;

    public function _initialize()
    {
        parent::_initialize();
        $this->model = new \app\admin\model\Credential;

    }



    /**
     * 默认生成的控制器所继承的父类中有index/add/edit/del/multi五个基础方法、destroy/restore/recyclebin三个回收站方法
     * 因此在当前控制器中可不用编写增删改查的代码,除非需要自己控制这部分逻辑
     * 需要将application/admin/library/traits/Backend.php中对应的方法复制到当前控制器,然后进行修改
     */


    /**
     * 查看
     */
    public function index()
    {
        //当前是否为关联查询
        $this->relationSearch = false;
        //设置过滤方法
        $this->request->filter(['strip_tags', 'trim']);
        if ($this->request->isAjax()) {
            //如果发送的来源是Selectpage,则转发到Selectpage
            if ($this->request->request('keyField')) {
                return $this->selectpage();
            }
            list($where, $sort, $order, $offset, $limit) = $this->buildparams();
            $where =  'a.voucher_url  IS NOT NULL'. 
            ' AND a.voucher_url <> ""'.
            ' AND b.nickname <> ""'.
            ' AND b.nickname IS NOT NULL';
            $list = $this->model
                        ->alias('a')  // 给主表设置别名
                        ->join('xq_user_info b', 'a.user_id = b.user_id', 'left')  // 内连接
                        ->where($where)  // 添加查询条件
                        // ->order($sort, $order)  // 排序
                        ->paginate($limit);  // 分页       

            foreach ($list as $row) {
                $row->visible(['id','voucher_type','nickname','user_id','voucher_url','status','time']);
                
            }
            // 获取 voucher_type 的值并显示异常或正常
            foreach ($list->items() as $item) {
                if ($item->voucher_type == 1) {
                    $item->voucher_type="工作认证";
                } elseif ($item->voucher_type == 2) {
                    $item->voucher_type="房产认证";
                }elseif ($item->voucher_type == 3) {
                    $item->voucher_type="车辆信息";
                }elseif ($item->voucher_type == 4) {
                    $item->voucher_type="单身承诺书";
                }elseif ($item->voucher_type == 5) {
                    $item->voucher_type="诚信承诺书";
                }elseif ($item->voucher_type == 6) {
                    $item->voucher_type="友好承诺书";
                }elseif ($item->voucher_type == 7) {
                    $item->voucher_type="信息保密协议";
                }elseif ($item->voucher_type == 8) {
                    $item->voucher_type="学历认证";
                }elseif ($item->voucher_type == 9) {
                    $item->voucher_type="体检认证";
                }elseif ($item->voucher_type == 10) {
                    $item->voucher_type="实名认证";
                }else {
                    $item->voucher_type="其他类型";
                }
                if ($item->status == 1) {
                    $item->status="已认证";
                }
                if ($item->status == 0) {
                    $item->status="未认证";
                }
            }
            $result = array("total" => $list->total(), "rows" => $list->items());

            return json($result);
        }
        return $this->view->fetch();
    }

}

其中where条件的写法

bash 复制代码
            $where =  'a.voucher_url  IS NOT NULL'. 
            ' AND a.voucher_url <> ""'.
            ' AND b.nickname <> ""'.
            ' AND b.nickname IS NOT NULL';

表连接的写法

bash 复制代码
            $list = $this->model
                        ->alias('a')  // 给主表设置别名
                        ->join('xq_user_info b', 'a.user_id = b.user_id', 'left')  // 内连接
                        ->where($where)  // 添加查询条件
                        // ->order($sort, $order)  // 排序
                        ->paginate($limit);  // 分页   

特定字段做显示处理写法

bash 复制代码
// 获取 voucher_type 的值并显示异常或正常
            foreach ($list->items() as $item) {
                if ($item->voucher_type == 1) {
                    $item->voucher_type="工作认证";
                } elseif ($item->voucher_type == 2) {
                    $item->voucher_type="房产认证";
                }elseif ($item->voucher_type == 3) {
                    $item->voucher_type="车辆信息";
                }elseif ($item->voucher_type == 4) {
                    $item->voucher_type="单身承诺书";
                }elseif ($item->voucher_type == 5) {
                    $item->voucher_type="诚信承诺书";
                }elseif ($item->voucher_type == 6) {
                    $item->voucher_type="友好承诺书";
                }elseif ($item->voucher_type == 7) {
                    $item->voucher_type="信息保密协议";
                }elseif ($item->voucher_type == 8) {
                    $item->voucher_type="学历认证";
                }elseif ($item->voucher_type == 9) {
                    $item->voucher_type="体检认证";
                }elseif ($item->voucher_type == 10) {
                    $item->voucher_type="实名认证";
                }else {
                    $item->voucher_type="其他类型";
                }
                if ($item->status == 1) {
                    $item->status="已认证";
                }
                if ($item->status == 0) {
                    $item->status="未认证";
                }
            }

页面显示js路径
页面中修改的地方

bash 复制代码
define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefined, Backend, Table, Form) {

    var Controller = {
        index: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'credential/index' + location.search,
                    add_url: 'credential/add',
                    edit_url: 'credential/edit',
                    del_url: 'credential/del',
                    multi_url: 'credential/multi',
                    import_url: 'credential/import',
                    table: 'user_credential',
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'id',
                columns: [
                    [
                        {checkbox: true},
                        {field: 'voucher_type', title: __('Voucher_type'), operate: 'LIKE', table: table, class: 'autocontent', formatter: Table.api.formatter.content},
                        {field: 'nickname', title: __('Nickname'),operate: 'LIKE', table: table, class: 'autocontent', formatter: Table.api.formatter.content},
                        {field: 'user_id', title: __('User_id'), operate: 'LIKE', table: table, class: 'autocontent', formatter: Table.api.formatter.content},
                        {field: 'voucher_url', title: __('Voucher_url'), operate: 'LIKE', formatter: Table.api.formatter.url},
                        {field: 'status', title: __('Status')},
                        {field: 'time', title: __('Time'), operate:'RANGE', addclass:'datetimerange', autocomplete:false, formatter: Table.api.formatter.datetime},
                        {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;
});

其中

bash 复制代码
 {field: 'nickname', title: __('Nickname'),operate: 'LIKE', table: table, class: 'autocontent', formatter: Table.api.formatter.content},

是新加的列

相关推荐
程序喵大人8 小时前
【C++进阶】STL算法与函数对象 - 09 函数对象保存状态并复用规则
开发语言·c++·算法·stl·函数对象
程序员黑豆11 小时前
Java包装类:基本类型与对象的桥梁
java·前端·ai编程
牛哇网络工作室14 小时前
UnityHDRP写实数字人全流程基础5—语音输入和语音识别
android·unity·c#·游戏引擎·aigc·语音识别·xcode
hsjiasb14 小时前
FreeRTOS学习(二十七)——任务栈与栈溢出检测
开发语言·stm32·学习·学习笔记·freertos
一笑的小酒馆14 小时前
Androidiot蓝牙配网简单封装
android
丰锋ff15 小时前
3.1 Qt事件之事件处理器
开发语言·qt
多弗朗皮卡丘15 小时前
C语言梦开始的地方7:函数
c语言·开发语言
mldong15 小时前
"applicant" 契约:退回发起人的闭环设计
java·架构
月华路15 小时前
G1 垃圾回收:脏卡队列、记忆集与并发精化线程机制
java·开发语言
gugucoding16 小时前
47. 【Java】Java内存模型(JMM)与可见性
java·开发语言