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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

fareast_mzh

打赏个金币

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值