[TOC]
模型的方法由
>[info]
>* vendor\topthink\think-orm\src\Model.php
abstract class Model implements JsonSerializable, ArrayAccess, Arrayable, Jsonable
JsonSerializable.jsonSerialize()json_encode(model對象)時觸發
ArrayAccess:數組方式操作model對象
Arrayable:toArray()方法
Jsonable:toJson()方法
>Model的 trait類:
>* vendor\topthink\think-orm\src\model\concern\Attribute.php
>vendor\topthink\think-orm\src\model\concern\RelationShip.php
>* vendor\topthink\think-orm\src\model\concern\ModelEvent.php
>* vendor\topthink\think-orm\src\model\concern\TimeStamp.php
>* vendor\topthink\think-orm\src\model\concern\Conversion.php
以及Db(facade隱射到了DbManager)、BaseQuery類構成
BaseQuery引入:
use concern\TimeFieldQuery;
use concern\AggregateQuery;
use concern\ModelRelationQuery;
use concern\ResultOperation;
use concern\Transaction;
use concern\WhereQuery;
## **model屬性**
屬性除了注明的其他都為protected
| 屬性名 | 默認值 | 描述 | 類型
| --- | --- | --- | ---|
| | <h2 style="color:blue">Model</h2> | | |
| $suffix | none| 數據表后綴 | string|
| $connection| none| 數據庫配置 | string|
| $name| none| 模型名稱(不加前綴) | string|
| $key| none| 主鍵值 | string|
| $table| none| 數據表名稱(需要加前綴) | string|
| $initialized| []| 初始化過的模型(靜態屬性) | array|
| $queryInstance| none| 查詢對象實例 | Query|
| $defaultSoftDelete| none| 軟刪除字段默認值 | mixed|
| $globalScope| []| 全局查詢范圍 | array|
| $db| none| Db對象 | DbManager(Db)|
| $invoker | none| 容器對象的依賴注入方法 | callable|
| $maker| none| 服務注入(靜態屬性 ) | Closure[](Closure:閉包函數)|
| $macro| [] | 方法注入 | Closure[][]|
| | <h2 style="color:blue">Attribute </h2> | | |
| 屬性 | 默認值 | 描述 | 類型 |
| $pk | id | 數據表主鍵 復合主鍵使用數組定義 | string或array |
| $schema | [] | 數據表字段信息 留空則自動獲取 | array |
| $field | [] | 當前允許寫入的字段 | array |
| $type| [] | 字段自動類型轉換 | array |
| $disuse| [] | 數據表廢棄字段 | array |
| $readonly| [] | 數據表廢棄字段 | array |
| $json| [] |JSON數據表字段| array |
| $jsonType| [] |JSON數據表字段類型| array |
| $jsonAssoc| false |JSON數據取出是否需要轉換為數組| bool |
| $strict| true|是否嚴格字段大小寫| bool |
| | <h2 style="color:blue">RelationShip</h2> | | |
| 屬性 | 默認值 | 描述 | 類型 |
| relationWrite | [] | 關聯自動寫入信息| array|
| | <h2 style="color:blue">ModelEvent</h2> | | |
| 屬性 | 默認值 | 描述 | 類型 |
| $event | none | Event對象(靜態屬性) | object |
| $withEvent | true | 是否需要事件響應 | bool |
| | <h2 style="color:blue">TimeStamp</h2> | | |
| 屬性 | 默認值 | 描述 | 類型 |
| $autoWriteTimestamp | none |是否需要自動寫入時間,值為布爾true/false則自動識別,如果設置為字符串 則表示時間字段的類型;支持的字段類型包括int/timestamp/datetime/date |bool或string |
| $createTime | create_time | 創建時間字段 false表示關閉 | false或string |
| $updateTime | update_time | 更新時間字段 false表示關閉 | false或|string |
| $dateFormat | none | 取出數據時間字段顯示格式,此項可在database.php數據庫配置文件設置默認:"Y-m-d H:i:s" | string |
| | <h2 style="color:blue">Conversion</h2> | | |
| 屬性 | 默認值 | 描述 | 類型 |
| $visible | [] | 數據輸出顯示的屬性 | array |
| $hidden| [] | 數據輸出隱藏的屬性 | array |
| $append| [] | 數據輸出需要追加的屬性 | array |
| $resultSetType | none | 數據集對象名 | string |
## **model方法**
| 方法名(public) | 描述 | 參數 | 返回值|
| --- | --- | --- | ---|
| | <h2 style="color:blue">Model</h2> | | |
| maker | 設置服務注入(靜態) | (Closure $maker) |void|
| macro | 設置方法注入(靜態) | (string $method, Closure $closure) |void|
| setDb | 設置Db對象 | (DbManager $db) |void|
| setInvoker | 設置容器對象及依賴注入方法 | (callable $callable) | void |
| invoke | 調用反射執行模型方法 支持參數綁定 | (string $method, array $vars = []) | mixed |
| getName | 獲取當前模型名稱 。先找name屬性,初始化時name沒有設置則賦值為當前運行模塊的類名如(User) | void | string |
| newInstance | 創建新的模型實例 | (array $data = [], $where = null) | Model |
| setUpdateWhere | 設置模型的更新條件(protected) | ($where) | void |
| setConnection | 設置當前模型的數據庫連接 |(string $connection) | $this |
| getConnection| 獲取當前模型的數據庫連接標識 |(void ) | string|
| connect| 切換數據庫連接進行查詢(靜態方法) |(string $connection) | Model|
| ~~setQuery~~(<span style="color:red">X</span>) |設置當前模型的數據庫查詢對象| (Query $query, bool $clear = true) | $this |
| ~~getQuery~~(<span style="color:red">X</span>) | 獲取當前模型的數據庫查詢對象 | void | Query或者null |
| setSuffix | 設置當前模型數據表的后綴 | (string $suffix) | $this |
| getSuffix | 獲取當前模型的數據表后綴 | void | string |
| suffix | 切換表后綴進行查詢(靜態方法) | (string $suffix) | Model |
| db | 獲取當前模型的數據庫查詢對象 | ($scope = []) | Query |
| force | 更新是否強制寫入數據 而不做比較(亦可用于軟刪除的強制刪除) | (bool $force = true) | $this |
| isForce | 判斷force | void | bool |
| replace | 新增數據是否使用Replace | (bool $replace = true) | $this |
| refresh | 刷新模型數據 | (bool $relation = false) | $this |
| exists | 設置數據是否存在 | (bool $exists = true) | $this |
| isExists | 判斷數據是否存在數據庫 | void | bool |
|isEmpty | 判斷模型是否為空 | void | bool |
| lazySave | 延遲保存當前數據對象 | ($data = []) | void |
| getWhere | 獲取當前的更新條件 | void | mixed |
| checkAllowFields(protected)| 檢查數據是否允許寫入 | (void) | array|
| updateData| 保存寫入數據(protected) | (void) | bool |
| insertData| 新增寫入數據(protected) | (string $sequence = null) | bool |
| save | 保存當前數據對象(入庫操作) | (array $data = [], string $sequence = null) | bool |
| saveAll | 保存多個數據到當前數據對象(入庫操作) | (iterable $dataSet, bool $replace = true) | Collection|
| delete | 刪除當前的記錄 | void |bool |
| create | 寫入數據(靜態方法) | (array $data, array $allowField = [], bool $replace = false) | Model |
| update | 更新數據(靜態方法) | (array $data, $where = [], array $allowField = []) | static(注意區分self) |
| destroy | 刪除記錄(靜態方法) | ($data, bool $force = false) | bool |
|__wakeup | 解序列化后處理 | void | |
|__isset| 檢測數據對象的值 | (string $name) | bool |
|__unset| 銷毀數據對象的值 | (string $name) | void |
|~~__debugInfo~~(<span style="color:red">X</span>) | | | |
| __set | 修改器原型 設置數據對象的值 | (string $name, $value) | void |
| __get | 獲取器原型 獲取數據對象的值 | (string $name) | mixed |
| __call | 調用不存在的方法時觸發(先macro方法,再獲取器方法,再Query方法) |($method, $args) | mixed |
| __callStatic | 調用不存在的靜態方法時觸發(先macro方法,再Query方法) |($method, $args) | mixed |
| withoutGlobalScope | 設置不使用的全局查詢范圍(靜態方法) | (array $scope = null) | Query |
| 可覆蓋的擴展方法名(protected)| 描述 | 參數 | 返回值|
| init | 自定義的初始化處理 | void | 自定義|
| checkData | | | 自定義|
| checkResult | | ($result) | 自定義|
| | <h2 style="color:blue">Attribute</h2> | | |
| getKey | 獲取模型對象的主鍵值 | void | mixed |
| allowField | 設置允許寫入的字段 | (array $field) | $this |
| readOnly | 設置只讀字段 | (array $field) | $this |
| data | 設置數據對象值 | (array $data, bool $set = false, array $allow = []) | $this |
| appendData | 批量追加數據對象值 | (array $data, bool $set = false) | $this |
| getOrigin | 獲取對象原始數據 如果不存在指定字段返回null | (string $name = null) | mixed |
| getData | 獲取對象原始數據 如果不存在指定字段返回false | (string $name = null) | mixed |
| getChangedData | 獲取變化的數據 并排除只讀數據 | void | array |
| set | 直接設置數據對象值 | (string $name, $value) | void |
| setAttrs | 通過修改器 批量設置數據對象值 | (array $data) | void |
| setAttr | 通過修改器 設置數據對象值 | (string $name, $value, array $data = []) | void |
| getAttr | 獲取器 獲取數據對象的值 | (string $name) | mixed |
| withAttribute | 設置數據字段獲取器 | ($name, callable $callback = null) | $this |
| isPk |判斷一個字段名是否為主鍵字段(protected)|(string $key)|bool|
| getRealFieldName(protected) | 獲取實際的字段名 | (string $name) | string |
| writeTransform(protected) | 數據寫入 類型轉換(protected) | ($value, $type) | mixed |
| getValue | 獲取經過獲取器處理后的數據對象的值(protected) | (string $name, $value, $relation = false) | mixed |
| getJsonValue | 獲取JSON字段屬性值(protected) | ($name, $value) | mixed |
| getRelationValue | 獲取關聯屬性值(protected) |(string $relation) | mixed |
| readTransform | 數據讀取 類型轉換(protected) |($value, $type) | mixed |
| | <h2 style="color:blue">RelationShip</h2> | | |
| 方法名 | 描述 | 參數 | 返回值 |
| setParent | 設置父關聯對象 | (Model $model) | $this |
| getParent | 獲取父關聯對象 | void | Model |
| getRelation | 獲取當前模型的關聯模型數據 | (string $name = null, bool $auto = false) | mixed |
| setRelation | 設置關聯數據對象值 | (string $name, $value, array $data = []) | $this |
| relationQuery | 查詢當前模型的關聯數據 | (array $relations, array $withRelationAttr = []) | void |
| together | 關聯數據寫入 | (array $relation) | $this |
| has(靜態) | 根據關聯條件查詢當前模型 | (string $relation, string $operator = '>=', int $count = 1, string $id = '*', string $joinType = '') | Query |
| hasWhere(靜態) | 根據關聯條件查詢當前模型 | (string $relation, $where = [], string $fields = '*', string $joinType = '') | Query |
| eagerly | 預載入關聯查詢 JOIN方式 | (Query $query, string $relation, $field, string $joinType = '', Closure $closure = null, bool $first = false) | bool |
| eagerlyResultSet | 預載入關聯查詢 返回數據集 | (array &$resultSet, array $relations, array $withRelationAttr = [], bool $join = false, $cache = false) | void |
| eagerlyResult | 預載入關聯查詢 返回模型對象 | (Model $result, array $relations, array $withRelationAttr = [], bool $join = false, $cache = false) | void |
| bindAttr | 綁定(一對一)關聯屬性到當前模型 | (string $relation, array $attrs = []) | $this |
| relationCount | 關聯統計 | (Model $result, array $relations, string $aggregate = 'sum', string $field = '*') | void |
| hasOne | HAS ONE 關聯定義 | hasOne(string $model, string $foreignKey = '', string $localKey = '') | HasOne |
| belongsTo | BELONGS TO 關聯定義 | (string $model, string $foreignKey = '', string $localKey = '') | BelongsTo |
| hasMany | HAS MANY 關聯定義 | (string $model, string $foreignKey = '', string $localKey = '') | HasMany |
| hasManyThrough | HAS MANY 遠程關聯定義 | (string $model, string $through, string $foreignKey = '', string $throughKey = '', string $localKey = '', string $throughPk = '') | HasManyThrough |
| hasOneThrough | HAS ONE 遠程關聯定義 | hasOneThrough(string $model, string $through, string $foreignKey = '', string $throughKey = '', string $localKey = '', string $throughPk = '') | HasOneThrough |
| belongsToMany | BELONGS TO MANY 關聯定義 | (string $model, string $middle = '', string $foreignKey = '', string $localKey = '') | BelongsToMany |
| morphOne | MORPH One 關聯定義 | (string $model, $morph = null, string $type = '') | MorphOne |
| morphMany | MORPH MANY 關聯定義 | (string $model, $morph = null, string $type = '') | MorphMany |
| morphTo | MORPH TO 關聯定義 | ($morph = null, array $alias = []) | MorphTo |
| morphByMany | MORPH BY MANY關聯定義 | (string $model, string $middle, $morph = null, string $foreignKey = null) | MorphToMany |
| removeRelation | 移除當前模型的關聯屬性 | void | $this |
| parseModel | 解析模型的完整命名空間(protected) | (string $model) | string |
| getForeignKey | 獲取模型的默認外鍵名(protected) | (string $name) | string |
| isRelationAttr |檢查屬性是否為關聯屬性 如果是則返回關聯方法名(protected) | (string $attr) | string|false |
| getRelationData |智能獲取關聯模型數據(protected) | (Relation $modelRelation) | mixed |
| checkAutoRelationWrite | 關聯數據自動寫入檢查(protected) | (void) | void |
| autoRelationUpdate | 自動關聯數據更新(針對一對一關聯 protected) | (void) | void |
| autoRelationInsert | 自動關聯數據寫入(針對一對一關聯 protected) | (void) | void |
| autoRelationDelete | 自動關聯數據刪除(針對一對一關聯 protected) | (void) | void |
| | <h2 style="color:blue">ModelEvent</h2> | | |
| setEvent | 設置Event對象(靜態方法) | (object $event) | void |
| withEvent | 當前操作的事件響應 | (bool $is_event_response) | $this |
| trigger | 觸發事件(protected) |(string $event) | bool |
| | <h2 style="color:blue">TimeStamp</h2> | | |
| isAutoWriteTimestamp | 是否需要自動寫入時間字段(調用checkTimeFieldType方法) | ($auto="bool or string") | $this |
| getAutoWriteTimestamp | 獲取自動寫入時間字段 | void | bool 或 string |
| setDateFormat | 設置時間字段格式化 | ($format='string or false') | $this |
| getDateFormat | 獲取自動寫入時間字段 | void | string 或 false |
| checkTimeFieldType | 檢測時間字段的實際類型(protected) | ($type) | mixed |
| autoWriteTimestamp | 自動寫入時間戳(protected) | (void) | mixed |
| getTimeTypeValue | 獲取指定類型的時間字段值(protected) | (string $type) | mixed |
| formatDateTime | 時間日期字段格式化處理(protected) | ($format, $time = 'now', bool $timestamp = false) | mixed |
| getTimestampValue | 獲取時間字段值(protected) |($value) | mixed |
| | <h2 style="color:blue">Conversion</h2> | | |
| append | 設置需要附加的輸出屬性 | (array $append = []) | $this |
| appendRelationAttr | 設置附加關聯對象的屬性 | (string $attr, array $append) | $this |
| hidden | 設置需要隱藏的輸出屬性 | (array $hidden = []) |$this |
| visible | 設置需要輸出的屬性 | (array $visible = []) | $this |
| toArray | 轉換當前模型對象為數組 | void | array |
| toJson| 轉換當前模型對象為JSON字符串 | (int $options = JSON_UNESCAPED_UNICODE) | string|
| __toString| model類對象被當做字符串輸出(echo、print輸出時轉換當前模型對象為JSON字符串) | void | string |
| jsonSerialize | json格式的當前模型對象轉數組格式 | void | array |
| toCollection| 轉換數據集為數據集對象 | (iterable $collection = [], string $resultSetType = null) | Collection |
| appendAttrToArray| | | |
| getBindAttr| | | |
# **特殊**
## **SoftDelete軟刪除trait類**
要使用軟刪除功能,需要引入`SoftDelete`trait,例如`User`模型按照下面的定義就可以使用軟刪除功能:
~~~
namespace app\model;
use think\Model;
use think\model\concern\SoftDelete;
class User extends Model
{
use SoftDelete;
protected $deleteTime = 'delete_time';
}
~~~
| SoftDelete屬性名 | 默認值 | 描述 | 類型
| --- | --- | --- | ---|
| | <h2 style="color:blue">Model</h2> | | |
| $withTrashed | false| 是否包含軟刪除數據 | bool|
| SoftDelete方法名(public) | 描述 | 參數 | 返回值|
| --- | --- | --- | ---|
| | <h2 style="color:blue">Model</h2> | | |
| trashed | 判斷當前實例是否被軟刪除 | (void) |bool|
| withTrashed | 查詢軟刪除數據(靜態) | (void) |Query|
| onlyTrashed | 只查詢軟刪除數據(靜態) | (void) |Query|
| destroy | 刪除記錄(靜態) | ($data, bool $force = false) |bool|
| delete | 刪除當前的記錄 | (void) |bool|
| restore | 恢復被軟刪除的記錄 | ($where = []) |bool|
| withTrashedData | 是否包含軟刪除數據(protected) |(bool $withTrashed) |this|
| getWithTrashedExp | 獲取軟刪除數據的查詢條件(protected) |(void) |array|
| getDeleteTimeField | 獲取軟刪除字段(protected) |(bool $read = false) |string\|false|
| withNoTrashed| 查詢的時候默認排除軟刪除數據(protected) |(Query $query) |void|
## **OptimLock樂觀鎖trait類**
這個使用前也要引用???
| OptimLock方法名(public) | 描述 | 參數 | 返回值|
| --- | --- | --- | ---|
| | <h2 style="color:blue">Model</h2> | | |
| getWhere | | (void) |string?|
| getOptimLockField | (protected) | (void) |string?|
| checkData | 數據檢查(protected) | (void) |void|
| recordLockVersion | 記錄樂觀鎖(protected) | (void) |void|
| updateLockVersion | 更新樂觀鎖(protected) | (void) |void|
| checkResult | (protected) | ($result) |void|
## --------------------------------------------------------------------------------------------------
* @method void onAfterRead(Model $model) static after_read事件定義
* @method mixed onBeforeInsert(Model $model) static before_insert事件定義
* @method void onAfterInsert(Model $model) static after_insert事件定義
* @method mixed onBeforeUpdate(Model $model) static before_update事件定義
* @method void onAfterUpdate(Model $model) static after_update事件定義
* @method mixed onBeforeWrite(Model $model) static before_write事件定義
* @method void onAfterWrite(Model $model) static after_write事件定義
* @method mixed onBeforeDelete(Model $model) static before_write事件定義
* @method void onAfterDelete(Model $model) static after_delete事件定義
* @method void onBeforeRestore(Model $model) static before_restore事件定義
* @method void onAfterRestore(Model $model) static after_restore事件定義
>[danger]**hasOne**與**BelongsTo**繼承至OneToOne
**HasOneThrough**繼承HasManyThrough
**MorphToMany**繼承BelongsToMany
**OneToOne**、**HasMany**、**HasManyThrough**、**BelongsToMany**、**MorphMany**、**MorphOne**、**MorphTo**繼承至**Relation**
>[danger]以下關聯類只收集public
## **Relation(基類)**
模型關聯基礎類
| 方法名(public) | 描述 | 參數 | 返回值 |
| --- | --- | --- | --- |
| | ## 方法 | | |
| getParent | 獲取關聯的所屬模型 | void | Model |
| getModel | 獲取當前的關聯模型對象實例 | void | Model |
| getQuery | 獲取關聯的查詢對象 | void | Query |
| ~~selfRelation~~ | 設置當前關聯為自關聯 | ($self = true) | this |
| isSelfRelation | 當前關聯是否為自關聯 | void | bool |
| update | 更新數據 | (array $data = []) | int |
| delete | 刪除記錄 |($data = null)| int |
| withLimit | 限制關聯數據的數量 | (int $limit) |this |
| withField| 限制關聯數據的字段 |(array $field) | this |
| __call | 獲取Query中的方法 | ($method, $args) | mixed |
## **OneToOne**
| 方法名(public) | 描述 | 參數 | 返回值 |
| --- | --- | --- | --- |
| | ## 方法| | |
| joinType | 設置join類型 | ($type) | this |
| eagerly | 預載入關聯查詢(JOIN方式) | (Query $query, string $relation, $field = true, string $joinType = '', Closure $closure = null, bool $first = false) | void |
| eagerlyResultSet | 預載入關聯查詢(數據集) | (array &$resultSet, string $relation, array $subRelation = [], Closure $closure = null, array $cache = [], bool $join = false) | void |
| eagerlyResult | 預載入關聯查詢(數據) | (Model $result, string $relation, array $subRelation = [], Closure $closure = null, array $cache = [], bool $join = false) | void |
| save | 保存(新增)當前關聯數據對象 | ($data, bool $replace = true) | Model \| false |
| ~~setEagerlyType~~ | 設置預載入方式 | ($type) | this |
| ~~getEagerlyType~~ | 獲取預載入方式 | void | integer |
| bind | 綁定關聯表的屬性到父模型屬性 | ($attr) | this |
| getBindAttr | 獲取綁定屬性 | void | array |
| ~~relationCount~~| 關聯統計 | ($result, $closure) | integer |
| | **抽象方法(protected)** | | |
| eagerlySet | || |
| eagerlyOne | || |
## **HasOne**
| 方法名(public) | 描述 | 參數 | 返回值 |
| --- | --- | --- | --- |
| | **方法** | | |
| getRelation | 延遲獲取關聯數據 | ($subRelation = '', $closure = null) |Model |
| getRelationCountQuery | 創建關聯統計子查詢 | (Closure $closure = null, string $aggregate = 'count', string $field = '*', string &$name = null) | string |
| relationCount | 關聯統計 | (Model $result, Closure $closure = null, string $aggregate = 'count', string $field = '*', string &$name = null) | integer |
| has | 根據關聯條件查詢當前模型 | (string $operator = '>=', int $count = 1, string $id = '*', string $joinType = '', Query $query = null) | Query |
| hasWhere | 根據關聯條件查詢當前模型 | ($where = [], $fields = null, string $joinType = '', Query $query = null) | Query |
## **BelongsTo**
| 方法名(public) | 描述 | 參數 | 返回值 |
| --- | --- | --- | --- |
| | ## 方法| | |
| getRelation | 延遲獲取關聯數據 | (array $subRelation = [], Closure $closure = null) | Model |
| getRelationCountQuery | 創建關聯統計子查詢 | (Closure $closure = null, string $aggregate = 'count', string $field = '*', string &$name = null) | string |
| relationCount | 關聯統計 | (Model $result, Closure $closure = null, string $aggregate = 'count', string $field = '*', string &$name = null) | integer |
| has | 根據關聯條件查詢當前模型 | (string $operator = '>=', int $count = 1, string $id = '*', string $joinType = '', Query $query = null) | Query |
| hasWhere | 根據關聯條件查詢當前模型 | ($where = [], $fields = null, string $joinType = '', Query $query = null) | Query |
| associate | 添加關聯數據 | (Model $model) | Model |
| dissociate | 注銷關聯數據 | void | Model |
## **HasMany**
| 方法名(public) | 描述 | 參數 | 返回值 |
| --- | --- | --- | --- |
| | **方法**| | |
| getRelation | 延遲獲取關聯數據 | (array $subRelation = [], Closure $closure = null) | Collection |
| eagerlyResultSet | 預載入關聯查詢(數據集) | (array &$resultSet, string $relation, array $subRelation = [], Closure $closure = null, array $cache = []) | void |
| eagerlyResult | 預載入關聯查詢(數據) | (Model $result, string $relation, array $subRelation = [], Closure $closure = null, array $cache = []) | void |
| has | 根據關聯條件查詢當前模型 | has(string $operator = '>=', int $count = 1, string $id = '*', string $joinType = 'INNER', Query $query = null) | Query |
| hasWhere | 根據關聯條件查詢當前模型 | ($where = [], $fields = null, string $joinType = '', Query $query = null) | Query |
| relationCount | 關聯統計 |(Model $result, Closure $closure = null, string $aggregate = 'count', string $field = '*', string &$name = null) | integer |
| getRelationCountQuery | 創建關聯統計子查詢 | (Closure $closure = null, string $aggregate = 'count', string $field = '*', string &$name = null) | string |
| save | 保存(新增)當前關聯數據對象 | ($data, bool $replace = true) | Model \| false |
| make | 創建關聯對象實例 | ($data = \[\]) | Model |
| saveAll | 批量保存當前關聯數據對象 | (iterable $dataSet, bool $replace = true) | integer |
## **HasManyThrough**
| 方法名(public) | 描述 | 參數 | 返回值 |
| --- | --- | --- | --- |
| | **方法**| | |
| getRelation | 延遲獲取關聯數據 | (array $subRelation = [], Closure $closure = null) | Collection |
| eagerlyResultSet | 預載入關聯查詢(數據集) | (array &$resultSet, string $relation, array $subRelation = [], Closure $closure = null, array $cache = []) | void |
| eagerlyResult | 預載入關聯查詢(數據) | (Model $result, string $relation, array $subRelation = [], Closure $closure = null, array $cache = []) | void |
| has | 根據關聯條件查詢當前模型 | (string $operator = '>=', int $count = 1, string $id = '*', string $joinType = '', Query $query = null) | Query |
| hasWhere | 根據關聯條件查詢當前模型 | ($where = [], $fields = null, string $joinType = '', Query $query = null) | Query |
| relationCount | 關聯統計 |(Model $result, Closure $closure = null, string $aggregate = 'count', string $field = '*', string &$name = null) | integer |
| getRelationCountQuery | 創建關聯統計子查詢 | (Closure $closure = null, string $aggregate = 'count', string $field = '*', string &$name = null) | string |
## **HasManyThrough**
HasOneThrough的基類
| 方法名(public) | 描述 | 參數 | 返回值 |
| --- | --- | --- | --- |
| | **方法**| | |
| getRelation | 延遲獲取關聯數據 | (array $subRelation = [], Closure $closure = null) | Collection |
| eagerlyResultSet | 預載入關聯查詢(數據集) | (array &$resultSet, string $relation, array $subRelation = [], Closure $closure = null, array $cache = []) | void |
| eagerlyResult | 預載入關聯查詢(數據) | (Model $result, string $relation, array $subRelation = [], Closure $closure = null, array $cache = []) | void |
## **HasOneThrough**
| 方法名(public) | 描述 | 參數 | 返回值 |
| --- | --- | --- | --- |
| | **方法**| | |
| getRelation | 延遲獲取關聯數據 | (array $subRelation = [], Closure $closure = null) | Model |
| eagerlyResultSet | 預載入關聯查詢(數據集) | (array &$resultSet, string $relation, array $subRelation = [], Closure $closure = null, array $cache = []) | void |
| eagerlyResult | 預載入關聯查詢(數據) | (Model $result, string $relation, array $subRelation = [], Closure $closure = null, array $cache = []) | void |
## **MorphMany**
多態一對多關聯
| 方法名(public) | 描述 | 參數 | 返回值 |
| --- | --- | --- | --- |
| | **方法**| | |
| getRelation | 延遲獲取關聯數據 | (array $subRelation = [], Closure $closure = null) | Collection |
| eagerlyResultSet | 預載入關聯查詢(數據集) | (array &$resultSet, string $relation, array $subRelation = [], Closure $closure = null, array $cache = []) | void |
| eagerlyResult | 預載入關聯查詢(數據) | (Model $result, string $relation, array $subRelation = [], Closure $closure = null, array $cache = []) | void |
| has | 根據關聯條件查詢當前模型 | has(string $operator = '>=', int $count = 1, string $id = '*', string $joinType = 'INNER', Query $query = null) | Query |
| hasWhere | 根據關聯條件查詢當前模型 | ($where = [], $fields = null, string $joinType = '', Query $query = null) | Query |
| relationCount | 關聯統計 |(Model $result, Closure $closure = null, string $aggregate = 'count', string $field = '*', string &$name = null) | integer |
| getRelationCountQuery | 創建關聯統計子查詢 | (Closure $closure = null, string $aggregate = 'count', string $field = '*', string &$name = null) | string |
| save | 保存(新增)當前關聯數據對象 | ($data, bool $replace = true) | Model \| false |
| make | 創建關聯對象實例 | ($data = \[\]) | Model |
| saveAll | 批量保存當前關聯數據對象 | (iterable $dataSet, bool $replace = true) | integer |
## **MorphOne**
多態一對一關聯類
| 方法名(public) | 描述 | 參數 | 返回值 |
| --- | --- | --- | --- |
| | **方法**| | |
| getRelation | 延遲獲取關聯數據 | (array $subRelation = [], Closure $closure = null) | Collection |
| eagerlyResultSet | 預載入關聯查詢(數據集) | (array &$resultSet, string $relation, array $subRelation = [], Closure $closure = null, array $cache = []) | void |
| eagerlyResult | 預載入關聯查詢(數據) | (Model $result, string $relation, array $subRelation = [], Closure $closure = null, array $cache = []) | void |
| has | 根據關聯條件查詢當前模型 | has(string $operator = '>=', int $count = 1, string $id = '*', string $joinType = 'INNER', Query $query = null) | Query |
| hasWhere | 根據關聯條件查詢當前模型 | ($where = [], $fields = null, string $joinType = '', Query $query = null) | Query |
| relationCount | 關聯統計 |(Model $result, Closure $closure = null, string $aggregate = 'count', string $field = '*', string &$name = null) | integer |
| getRelationCountQuery | 創建關聯統計子查詢 | (Closure $closure = null, string $aggregate = 'count', string $field = '*', string &$name = null) | string |
| save | 保存(新增)當前關聯數據對象 | ($data, bool $replace = true) | Model \| false |
| make | 創建關聯對象實例 | ($data = \[\]) | Model |
## **MorphTo**
多態關聯類
| 方法名(public) | 描述 | 參數 | 返回值 |
| --- | --- | --- | --- |
| | **方法**| | |
| getModel | 獲取當前的關聯模型類的實例 | void | Model |
| getRelation | 延遲獲取關聯數據 | (array $subRelation = [], Closure $closure = null) | Model |
| has | 根據關聯條件查詢當前模型 | (string $operator = '>=', int $count = 1, string $id = '*', string $joinType = '', Query $query = null) | Query |
| hasWhere | 這里不支持拋出錯誤 | | |
| setAlias | 設置多態別名 |(array $alias) | this |
| removeOption | 移除關聯查詢參數 | void | this |
| eagerlyResultSet | 預載入關聯查詢(數據集) | (array &$resultSet, string $relation, array $subRelation = [], Closure $closure = null, array $cache = []) | void |
| eagerlyResult | 預載入關聯查詢(數據) | (Model $result, string $relation, array $subRelation = [], Closure $closure = null, array $cache = []) | void |
| relationCount | 關聯統計 |(Model $result, Closure $closure = null, string $aggregate = 'count', string $field = '*', string &$name = null) | integer |
| associate | 添加關聯數據 | (Model $model, string $type = '') | Model |
| dissociate | 注銷關聯數據 | void | Model |
## **BelongsToMany**
| 方法名(public) | 描述 | 參數 | 返回值 |
| --- | --- | --- | --- |
| | **方法**| | |
| pivot | 設置中間表模型 | (string $pivot) | this |
| name | 設置中間表數據名稱 | (string $name) | this |
| select | 重載select方法 | ($data = null) | Collection |
| paginate | 重載paginate方法 | ($listRows = null, $simple = false) | Paginator |
| find | 重載find方法 | ($data = null) | Model |
| selectOrFail |查找多條記錄 如果不存在則拋出異常 | ($data = null) | Collection |
| findOrFail | 查找單條記錄 如果不存在則拋出異常 | ($data = null) | Model |
| getRelation | 延遲獲取關聯數據 | (array $subRelation = [], Closure $closure = null) | Collection |
| has | 根據關聯條件查詢當前模型 | (string $operator = '>=', $count = 1, $id = '*', string $joinType = 'INNER', Query $query = null) | Query |
| hasWhere | 根據關聯條件查詢當前模型 | ($where = [], $fields = null, string $joinType = '', Query $query = null) | Query |
| wherePivot | 設置中間表的查詢條件 | ($field, $op = null, $condition = null) | this |
| eagerlyResultSet | 預載入關聯查詢(數據集) | (array &$resultSet, string $relation, array $subRelation, Closure $closure = null, array $cache = []) | void |
| eagerlyResult | 預載入關聯查詢(數據) | (Model $result, string $relation, array $subRelation, Closure $closure = null, array $cache = []) | void |
| relationCount | 關聯統計 |(Model $result, Closure $closure = null, string $aggregate = 'count', string $field = '*', string &$name = null) | integer |
| getRelationCountQuery | 創建關聯統計子查詢 | (Closure $closure = null, string $aggregate = 'count', string $field = '*', string &$name = null) | string |
| save | 保存(新增)當前關聯數據對象 | ($data, array $pivot = []) | array \| Pivot |
| saveAll| 批量保存當前關聯數據對象 | (iterable $dataSet, array $pivot = [], bool $samePivot = false) | array \| false|
| attach| 附加關聯的一個中間表數據 | ($data, array $pivot = []) | array \| Pivot |
| attached| 判斷是否存在關聯數據 | ($data) | array \| false |
| detach| 解除關聯的一個中間表數據 | ($data = null, bool $relationDel = false) | integer |
| sync| 數據同步 | (array $ids, bool $detaching = true) | array |
## **MorphToMany**
| 方法名(public) | 描述 | 參數 | 返回值 |
| --- | --- | --- | --- |
| | **方法**| | |
| eagerlyResultSet | 預載入關聯查詢(數據集) | (array &$resultSet, string $relation, array $subRelation, Closure $closure = null, array $cache = []) | void |
| eagerlyResult | 預載入關聯查詢(數據) | (Model $result, string $relation, array $subRelation, Closure $closure = null, array $cache = []) | void |
| relationCount | 關聯統計 |(Model $result, Closure $closure = null, string $aggregate = 'count', string $field = '*', string &$name = null) | integer |
| getRelationCountQuery | 創建關聯統計子查詢 | (Closure $closure = null, string $aggregate = 'count', string $field = '*', string &$name = null) | string |
| attach| 附加關聯的一個中間表數據 | ($data, array $pivot = []) | array \| Pivot |
| attached| 判斷是否存在關聯數據 | ($data) | array \| false |
| detach| 解除關聯的一個中間表數據 | ($data = null, bool $relationDel = false) | integer |
| sync| 數據同步 | (array $ids, bool $detaching = true) | array |
## **ModelRelationQuery**
| 方法名(public) | 描述 | 參數 | 返回值 |
| --- | --- | --- | --- |
| | **方法**| | |
| model | 指定模型對象實例 | (Model $model) | $this |
| getModel | 獲取當前的模型對象 | void | Model|null |
| hidden | 設置需要隱藏的輸出屬性 | (array $hidden) | this |
| visible | 設置需要輸出的屬性 | (array $visible) | (array $hidden) |
| append | 設置需要追加輸出的屬性 | (array $append) | this |
| scope | 添加查詢范圍 | ($scope, ...$args) | this |
| relation | 設置關聯查詢 | (array $relation) | this |
| withSearch | 使用搜索器條件搜索字段 | ($fields, $data = [], string $prefix = '') | this |
| withAttr | 設置數據字段獲取器 | ($name, callable $callback = null) | this |
| with | 關聯預載入 In方式,參數是關聯方法名,參數可以是數組或者字符串 | ($with) | this |
| withJoin | 關聯預載入 JOIN方式 | ($with, string $joinType = '') | this |
| withCache | 關聯緩存 | withCache($relation = true, $key = true, $expire = null, string $tag = null) | this |
| withCount | 關聯統計 | ($relation, bool $subQuery = true) | this |
| withSum | 關聯統計Sum | ($relation, string $field, bool $subQuery = true) | this |
| withMax | 關聯統計Max | ($relation, string $field, bool $subQuery = true) | this |
| withMin | 關聯統計Min | ($relation, string $field, bool $subQuery = true) | this |
| withAvg | 關聯統計Avg | ($relation, string $field, bool $subQuery = true) | this |
| has | 根據關聯條件查詢當前模型 | (string $relation, string $operator = '>=', int $count = 1, string $id = '*', string $joinType = '') | this |
| hasWhere | 根據關聯條件查詢當前模型 | (string $relation, $where = [], string $fields = '*', string $joinType = '') | this |
>$subQuery參數 是否使用子查詢
$relation 關聯方法名
```
//protected static $macro = [];
public static function macro(string $method, Closure $closure)
{
if (!isset(static::$macro[static::class])) {
static::$macro[static::class] = [];
}
static::$macro[static::class][$method] = $closure;
}
public function __call($method, $args)
{
if (isset(static::$macro[static::class][$method])) {
return call_user_func_array(static::$macro[static::class][$method]->bindTo($this, static::class), $args);
}
if ('withattr' == strtolower($method)) {
return call_user_func_array([$this, 'withAttribute'], $args);
}
return call_user_func_array([$this->db(), $method], $args);
}
public static function __callStatic($method, $args)
{
if (isset(static::$macro[static::class][$method])) {
return call_user_func_array(static::$macro[static::class][$method]->bindTo(null, static::class), $args);
}
$model = new static();
return call_user_func_array([$model->db(), $method], $args);
}
```
- 空白目錄
- php語法結構
- 安裝與更新
- 開啟調試模式及代碼跟蹤器
- 架構
- 源碼分析
- 應用初始化
- 請求流程
- 中間件源碼分析
- 請求處理源碼分析
- Request源碼分析
- 模板編譯流程
- 路由與請求流程
- 容器
- 獲取目錄位置
- 入口文件
- 多應用模式及URL訪問
- 依賴注入與容器
- 容器屬性及方法
- Container
- App
- facade
- 中間件(middleware)
- 系統服務
- extend 擴展類庫
- 筆記
- 配置
- env配置定義及獲取
- 配置文件的配置獲取
- 單應用模式-(配置)文件目錄結構(默認)
- 多應用模式(配置)文件目錄結構(配置文件)
- 配置文件
- 應用配置:app.php
- 緩存配置: cache.php
- 數據庫配置:database.php
- 路由和URL配置:route.php
- Cookie配置:cookie.php
- Session配置:session.php
- 命令行配置:console.php
- 多語言配置:lang.php
- 日志配置:log.php
- 頁面Trace配置:trace.php
- 磁盤配置: filesystem.php
- 中間件配置:middleware.php
- 視圖配置:view.php
- 改成用yaconf配置
- 事件
- 例子:省略事件類的demo
- 例子2:完整事件類
- 例子3:事件訂閱,監聽多個事件
- 解析
- 路由
- 路由定義
- 路由地址
- 變量規則
- MISS路由
- URL生成
- 閉包支持
- 路由參數
- 路由中間件
- 路由分組
- 資源路由
- 注解路由
- 路由綁定
- 域名路由
- 路由緩存
- 跨域路由
- 控制器
- 控制器定義
- 空控制器、空操作
- 空模塊處理
- RESTFul資源控制器
- 控制器中間件
- 請求對象Request(url參數)
- 請求信息
- 獲取輸入變量($_POST、$_GET等)
- 請求類型的獲取與偽裝
- HTTP頭信息
- 偽靜態
- 參數綁定
- 請求緩存
- 響應對象Response
- 響應輸出
- 響應參數
- 重定向
- 文件下載
- 錯誤頁面的處理辦法
- 應用公共文件common.php
- 模型
- 模型定義及常規屬性
- 模型數據獲取與模型賦值
- 查詢
- 數據集
- 增加
- 修改
- 刪除
- 條件
- 查詢范圍scope
- 獲取器
- 修改器
- 搜索器
- 軟刪除
- 模型事件
- 關聯預載入
- 模型關聯
- 一對一關聯
- 一對多關聯
- 多對多關聯
- 自動時間戳
- 事務
- 數據庫
- 查詢構造器
- 查詢合集
- 子查詢
- 聚合查詢
- 時間查詢
- 視圖查詢(比join簡單)
- 獲取查詢參數
- 快捷方法
- 動態查詢
- 條件查詢
- 打印sql語句
- 增
- 刪
- 改
- 查
- 鏈式操作
- 查詢表達式
- 分頁查詢
- 原生查詢
- JSON字段
- 鏈接數據庫配置
- 分布式數據庫
- 查詢事件
- Db獲取器
- 事務操作
- 存儲過程
- Db數據集
- 數據庫驅動
- 視圖
- 模板
- 模板配置
- 模板位置
- 模板渲染
- 模板變量與賦值(assign)
- 模板輸出替換
- url生成
- 模板詳解
- 內置標簽
- 三元運算
- 變量輸出
- 函數輸出
- Request請求參數
- 模板注釋及原樣輸出
- 模板繼承
- 模板布局
- 原生PHP
- 模板引擎
- 視圖過濾
- 視圖驅動
- 驗證
- 驗證進階之最終版
- 錯誤和日志
- 異常處理
- 日志處理
- 調試
- 調試模式
- Trace調試
- SQL調試
- 變量調試
- 遠程調試
- 雜項
- 緩存
- Session
- Cookie
- 多語言
- 上傳
- 擴展說明
- N+1查詢
- TP類庫
- 擴展類庫
- 數據庫遷移工具
- Workerman
- think助手工具庫
- 驗證碼
- Swoole
- request
- app
- Response
- View
- Validate
- Config
- 命令行
- 助手函數
- 升級指導(功能的添加與刪除說明)
- siyucms
- 開始
- 添加頁面流程
- 列表頁加載流程
- 彈出框
- 基礎控制器
- 基礎模型
- 快速構建
- 表單form構建
- 表格table構建
- MakeBuilder
- 前端組件
- 日期組件
- layer 彈層組件
- Moment.js 日期處理插件
- siyucms模板布局
- 函數即其變量
- 前端頁面
- $.operate.方法
- $.modal.方法:彈出層
- $.common.方法:通用方法
- 被cms重寫的表格options
- 自定義模板
- 搜索框
- 自定義form表單
- 獲取表單搜索參數并組裝為url字符串