欢迎光临
我们一直在努力

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

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

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

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

空数组

JdProxy.php

namespace appcrmmodel;  use thinkModel; // use appadminmodelCommon 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';  香港vps    // 更新时间字段     protected $updateTime = 'updated_at';      // 是否需要自动写入时间戳 如果设置为字符串 则表示时间字段的类型     protected $autoWriteTimestamp = 'datetime';      public function getTable($name = '')     {         return parent::getTable($name); // TODO: Change the autogenerated stub     }      /**      * type是父级thinkModel的属性      * 不能直接$this->type      */     public function getType() {         return $this->data['type'];     }  // ... }

查看父类 thinkModel

thinkphp/library/think/Model.php

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

假设走到__get方法

接下来到getAttr方法

然后是getData方法

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

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

可以改名typeName, clazz

本文来源网站:info110.com,若侵权,请联系删除。

赞(0)
【声明】:本博客不参与任何交易,也非中介,仅记录个人感兴趣的主机测评结果和优惠活动,内容均不作直接、间接、法定、约定的保证。访问本博客请务必遵守有关互联网的相关法律、规定与规则。一旦您访问本博客,即表示您已经知晓并接受了此声明通告。