Home  >  Article  >  PHP Framework  >  Solve the problem that TP5 cannot get the custom fields of the Model

Solve the problem that TP5 cannot get the custom fields of the Model

藏色散人
藏色散人forward
2021-09-11 16:00:032633browse

thinkphp frameworkThe tutorial column will introduce to you the problem that TP5 cannot get the custom field type of the Model instance. I hope it will be helpful to friends in need!

Solve the problem that TP5 cannot get the custom fields of the Model

#thinkphp5 Can’t get the custom field type of the Model instance?

$proxy->type The result is []

empty array

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'];
    }
 // ...
}

View parent class\think\Model

thinkphp/library/think/Model.php

\think\ If the Model defines the type field, it will not go to the __get magic method

Suppose you go to the __get method

Then go to the getAttr method

Then there is the getData method

So I added a method to the custom Model to get the value of the type field

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

You can change the name typeName, clazz

The above is the detailed content of Solve the problem that TP5 cannot get the custom fields of the Model. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:csdn.net. If there is any infringement, please contact admin@php.cn delete