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'; // 更新时间字段 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
以上就是解决TP5取不到Model的自定义字段问题的详细内容,更多请关注创想鸟其它相关文章!
版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。
如发现本站有涉嫌抄袭侵权/违法违规的内容, 请发送邮件至 chuangxiangniao@163.com 举报,一经查实,本站将立刻删除。
发布者:程序猿,转转请注明出处:https://www.chuangxiangniao.com/p/74739.html
微信扫一扫
支付宝扫一扫