【问题标题】:Laravel 5 - query using a model, get results with a different modelLaravel 5 - 使用模型查询,使用不同模型获取结果
【发布时间】:2016-02-29 06:09:41
【问题描述】:

问题

是否可以通过与最初进行查询的模型不同的模型返回模型查询结果?

例如,如果我们有两个模型,ModelAModelB,我们会获取一些数据库结果:

$modelA = new ModelA;

$results = $modelA->all();

dd($results);

它可以以某种方式返回 ModelB 对象,而不是 ModelA 对象的集合吗?想要的输出,例如:

Collection {#325 ▼
    #modelA_Table: Array:4 [▼
        0 => ModelB { #297 ▶}
        1 => ModelB { #306 ▶}
        2 => ModelB { #311 ▶}
        3 => ModelB { #318 ▶}
    ]
}

上下文

关系是一个分类层次结构,其中ModelBModelA 的子分类:

class ModelA extends Model {
    protected $table = 'ModelA_Table';

    protected $fillable = [];

    private $discriminator = 'a_type';

    public function __construct(array $attributes = array()){
        $this->initialize();
        parent::__construct($attributes);
    }

    private function initialize() {
        $this->fillable = array_merge($this->fillable, $this->fillables());
    }

    private function fillables() {
        return [
            'a_name',
            'a_type'
            'a_price'
        ];
    }

}

class ModelB extends ModelA {

    protected $fillable = [];

    public function __construct(array $attributes = array()){
        $this->initialize();
        parent::__construct($attributes);
    }

    private function initialize() {
        $this->fillable = array_merge($this->fillable, $this->fillables());
    }

    private function fillables() {
        return [
            'b_width',
            'b_height'
        ];

    }
}

两种模型都是由单表继承 (ModelA_Table) 持久化的单个实体的不同分类单元(分类级别)。

类比 -- 一般 : 具体 :: ModelA : ModelB :: Vehicle : Truck

回到代码,当 ModelB 被实例化时,它会通过构造函数中的 initialize() 将自己的可填充对象附加到其继承的父可填充对象中。其中ModelB可以继承ModelA的fillable,反之则不然; ModelA 不能继承 ModelB 的填充物。我可以要求 Truck->find(1) 并获取 Truck 和 Vehicle 属性,但 Vehicle->find(1) 只会给我 Vehicle 属性,因为 Vehicle (一般分类法)不能从其子级(从特定分类法)继承)。

这让我处于现在的位置。


基本上,如果 ModelA 是 Vehicle,ModelB 是 Car,我需要这样做:

1) 车辆模型按 id 获取行

2) 车辆模型查看字段“a_type”

3) 'a_type' 可以是 'Car'、'Motorcycle' 或 'Truck' 等

4) 返回的水合对象将属于“a_type”类


使用 ModelA 查询,使用 ModelB 获取结果。

【问题讨论】:

    标签: php laravel-5


    【解决方案1】:

    我使用 Jeff Madsen's solution 修改 Eloquent 模型的 newFromBuilder 方法解决了这个问题。

    模型.php:

    public function newFromBuilder($attributes = array())
    {
        $instance = $this->newInstance(array(), true);
    
        $instance->setRawAttributes((array) $attributes, true);
    
        return $instance;
    }   
    

    在扩展类中覆盖:

    public function newFromBuilder($attributes = array())
    {
        $class = $this->{$this->discriminator} . ucwords($attributes->status);
        $instance = new $class;
        $instance->exists = true;
        $instance->setRawAttributes((array) $attributes, true);
        return $instance;
    }  
    

    新的类将是$class,这是检索到的鉴别器字段的值,由属性discriminator指定

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-10-29
      • 2017-09-13
      • 2018-01-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多