您的位置 首页 php

解决TP5取不到Model的自定义字段问题

thinkphp框架教程栏目将给大家介绍关于TP5取不到Model实例的自定义字段 type的问题,希望对需要的朋友有所帮助!

thinkphp5 取不到Model实例的 自定义字段 type?

$proxy->type 得到的结果是 []

空数组

JdProxy.php

namespace app\crm\model;use think\Model;// use app\admin\model\Common as BaseModel;class JdProxy extends Model {    protected $table = '5kcrm_crm_district';    protected $pk = 'id';    protected $field = ['id',        'type',        'arm_acid',        'province','city','district',        'proxyname','manager','gradetype', 'has_updated'];    protected function initialize() {        parent::initialize();    }    // 创建时间字段    protected $createTime = 'created_at';    // 更新时间字段    protected $updateTime = 'updated_at';    // 是否需要自动写入时间戳 如果设置为字符串 则表示时间字段的类型    protected $autoWriteTimestamp = 'datetime';    public function getTable($name = '')    {        return parent::getTable($name); // TODO: Change the autogenerated stub    }    /**     * type是父级\think\Model的属性     * 不能直接$this->type     */    public function getType() {        return $this->data['type'];    } // ...}

查看父类 \think\Model

thinkphp/library/think/Model.php

\think\Model 定义了type字段 就不会走到 __get魔术方法

假设走到__get方法

接下来到getAttr方法

然后是getData方法

所以我在自定义的Model中添加方法取得type字段的值

    public function getType() {         return $this->data['type'];     }对于一些常见词用来命名需要注意 比如 type, name, class...

可以改名typeName, clazz

文章来源:智云一二三科技

文章标题:解决TP5取不到Model的自定义字段问题

文章地址:https://www.zhihuclub.com/24733.shtml

关于作者: 智云科技

热门文章

网站地图