php - 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
}
},
但看起来像一些定制的黑客,对这个建议有什么看法吗?
解决方案
推荐阅读
- jenkins-declarative-pipeline - Jenkins 声明式管道和自定义 maven settings.xml
- python - 重新创建文件时不会崩溃的读取循环
- c# - 如何在 C# 中用 swagger 为一个请求对象定义多个请求示例?
- .net - 在管道中使用 Azure 密钥保管库中的证书
- anaconda - 如何更改 conda 在括号中显示的名称?
- c# - 无法为 Outlook 插件加载 VSTO 部署文件
- machine-learning - 如何对不同的输入数据使用机器学习算法?
- javascript - 使用与 providedIn 相同的服务:root 作为单例和组件的提供者与服务
- reflection - F# 反射:将数组作为参数传递给 MethodInfo.Invoke
- json - Angular 8 ngx-translate 从 URL 加载翻译 JSON 文件