如何地图YAML OneToOne关系

时间:2019-02-01 12:28:18

标签: symfony doctrine-orm

我正在尝试使用YAML在两个表之间创建OneToOne关系。我不确定他们应该如何沟通。

class Games
{

    private $id;


    private $title;


    private $description;


    private $img_link;


    private $website_link;


    private $pegi;


    private $release_date;

    private $requirements;
}

App\Entity\Games:
  type: entity
  repositoryClass: App\Repository\GamesRepository
  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: App\Entity\Requirements
      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;
}

App\Entity\Requirements:
  type: entity
  repositoryClass: App\Repository\RequirementsRepository
  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();

我没有收到任何错误,因此很难对其进行调试。根据文档,看起来还可以。因此,不确定问题出在哪里。

2 个答案:

答案 0 :(得分:0)

如果class Games是您的实体,则应该在其中使用方法getRequirements()。 如果您的实体为空,请尝试根据架构生成实体。

https://symfony.com/doc/current/doctrine/reverse_engineering.html

答案 1 :(得分:0)

关键问题是YAML中的OneToOne!= oneToOne。