Integrity constraint violation: 1052 Column'createtime' in where clause is

Must be used in the controller

$this->model = new model; the standard format is also the only format

Otherwise, the front-end field query will report errors such as Integrity constraint violation: 1052 Column'createtime' in where clause is.

list($where, $sort, $order, $offset, $limit) = $this->buildparams();

$where will process the fields of the current model table,

See lines 275 and 302 in the code /application/common/controller/Backend.php for details

if (!empty($this->model)) {
    $name = \think\Loader::parseName(basename(str_replace('\\', '/', get_class($this->model))));
    $name = $this->model->getTable();
    $tableName = $name . '.';
}
    if (stripos($k, ".") === false) {
        $k = $tableName . $k;
    }
buildparams()方法会获取当前模型的表名

In line 302, determine whether there is a table name before the field to be queried. If there is no table name, the current table name will be appended

In js, the associated table field to be displayed should add the table name

{field: 'order_num', title: '订单号'},
{field: 'user.user_name', title: '姓名'},

If no table name is added, the table name of the current model will be automatically added

 

 

 Controller standard format

        $this->model = new \app\admin\model\Order();
        // 这里一定要用$this->model  否则前端查询时会报错
        //设置过滤方法
        $this->request->filter(['strip_tags', 'trim']);
        if ($this->request->isAjax()){
            list($where, $sort, $order, $offset, $limit) = $this->buildparams();
            $total = $this->model
                ->where('order.status','=',2)
                ->with('user')
                ->where('unit_id',$this->unit_id)
                ->where($where)
                ->order($sort, $order)
                ->count();
            $list = $this->model
                ->with('user')
                ->where('order.status','=',2)
                ->where('unit_id',$this->unit_id)
                ->where($where)
                ->order($sort, $order)
                ->limit($offset, $limit)
                ->select();


            $list = collection($list)->toArray();
            $result = array("total" => $total, "rows" => $list);

            return json($result);
        }

The user in the with association is added to the Order model

<?php

namespace app\admin\model;

use think\Model;


class Order extends Model
{
    public function user(){
        return $this->belongsTo('User', 'user_id', 'id', [], 'LEFT')->setEagerlyType(0);
    }
}
?>

Use method in front-end JS,

zdxx: function () {
                var table_zdxx = $("#table_zdxx");
                table_zdxx.bootstrapTable({
                    url: $.fn.bootstrapTable.defaults.extend.zdxx_index,
                    extend:{
                    },
                    toolbar: '#toolbar_zdxx',
                    sortName: 'id',
                    pagination: true,
                    commonSearch: true,
                    search: true,
                    columns: [
                        [
                            {field: 'state', checkbox: true, },
                            {field: 'id', title: 'ID'},
                            {field: 'order_num', title: '订单号'},
                            {field: 'user.user_name', title: '姓名'},
                            {field: 'num', title: '购买数量'},
                            {field: 'money', title: '金额'},
                            {field: 'isinvoice', title: '是否开票',formatter:function (value) {
                                    if(value==0)
                                        return '未开票';
                                    else if(value==1)
                                        return '已开票';
                                },
                                searchList: {"0": '未开票', "1": '已开票'},},
                            {field: 'createtime', title: '申请时间',formatter: Table.api.formatter.datetime, operate: 'RANGE', addclass: 'datetimerange',},


                        ]
                    ]
                });
                // 为表格1绑定事件
                Table.api.bindevent(table_nbry);
            },

 

Guess you like

Origin blog.csdn.net/lows_H/article/details/105686393