symfony/doctrine中接口的继承关系

juzqafwq  于 2022-11-16  发布在  其他
关注(0)|答案(1)|浏览(146)

我有这样的实体:
请求. php(父项)

/**
 * @ORM\Entity(repositoryClass=RequestRepository::class)
 * @ORM\InheritanceType("JOINED")
 * @ORM\DiscriminatorColumn(name="type", type="string")
 * @ORM\DiscriminatorMap({
 *      "requestA" = "RequestA",
 *      "requestB" = "RequestB"
 * })
 */
abstract class Request
{
    /*...*/
}

请求A.php(子项A)

/**
 * @ORM\Entity(repositoryClass=DemenagementRepository::class)
 */
class RequestA extends Request implements AddressEntityInterface
{
    /**
     * @ORM\OneToOne(targetEntity=Address::class, inversedBy="request")
     * @ORM\JoinColumn(nullable=false)
     */
    private $address;

    // +others...
}

请求B.php(子级B)

/**
 * @ORM\Entity(repositoryClass=DemenagementRepository::class)
 */
class RequestB extends Request implements AddressEntityInterface
{
    /**
     * @ORM\OneToOne(targetEntity=Address::class, inversedBy="request")
     * @ORM\JoinColumn(nullable=false)
     */
    private $address;

    // +others...
}

AddressEntityInterface.php

interface AddressEntityInterface
{
    public function getAddress(): ?Address;
    public function setAddress(Address $address): self;
}

Address.php

/**
 * @ORM\Entity(repositoryClass=AddressRepository::class)
 */
class Address
{
    /**
     * @ORM\OneToOne(targetEntity=??????, mappedBy="address")
     */
    private $request;

    public function getRequest() { /* ... */ }
}

我想使用getRequest()还原关系,如何动态创建targetEntity?
谢谢

shstlldc

shstlldc1#

我相信您需要多态关系或者他们喜欢从docrine中调用它们'继承Map'
我给你留下一些网站查看,你可以找到你需要的一切
Official Documentation (Doctrine)
Stackoverflow Practical example
Youtube tutorial
Stackoverflow, discussion〈-我个人建议你阅读所有这些内容,这样你就可以具体了解 您所需要的
我希望我的小路线能帮到你。

相关问题