Home > PHP Framework > ThinkPHP > 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

藏色散人
Release: 2021-09-12 14:51:44
forward
2753 people have browsed it

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

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...
Copy after login

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!

Related labels:
source:csdn.net
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Issues
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template