亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

使用雄辯獲取模型子類型的實例

使用雄辯獲取模型子類型的實例

PHP
瀟瀟雨雨 2022-09-17 21:32:43
我有一個基于表格的模型。Animalanimal此表包含一個字段,該字段可以包含諸如貓或狗之類的值。type我希望能夠創建諸如以下內容的對象:class Animal extends Model { }class Dog extends Animal { }class Cat extends Animal { }然而,能夠像這樣獲取動物:$animal = Animal::find($id);但是,根據字段的實例或取決于字段,我可以檢查使用或將使用類型提示方法。原因是90%的代碼是共享的,但一個可以吠叫,另一個可以喵喵叫。$animalDogCattypeinstance of我知道我可以做到,但這不是我想要的:我只有在它被獲取后才能確定對象的類型。我也可以獲取 Animal,然后在正確的對象上運行,但這是在進行兩次數據庫調用,我顯然不想要。Dog::find($id)find()我試圖尋找一種方法來“手動”實例化一個雄辯的模型,比如《動物的狗》,但我找不到任何相應的方法。我錯過了任何想法或方法嗎?
查看完整描述

5 回答

?
斯蒂芬大帝

TA貢獻1827條經驗 獲得超8個贊

正如OP在他的評論中所說:數據庫設計已經設置好了,因此Laravel的多態關系似乎不是這里的一個選項。

我喜歡克里斯·尼爾(Chris Neal)的答案,因為我最近不得不做類似的事情(編寫我自己的數據庫驅動程序來支持dbase / DBF文件的Eloquent),并且在Laravel的雄辯ORM的內部積累了豐富的經驗。

我添加了我的個人風格,使代碼更加動態,同時保持每個模型的顯式映射。

我快速測試的支持功能:

  • Animal::find(1)按照您的問題中提出的問題工作

  • Animal::all()也可以工作

  • Animal::where(['type' => 'dog'])->get()將返回 -objects 作為集合AnimalDog

  • 使用此特征的每個雄辯類的動態對象映射

  • 回退到 -model,以防未配置映射(或數據庫中出現新映射)Animal

弊:

  • 它正在重寫模型的內部和完全(復制和粘貼)。這意味著如果從框架到此成員函數有任何更新,則需要手動采用代碼。newInstance()newFromBuilder()

我希望它有所幫助,并且我會在您的場景中提出任何建議,問題和其他用例。以下是它的用例和示例:

class Animal extends Model

{

    use MorphTrait; // You'll find the trait in the very end of this answer


    protected $morphKey = 'type'; // This is your column inside the database

    protected $morphMap = [ // This is the value-to-class mapping

        'dog' => AnimalDog::class,

        'cat' => AnimalCat::class,

    ];


}


class AnimalCat extends Animal {}

class AnimalDog extends Animal {}

這是如何使用它的一個例子,下面是它的相應結果:


$cat = Animal::find(1);

$dog = Animal::find(2);

$new = Animal::find(3);

$all = Animal::all();


echo sprintf('ID: %s - Type: %s - Class: %s - Data: %s', $cat->id, $cat->type, get_class($cat), $cat, json_encode($cat->toArray())) . PHP_EOL;

echo sprintf('ID: %s - Type: %s - Class: %s - Data: %s', $dog->id, $dog->type, get_class($dog), $dog, json_encode($dog->toArray())) . PHP_EOL;

echo sprintf('ID: %s - Type: %s - Class: %s - Data: %s', $new->id, $new->type, get_class($new), $new, json_encode($new->toArray())) . PHP_EOL;


dd($all);

這將產生以下結果:


ID: 1 - Type: cat - Class: App\AnimalCat - Data: {"id":1,"type":"cat"}

ID: 2 - Type: dog - Class: App\AnimalDog - Data: {"id":2,"type":"dog"}

ID: 3 - Type: new-animal - Class: App\Animal - Data: {"id":3,"type":"new-animal"}


// Illuminate\Database\Eloquent\Collection {#1418

//  #items: array:2 [

//    0 => App\AnimalCat {#1419

//    1 => App\AnimalDog {#1422

//    2 => App\Animal {#1425

如果你想使用這里,你當然是它的完整代碼:MorphTrait


<?php namespace App;


trait MorphTrait

{


    public function newInstance($attributes = [], $exists = false)

    {

        // This method just provides a convenient way for us to generate fresh model

        // instances of this current model. It is particularly useful during the

        // hydration of new objects via the Eloquent query builder instances.

        if (isset($attributes['force_class_morph'])) {

            $class = $attributes['force_class_morph'];

            $model = new $class((array)$attributes);

        } else {

            $model = new static((array)$attributes);

        }


        $model->exists = $exists;


        $model->setConnection(

            $this->getConnectionName()

        );


        $model->setTable($this->getTable());


        return $model;

    }


    /**

     * Create a new model instance that is existing.

     *

     * @param array $attributes

     * @param string|null $connection

     * @return static

     */

    public function newFromBuilder($attributes = [], $connection = null)

    {

        $newInstance = [];

        if ($this->isValidMorphConfiguration($attributes)) {

            $newInstance = [

                'force_class_morph' => $this->morphMap[$attributes->{$this->morphKey}],

            ];

        }


        $model = $this->newInstance($newInstance, true);


        $model->setRawAttributes((array)$attributes, true);


        $model->setConnection($connection ?: $this->getConnectionName());


        $model->fireModelEvent('retrieved', false);


        return $model;

    }


    private function isValidMorphConfiguration($attributes): bool

    {

        if (!isset($this->morphKey) || empty($this->morphMap)) {

            return false;

        }


        if (!array_key_exists($this->morphKey, (array)$attributes)) {

            return false;

        }


        return array_key_exists($attributes->{$this->morphKey}, $this->morphMap);

    }

}


查看完整回答
反對 回復 2022-09-17
?
拉丁的傳說

TA貢獻1789條經驗 獲得超8個贊

您可以在拉拉維爾中使用多態關系,如官方拉拉維爾文檔中所述。這是你如何做到這一點。


定義給定模型中的關系


class Animal extends Model{

    public function animable(){

        return $this->morphTo();

    }

}


class Dog extends Model{

    public function animal(){

        return $this->morphOne('App\Animal', 'animable');

    }

}


class Cat extends Model{

    public function animal(){

        return $this->morphOne('App\Animal', 'animable');

    }

}

在這里,您需要在 animals 表中使用兩列,第一列是animable_type,另一列是animable_id以確定在運行時附加到它的模型的類型。


您可以獲取給定的狗或貓模型,


$animal = Animal::find($id);

$anim = $animal->animable; //this will return either Cat or Dog Model

之后,您可以使用實例of檢查對象的類。$anim


如果您在應用程序中添加其他動物類型(即狐貍或獅子),則此方法將幫助您進行將來的擴展。它將在不更改代碼庫的情況下工作。這是滿足您要求的正確方法。然而,在不使用多態關系的情況下,沒有替代方法可以實現多態性和渴望加載在一起。如果不使用多態關系,則最終會得到多個數據庫調用。但是,如果您有一個區分模式類型的列,則可能是您有一個錯誤的結構化架構。我建議你改進它,如果你想簡化它以備將來的發展。


重寫模型的內部新實例()和 newFromBuilder() 不是一種好/推薦的方法,一旦你從框架中獲得更新,你必須重新處理它。


查看完整回答
反對 回復 2022-09-17
?
開滿天機

TA貢獻1786條經驗 獲得超13個贊

我認為您可以重寫模型上的方法,并從屬性中檢查類型,然后初始化相應的模型。newInstanceAnimal


    public function newInstance($attributes = [], $exists = false)

    {

        // This method just provides a convenient way for us to generate fresh model

        // instances of this current model. It is particularly useful during the

        // hydration of new objects via the Eloquent query builder instances.

        $modelName = ucfirst($attributes['type']);

        $model = new $modelName((array) $attributes);


        $model->exists = $exists;


        $model->setConnection(

            $this->getConnectionName()

        );


        $model->setTable($this->getTable());


        $model->mergeCasts($this->casts);


        return $model;

    }

您還需要重寫該方法。newFromBuilder



    /**

     * Create a new model instance that is existing.

     *

     * @param  array  $attributes

     * @param  string|null  $connection

     * @return static

     */

    public function newFromBuilder($attributes = [], $connection = null)

    {

        $model = $this->newInstance([

            'type' => $attributes['type']

        ], true);


        $model->setRawAttributes((array) $attributes, true);


        $model->setConnection($connection ?: $this->getConnectionName());


        $model->fireModelEvent('retrieved', false);


        return $model;

    }


查看完整回答
反對 回復 2022-09-17
?
躍然一笑

TA貢獻1826條經驗 獲得超6個贊

如果您真的想這樣做,可以在 Animal 模型中使用以下方法。


<?php


namespace App;


use Illuminate\Database\Eloquent\Model;


class Animal extends Model

{


    // other code in animal model .... 


    public static function __callStatic($method, $parameters)

    {

        if ($method == 'find') {

            $model = parent::find($parameters[0]);


            if ($model) {

                switch ($model->type) {

                    case 'dog':

                        return new \App\Dog($model->attributes);

                    case 'cat':

                        return new \App\Cat($model->attributes);

                }

                return $model;

            }

        }


        return parent::__callStatic($method, $parameters);

    }

}


查看完整回答
反對 回復 2022-09-17
?
小怪獸愛吃肉

TA貢獻1852條經驗 獲得超1個贊

我想我知道你在找什么。請考慮使用 Laravel 查詢范圍的優雅解決方案,有關其他信息,請參閱 https://laravel.com/docs/6.x/eloquent#query-scopes:


創建保存共享邏輯的父類:

class Animal extends \Illuminate\Database\Eloquent\Model

{

    const TYPE_DOG = 'dog';

    const TYPE_CAT = 'cat';

}

創建具有全局查詢作用域和事件處理程序的子級(或多個級):saving

class Dog extends Animal

{

    public static function boot()

    {

        parent::boot();


        static::addGlobalScope('type', function(\Illuminate\Database\Eloquent\Builder $builder) {

            $builder->where('type', self::TYPE_DOG);

        });


        // Add a listener for when saving models of this type, so that the `type`

        // is always set correctly.

        static::saving(function(Dog $model) {

            $model->type = self::TYPE_DOG;

        });

    }

}

(這同樣適用于另一個類,只需替換常量)Cat


全局查詢作用域充當默認查詢修改,以便類將始終查找包含 的記錄。Dogtype='dog'


假設我們有 3 條記錄:


- id:1 => Cat

- id:2 => Dog

- id:3 => Mouse

現在調用將導致 ,因為默認查詢范圍將找不到 哪個是 。調用 和 都將工作,盡管只有最后一個會給你一個實際的 Cat 對象。Dog::find(1)nullid:1CatAnimal::find(1)Cat::find(1)


這種設置的好處是,您可以使用上面的類來創建如下關系:


class Owner

{

    public function dogs()

    {

        return $this->hasMany(Dog::class);

    }

}

這種關系會自動只給你所有的動物與(以類的形式)。將自動應用查詢范圍。type='dog'Dog


此外,調用將自動將 設置為 由于事件掛鉤(請參閱 https://laravel.com/docs/6.x/eloquent#events)。Dog::create($properties)type'dog'saving


請注意,調用沒有默認值,因此在這里您需要手動設置(這是預期的)。Animal::create($properties)type


查看完整回答
反對 回復 2022-09-17
  • 5 回答
  • 0 關注
  • 162 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號