我正在尝试使用 YAML 在两个表之间创建一对一关系。我不确定他们应该如何沟通。
class Games
{
private $id;
private $title;
private $description;
private $img_link;
private $website_link;
private $pegi;
private $release_date;
private $requirements;
}
AppEntityGames:
type: entity
repositoryClass: AppRepositoryGamesRepository
table: games
id:
id:
type: integer
generator:
strategy: AUTO
fields:
title:
type: string
length: 255
nullable: true
description:
type: text
nullable: true
img_link:
type: text
nullable: true
website_link:
type: text
nullable: true
pegi:
type: integer
nullable: true
release_date:
type: date
nullable: true
OneToOne:
requirements:
targetEntity: AppEntityRequirements
joinColumn:
name: requirements_fk
referencedColumnName: id
class Requirements
{
private $id;
private $os_min;
private $cpu_min;
private $ram_min;
private $hdd_min;
private $gpu_min;
private $directx_min;
private $os_req;
private $cpu_req;
private $ram_req;
private $hdd_req;
private $gpu_req;
private $directx_req;
}
AppEntityRequirements:
type: entity
repositoryClass: AppRepositoryRequirementsRepository
table: requirements
id:
id:
type: integer
generator:
strategy: AUTO
fields:
os_min:
type: string
length: 255
nullable: true
cpu_min:
type: string
length: 255
nullable: true
ram_min:
type: integer
nullable: true
hdd_min:
type: integer
nullable: true
gpu_min:
type: string
lenght: 255
nullable: true
directx_min:
type: integer
nullable: true
os_req:
type: string
length: 255
nullable: true
cpu_req:
type: string
length: 255
nullable: true
ram_req:
type: integer
nullable: true
hdd_req:
type: integer
nullable: true
gpu_req:
type: string
lenght: 255
nullable: true
directx_req:
type: integer
nullable: true
我尝试像这样查询它,但它总是返回 null,即使它应该返回一个条目。
$temp = $this->getDoctrine()->getRepository(Games::class)->find($id);
$requirements = $temp->getRequirements();
我没有收到任何错误,所以,尝试调试它真的很难。根据文档,它看起来还可以。所以,不确定问题出在哪里。
如果class Games
是你的实体,你应该在那里getRequirements()
方法。如果实体为空,请尝试基于架构生成实体。
https://symfony.com/doc/current/doctrine/reverse_engineering.html
关键问题是 YAML 中的一对一!= 一对一。