FastAdmin Cms 模型管理 新增 时间戳 字段 int 型

php开源项目 / 1179人浏览 / 0人评论

FastAdmin Cms 模型管理 新增 时间戳 字段 int 型

FastAdmin Cms 模型管理 新增 时间戳 字段 int 型

1、模板修改(编辑,新增文章中需要)

/application/admin/view/cms/archives/fields.html

(1)、新增 numdate 类别(可自定义)

{case numdate}
<div class='input-group date datetimepicker'>
<input type='text' name="row[{$item.name}]" data-rule="required(isnormal)" data-date-format="YYYY-MM-DD HH:mm:ss" value="{$item.value|date='Y m d H:i:s', ###}" class="form-control datetimepicker"/>
<span class="input-group-addon">
<span class="fa fa-calendar"></span>
</span>
</div>
{/case}

(2)、新增方法,已经从继承的控制器拷贝到Achives 控制器下:

/**
* 添加
*/
public function add()
{
if ($this->request->isPost()) {
    $params = $this->request->post("row/a");
    if ($params) {
        $params = $this->preExcludeFields($params);
/*新增处理时间戳*/
        if(isset($params['product_time']) && !empty($params['product_time'])){
            $params['product_time'] = strtotime($params['product_time']);    
        }
/*新增处理时间戳*/
        if ($this->dataLimit && $this->dataLimitFieldAutoFill) {
            $params[$this->dataLimitField] = $this->auth->id;
        }
        $result = false;
        Db::startTrans();
        try {
            //是否采用模型验证
            if ($this->modelValidate) {
                $name = str_replace("\\model\\", "\\validate\\", get_class($this->model));
                $validate = is_bool($this->modelValidate) ? ($this->modelSceneValidate ? $name . '.add' : $name) : $this->modelValidate;
                $this->model->validateFailException(true)->validate($validate);
            }
            $result = $this->model->allowField(true)->save($params);
            Db::commit();
        } catch (ValidateException $e) {
            Db::rollback();
            $this->error($e->getMessage());
        } catch (PDOException $e) {
            Db::rollback();
            $this->error($e->getMessage());
        } catch (Exception $e) {
            Db::rollback();
            $this->error($e->getMessage());
        }
        if ($result !== false) {
            $this->success();
        } else {
            $this->error(__('No rows were inserted'));
        }
    }
    $this->error(__('Parameter %s can not be empty', ''));
}
return $this->view->fetch();
}  

(3)、编辑方法同样需要对时间(Y m d H:i:s)处理成时间戳

/**
     * 编辑
     *
     * @param mixed $ids
     * @return string
     */
    public function edit($ids = null)
    {
        $row = $this->model->get($ids);
        if (!$row) {
            $this->error(__('No Results were found'));
        }

        $adminIds = $this->getDataLimitAdminIds();
        if (is_array($adminIds)) {
            if (!in_array($row[$this->dataLimitField], $adminIds)) {
                $this->error(__('You have no permission'));
            }
        }
        if (!$this->isSuperAdmin && !in_array($row['channel_id'], $this->channelIds)) {
            $this->error(__('You have no permission'));
        }
        if ($this->request->isPost()) {
            $params = $this->request->post("row/a");
/*新增处理时间戳*/
            if(isset($params['product_time']) && !empty($params['product_time'])){
                $params['product_time'] = strtotime($params['product_time']);    
            }
/*新增处理时间戳*/            
            if ($params) {
                $params = $this->preExcludeFields($params);
                $result = false;
                Db::startTrans();
                try {
                    //是否采用模型验证
                    if ($this->modelValidate) {
                        $name = str_replace("\\model\\", "\\validate\\", get_class($this->model));
                        $validate = is_bool($this->modelValidate) ? ($this->modelSceneValidate ? $name . '.edit' : $name) : $this->modelValidate;
                        $row->validateFailException(true)->validate($validate);
                    }
                    $result = $row->allowField(true)->save($params);
                    Db::commit();
                } catch (ValidateException $e) {
                    Db::rollback();
                    $this->error($e->getMessage());
                } catch (PDOException $e) {
                    Db::rollback();
                    $this->error($e->getMessage());
                } catch (Exception $e) {
                    Db::rollback();
                    $this->error($e->getMessage());
                }
                if ($result !== false) {
                    $this->success();
                } else {
                    $this->error(__('No rows were updated'));
                }
            }
            $this->error(__('Parameter %s can not be empty', ''));
        }else{
            
        }
        
        $channel = Channel::get($row['channel_id']);
        if (!$channel) {
            $this->error(__('No specified channel found'));
        }
        $model = \app\admin\model\cms\Modelx::get($channel['model_id']);
        if (!$model) {
            $this->error(__('No specified model found'));
        }
        $addon = db($model['table'])->where('id', $row['id'])->find();
        
        if ($addon) {
            $row->setData($addon);
        }
        if(isset($row['product_time']) && !empty($row['product_time'])){
            $row['product_time'] = date('Y m d H:i:s', $row['product_time']);
        }
        $disabledIds = [];
        $all = collection(Channel::order("weigh desc,id desc")->select())->toArray();
        foreach ($all as $k => $v) {
            if ($v['type'] != 'list' || $v['model_id'] != $channel['model_id']) {
                $disabledIds[] = $v['id'];
            }
        }
        $tree = Tree::instance()->init($all, 'parent_id');
        $channelOptions = $tree->getTree(0, "<option model='@model_id' value=@id @selected @disabled>@spacer@name</option>", $row['channel_id'], $disabledIds);
        $this->view->assign('channelOptions', $channelOptions);
        $this->view->assign("row", $row);
        if($channel['model_id'] == 2){
            return $this->view->fetch('edit_product');
        }
        return $this->view->fetch();
}

(4)、修改配置类型

/application/common/model/Config.php

/**
     * 读取配置类型
     * @return array
     */
    public static function getTypeList()
    {
        $typeList = [
            'string'   => __('String'),
            'text'     => __('Text'),
            'editor'   => __('Editor'),
            'number'   => __('Number'),
            'date'     => __('Date'),
            'numdate'  => __('NumberDate'),//新增字段表类型,numdate(同上是自定义的)位置的语言包
            'time'     => __('Time'),
            'datetime' => __('Datetime'),
            'select'   => __('Select'),
            'selects'  => __('Selects'),
            'image'    => __('Image'),
            'images'   => __('Images'),
            'file'     => __('File'),
            'files'    => __('Files'),
            'switch'   => __('Switch'),
            'checkbox' => __('Checkbox'),
            'radio'    => __('Radio'),
            'array'    => __('Array'),
            'custom'   => __('Custom'),
        ];
        return $typeList;
    }

    public static function getRegexList()
    {
        $regexList = [
            'required' => '必选',
            'digits'   => '数字',
            'letters'  => '字母',
            'date'     => '日期',
            'numdate'  => '数字日期', //后台选择显示对应,numdate(同上是自定义的)位置的语言包
            'time'     => '时间',
            'email'    => '邮箱',
            'url'      => '网址',
            'qq'       => 'QQ号',
            'IDcard'   => '身份证',
            'tel'      => '座机电话',
            'mobile'   => '手机号',
            'zipcode'  => '邮编',
            'chinese'  => '中文',
            'username' => '用户名',
            'password' => '密码'
        ];
        return $regexList;
}

(5)、语言包配置(在此文件新增)

/application/admin/lang/zh-cn/cms/fields.php

<?php

return [
    'Model_id'     => '模型ID',
    'Name'         => '名称',
    'Type'         => '类型',
    'Title'        => '标题',
    'Content'      => '条目列表',
    'Rule'         => '验证规则',
    'Validate Msg' => '错误消息',
    'Validate Ok'  => '成功消息',
    'Validate Tip' => '提示消息',
    'Extend'       => '扩展信息',
    'Weigh'        => '排序',
    'Setting'      => '字段设置',
    'Length'       => '字段长度',
    'Decimals'     => '小数点长度',
    'Minimum'      => '最少选择',
    'Maximum'      => '最大选择',
    'Defaultvalue' => '默认值',
    'Iscontribute' => '是否可投稿',
    'Isfilter'     => '是否列表筛选',
    'String'       => '字符',
    'Text'         => '文本',
    'Editor'       => '编辑器',
    'Number'       => '数字',
    'Date'         => '日期',
    'Time'         => '时间',
    'Datetime'     => '日期时间',
    'NumberDate'   => '数字日期',/*新增在此*/
    'Image'        => '图片',
    'Images'       => '图片(多)',
    'File'         => '文件',
    'Files'        => '文件(多)',
    'Select'       => '列表',
    'Selects'      => '列表(多选)',
    'Switch'       => '开关',
    'Checkbox'     => '复选',
    'Radio'        => '单选',
    'Array'        => '数组',
    'Array key'    => '键名',
    'Array value'  => '键值',
    'Createtime'   => '添加时间',
    'Updatetime'   => '更新时间',
    'Status'       => '状态'
];

(6)、还需要修改一个文件(不然数据可能无法新增入库,类型错误)

/addons/cms/library/Alter.php

public function setType($type)
{
switch ($type) {
    case 'checkbox':
    case 'selects':
        $this->data['type'] = 'SET';
        break;
    case 'radio':
    case 'select':
        $this->data['type'] = 'ENUM';
        break;
    case 'number':
        $this->data['type'] = 'INT';
        break;
/*新增位置*/
    case 'numdate'://日期
        $this->data['type'] = 'INT';
        break;
/*新增位置*/
    case 'date':
    case 'datetime':
    case 'time':
        $this->data['type'] = strtoupper($type);
        break;
    case 'editor':
        $this->data['type'] = 'TEXT';
        break;
    default:
        $this->data['type'] = 'VARCHAR';
        break;
}
return $this;
}

(7)、修改完毕后台新增,如下图显示

数字日期

字段表(cms_fields)中显示如下,product_time(字段)自定义

数字日期显示

对应模型表显示如下product_time(字段)自定义(int型)

字段类型

转载注明:

扩展查找

0 条评论

还没有人发表评论

发表评论 取消回复

记住我的信息,方便下次评论
有人回复时邮件通知我