解決TP5取不到Model的自定義字段問題

thinkphp框架教程欄目將給大家介紹關于TP5取不到Model實例的自定義字段 type的問題,希望對需要的朋友有所幫助!

解決TP5取不到Model的自定義字段問題

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

解決TP5取不到Model的自定義字段問題

thinkModel 定義了type字段 就不會走到 __get魔術方法

假設走到__get方法

解決TP5取不到Model的自定義字段問題

接下來到getAttr方法

解決TP5取不到Model的自定義字段問題

然后是getData方法

解決TP5取不到Model的自定義字段問題

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

????public?function?getType()?{ ?????????return?$this->data['type']; ?????}對于一些常見詞用來命名需要注意?比如?type,?name,?class...

可以改名typeName, clazz

? 版權聲明
THE END
喜歡就支持一下吧
點贊9 分享