laravel 5.1 Model 屬性詳解

<?php

 

namespace Illuminate\Database\Eloquent;

 

/**

 * 下面提到某些詞的含義:

 *      1、覆蓋: 在繼承該類 \Illuminate\Database\Eloquent\Model 的自定義的模型類中, 定義一個同名 field,值不一樣

 */

abstract class Model1 implements ArrayAccess, Arrayable, Jsonable, JsonSerializable, QueueableEntity, UrlRoutable

{

    /**

     * 數據庫連接,我們在 config/database.php 裏面的 connections 數組定義的連接。

     * usage:

     *      (new xxModel)->connection('mysql')

     *      DB::connection('pgsql')

     *      或者在模型定義裏面覆蓋該屬性,如: protected $connection = 'pgsql';

     */

    protected $connection;

 

    /**

     * 模型關聯的數據庫表名,默認是 模型名->下劃線命名->複數形式,

     * 好比如定義了 User 模型,class User extend Model,那麼默認的表名是 users。

     *

     * usage:

     *      (new xxModel)->setTable('xxx')

     *      (new xxModel)->getModel()

     *      或者在模型定義的時候覆蓋該屬性,如: protected $table = 'tb_user';

     */

    protected $table;

 

    /**

     * 主鍵字段,默認爲 id,也可以覆蓋該屬性

     */

    protected $primaryKey 'id';

 

    /**

     * 不知道哪裏用到,除了該文件的 getter 和 setter

     */

    protected $perPage = 15;

 

    /**

     * 主鍵是否默認自增長

     */

    public $incrementing = true;

 

    /**

     * 是否由模型去維護時間戳字段,如果我們想手動去維護,可以設置爲 false

     * usage:

     *      默認的時間戳字段是 created_at、updated_at,

     *      我們如果想要使用自定義的字段,則要在模型裏面覆蓋 CREATED_AT、UPDATED_AT 這兩個常量(下面有定義)

     * 其他:

     *      默認使用 mysql 的 datetime 類型,如果需要更改爲 10 位整型,可以設置 protected $dateFormat = 'U'; ()

     */

    public $timestamps = true;

 

    /**

     * 我們在給模型對象設置非 public 屬性的時候,會通過 setAttributes 方法,保存到該數組中

     * usage:

     *      $user = new User();

     *      $user->name = 'laravel';

     *      User 中沒有定義 public $name 的時候, $attributes 就會多了 'name' => 'laravel' 的鍵值對

     */

    protected $attributes = [];

 

    /**

     * 保存模型的原始數據,後續修改模型屬性只會修改 $attributes,以便偵測變化

     */

    protected $original = [];

 

    /**

     * 模型的關聯數據

     */

    protected $relations = [];

 

    /**

     * 隱藏的屬性,我們調用模型的 toArray 方法的時候不會得到該數組中的屬性,

     * 如果需要也得到隱藏屬性,可以通過 withHidden 方法

     */

    protected $hidden = [];

 

    /**

     * 與 hidden 數組作用差不多,共同作用

     */

    protected $visible = [];

 

    /**

     * 其中一個用法,根據現有某幾個屬性,計算出新屬性,並在 模型 toArray 的時候顯示

     * usage:

     *      模型裏面定義: protected $appends = ['full_name'];

     *      public function getFullNameAttribute() { return $this->firstName . ' ' . $this->lastName; }

     */

    protected $appends = [];

 

    /**

     * mass assignment 的時候可以批量設置的屬性,目的是防止用戶提交我們不想更新的字段

     * 注意:

     *      和 $guarded 同時使用的時候, $guard 設置的會無效

     */

    protected $fillable = [];

 

    /**

     * 不能批量賦值的屬性

     */

    protected $guarded = ['*'];

 

    /**

     * 需要進行時間格式轉換的字段

     * 應用場景:

     *      一般情況我們只定義了 created_at、updated_at,我們還可能會保存用戶註冊時間這些,register_time,

     *      這樣我們就可以定義,protected $dates = ['register_time'];

     * 好比如:

     *      我們定義的 $dateFormat 爲 mysql 的 datetime 格式,我們即使把 register_time 設置爲 time(),

     *      實際保存的其實是 datetime 格式的

     */

    protected $dates = [];

 

    /**

     * created_at、updated_at、$dates數組 進行時間格式轉換的時候使用的格式

     */

    protected $dateFormat;

 

    /**

     * 自動格式轉換,定義方式: protected $casts = ['info' => 'json'];

     * 所有可用格式: int、integer、real、float、double、string、bool、boolean、

     *              object、array、json、collection、date、datetime

     * 應用場景:

     *      我們想要保存某些特殊格式到數據庫(如json、object),我們可以使用該數組對我們的數據進行自動轉換,

     * usage:

     *      $user = User::create([

     *          'name' => 'ruby',

     *          'info' => ['city' => 'Guangzhou']

     *      ]);

     *

     *      $query_user = User::find($user['id']);

     *      dd($query_user->info);

     * 這裏我們可以看數據庫保存的 info 字段,實際上是 json 編碼的,並且取出來的是 json 解碼後的數據

     */

    protected $casts = [];

 

    /**

     * 需要同步更新 updated_at 的關聯,調用 save 方法的時候會更新該數組裏面定義的關聯的 updated_at 字段

     */

    protected $touches = [];

 

    /**

     * todo 自定義事件,目前還沒用過  -_-

     */

    protected $observables = [];

 

    /**

     * 需要預加載的關聯

     */

    protected $with = [];

 

    /**

     * The class name to be used in polymorphic relations.

     *

     * todo 也不知道

     */

    protected $morphClass;

 

    /**

     * 模型是否存在

     */

    public $exists = false;

 

    /**

     * 判斷模型是否是當前請求插入的

     */

    public $wasRecentlyCreated = false;

 

    /**

     * 模型屬性名是否是下劃線形式的

     */

    public static $snakeAttributes = true;

 

    /**

     * 用來建立數據庫連接

     *

     * @var \Illuminate\Database\ConnectionResolverInterface

     */

    protected static $resolver;

 

    /**

     * 用來分發模型事件

     *

     * @var \Illuminate\Contracts\Events\Dispatcher

     */

    protected static $dispatcher;

 

    /**

     * 模型 new 的時候調用模型的 bootXXX 方法, XXX 是模型名稱

     */

    protected static $booted = [];

 

    /**

     * 全局查詢條件,需要定義

     *

     * @see \Illuminate\Database\Eloquent\ScopeInterface

     */

    protected static $globalScopes = [];

 

    /**

     * 批量設置屬性的時候是否不需要篩選字段

     */

    protected static $unguarded = false;

 

    /**

     * 緩存 getXXAttribute 的值

     */

    protected static $mutatorCache = [];

 

    /**

     * 多對多關聯方法,不知道哪裏用了

     */

    public static $manyMethods = ['belongsToMany''morphToMany''morphedByMany'];

 

    /**

     * 創建時間戳字段名稱

     */

    const CREATED_AT = 'created_at';

 

    /**

     * 更新時間戳字段名稱

     */

    const UPDATED_AT = 'updated_at';

}

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章