gpt4 book ai didi

symfony - 如何与 MappedSuperclass 作品建立联系?

转载 作者:行者123 更新时间:2023-12-05 01:12:25 25 4
gpt4 key购买 nike

我在 Symfony 5 中与 MappedSuperclass 的多对一关系存在问题。我有实体 Employee、Employer,它扩展了 MappedSuperclass 抽象类 Person。我想创建与人(员工和雇主)相关的报告实体,如下所示:

    /**
* @ORM\ManyToOne(targetEntity=Person::class)
*/
private $person;

但是当我尝试推送迁移时,我收到以下错误消息:

Column name id referenced for relation from App\Entity\Raport towards App\Entity\Person does not exist.

但我在这些类中有 id 属性:

    /**
* @ORM\Id()
* @ORM\GeneratedValue()
* @ORM\Column(type="integer")
*/
protected $id;

我在 Symfony 页面示例中找到了创建界面来执行此操作,但它也不适用于我。也许有人以前遇到过这个问题并且知道如何解决它。非常感谢您的回复。

编辑我的 Person 类:

**
* Abstract base class to be extended by my entity classes with same fields
*
* @MappedSuperclass
*/
abstract class Person
{
/**
* @ORM\Id()
* @ORM\GeneratedValue()
* @ORM\Column(type="integer")
*/
protected $id; //This property exist in Employee and Employer too (like doctrine documentation said)

现在,当我将其从父类(super class)更改为“JOINED”继承时,当我尝试立即创建 Employee 或 Employer 时,我收到以下错误:

An exception occurred while executing 'INSERT INTO person (discr) VALUES  
(?)' with params ["Employee"]:

SQLSTATE[42000]: Syntax error or access violation: 1064 You have an error i
n your SQL syntax; check the manual that corresponds to your MySQL server v
ersion for the right syntax to use near 'person (discr) VALUES ('Employee')'
at line 1

没有任何其他方法可以仅在一个属性中与实现一个接口(interface)或扩展类的少数实体建立关系?有时我讨厌 Doctrine ......而我的人实体可能有助于配置错误:

* @ORM\Entity(repositoryClass=PersonRepository::class)
* @ORM\InheritanceType("JOINED")
* @ORM\DiscriminatorColumn(name="discr", type="string")
* @ORM\DiscriminatorMap({"person" = "Person", "employee" = "Employee", "employer" = "Employer"})
*/
class Person
{
/**
* @ORM\Id()
* @ORM\GeneratedValue()
* @ORM\Column(type="integer")
*/
protected $id;

public function getId(): ?int
{
return $this->id;
}

}

最佳答案

您不能将关系目标设为 MappedSuperClass,因为它本身不是实体。它只能自己定义一个拥有的一对一关系(或非常有限的多对多):

A mapped superclass cannot be an entity, it is not query-able and persistent relationships defined by a mapped superclass must be unidirectional (with an owning side only). This means that One-To-Many associations are not possible on a mapped superclass at all. Furthermore Many-To-Many associations are only possible if the mapped superclass is only used in exactly one entity at the moment. For further support of inheritance, the single or joined table inheritance features have to be used.

source: https://www.doctrine-project.org/projects/doctrine-orm/en/2.7/reference/inheritance-mapping.html#mapped-superclasses

可能想要的是单表继承或类表继承(参见上面的链接,它也显示了这些)。虽然它会变得很烦人。您已收到警告。

关于symfony - 如何与 MappedSuperclass 作品建立联系?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62006565/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com