首页 > 解决方案 > laravel eloquent 用多表序列化自定义关系

问题描述

我用

"laravel/framework": "^8.54",

并且在我的数据库结构中,我有一个模型,它通过 id 聚合不同的属性,并设置鉴别器列来标识这到底是什么表,就像这样

sql

create table configure_property_estates
(
    id                    bigint unsigned auto_increment
        primary key,
    estates_id            bigint unsigned not null,
    configure_property_id bigint unsigned not null,
    discriminator         varchar(100)    not null,
    created_at            timestamp       null,
    updated_at            timestamp       null,
    constraint configure_property_estates_estates_id_foreign
        foreign key (estates_id) references estates (id)
            on delete cascade
);

create index configure_property_estates_configure_property_id_index
    on configure_property_estates (configure_property_id);

json响应

 "configure_property_estates": [
      {
        "id": 28,
        "estates_id": 14,
        "configure_property_id": 1,
        "discriminator": "ConfigureEstatePropertyKitchen",
        "created_at": "2021-10-10T15:17:46.000000Z",
        "updated_at": "2021-10-10T15:17:46.000000Z"
      },
      {
        "id": 29,
        "estates_id": 14,
        "configure_property_id": 1,
        "discriminator": "ConfigureEstatePropertyRequirements",
        "created_at": "2021-10-10T15:17:47.000000Z",
        "updated_at": "2021-10-10T15:17:47.000000Z"
      },
      {
        "id": 30,
        "estates_id": 14,
        "configure_property_id": 2,
        "discriminator": "ConfigureEstatePropertyType",
        "created_at": "2021-10-10T15:17:47.000000Z",
        "updated_at": "2021-10-10T15:17:47.000000Z"
      }
    ],

这个模型,没有 FK 唯一索引:

class ConfigurePropertyEstates extends Model
{
    /**
     * The attributes that are mass assignable.
     *
     * @var string[]
     */
    protected $fillable = [
        'estates_id',
        'configure_property_id',
        'discriminator'
    ];

    public function getTable()
    {
        return 'configure_property_estates';
    }

    /**
     * Get all of the estates that are assigned this conf.
     */
    public function estates()
    {
        return $this->belongsTo(Estates::class, 'estates_id');
    }
}

os,我的问题是,如果我想通过配置属性模型来扩展这个模型的 json 解释,需要做什么,像这样

  {
    "id": 28,
    "estates_id": 14,
    "configure_property_id": 1,
    "configure_property": {
        "id": 1,
        "some_property_column": "test"
    }
    "discriminator": "ConfigureEstatePropertyKitchen",
    "created_at": "2021-10-10T15:17:46.000000Z",
    "updated_at": "2021-10-10T15:17:46.000000Z"
  },

是否存在某种方式,某种类型的关系,或者可能是解决这种情况的某种自定义方法?

更新

我将此代码添加到我的模型ConfigurePropertyEstates中,这就像我预期的那样工作

public function toArray()
{
    $toArray = parent::toArray();
    $nameSpaceDiscriminator = EstatePropertiesService::fetchNameSpaceDiscriminator($toArray['discriminator']);
    if (!class_exists($nameSpaceDiscriminator)) {
        throw new \Exception('class ' . $nameSpaceDiscriminator . ' does not exist');
    }
    $toArray['configure_property'] = $nameSpaceDiscriminator::whereId($toArray['configure_property_id'])->first();
    return $toArray;
}

回复

  {
    "id": 28,
    "estates_id": 14,
    "configure_property_id": 1,
    "discriminator": "ConfigureEstatePropertyKitchen",
    "created_at": "2021-10-10T15:17:46.000000Z",
    "updated_at": "2021-10-10T15:17:46.000000Z",
    "configure_property": {
      "id": 1,
      "property_name": "test",
      "property_value": "tets",
      "available": 1,
      "created_at": null,
      "updated_at": null
    }
  },

但看起来像一些定制的黑客,对这个建议有什么看法吗?

标签: phplaravel

解决方案


推荐阅读