简体   繁体   English

SonataAdminBundle 继承问题

[英]SonataAdminBundle inheritance issue

I'm facing an issue about the inheritance with SonataAdminBundle我正面临有关SonataAdminBundle继承的问题

I have 2 entities that linked each other : AbstractPerson & Vehicle and 2 entities that inherit of AbstractPerson : Person & Society我有 2 个相互关联的实体: AbstractPerson & Vehicle和 2 个继承AbstractPerson 的实体: Person & Society

<?php

namespace Foo\Bundle\BarBundle\Entity;

use Doctrine\ORM\Mapping as ORM;
use Doctrine\Common\Collections\ArrayCollection;

/**
 * AbstractPerson
 *
 * @ORM\Entity
 * @ORM\InheritanceType("SINGLE_TABLE")
 * @ORM\DiscriminatorColumn(name="discr", type="string")
 * @ORM\DiscriminatorMap({"person" = "Person", "society" = "Society"})
 */
abstract class AbstractPerson
{
    /**
     * @var integer
     *
     * @ORM\Column(name="id", type="integer")
     * @ORM\Id
     * @ORM\GeneratedValue(strategy="AUTO")
     */
    protected $id;

    /**
     * @var string
     *
     * @ORM\Column(name="name", type="string", length=255)
     */
    protected $name;

    /**
     * @ORM\OneToMany(targetEntity="Vehicle", mappedBy="owner")
     * @ORM\JoinColumn(nullable=false)
     */
    protected $vehicles;
}

/**
 * Person
 *
 * @ORM\Entity
 */
class Person extends AbstractPerson
{
}

/**
 * Society
 *
 * @ORM\Entity
 */
class Society extends AbstractContact
{
}

And there is my Vehicle entity:还有我的Vehicle实体:

<?php

namespace Foo\Bundle\BarBundle\Entity;

use Doctrine\ORM\Mapping as ORM;
use Doctrine\Common\Collections\ArrayCollection;

/**
 * Vehicle
 *
 * @ORM\Entity
 * @ORM\Table(name="vehicle")
 */
class Vehicle
{
    /**
     * @var integer
     *
     * @ORM\Column(name="id", type="integer")
     * @ORM\Id
     * @ORM\GeneratedValue(strategy="AUTO")
     */
    protected $id;

        /**
         * @ORM\ManyToOne(targetEntity="AbstractPerson" ,inversedBy="vehicles")
         * @ORM\JoinColumn(name="person_id", referencedColumnName="id")
         */
        protected $owner;
}

SonataAdminBundle allows us to handle inheritance between entities with setSubClasses method sets in the admin.yml : SonataAdminBundle 允许我们使用admin.yml 中的 setSubClasses方法集处理实体之间的继承:

services:
    sonata.admin.abstract_contact:
        class: Foo\Bundle\BarBundle\Admin\AbstractPersonAdmin
        tags:
            - { name: sonata.admin, manager_type: orm, group: "Vehicle Manager", label: "AbstractPerson", label_catalogue: "messages" }
    arguments:
        - ~
        - Foo\Bundle\BarBundle\Entity\AbstractPerson
        - ~
    calls:
        - [setSubClasses, [{society: Foo\Bundle\BarBundle\Entity\Society, person: Foo\Bundle\BarBundle\Entity\Person}]] # Here the method that handles inheritance

Now I can create Person & Society but the problem is in the Vehicle form.现在我可以创建Person & Society,但问题出在Vehicle形式中。 From this form I can create a person but Sonata want to instanciate an AbstractPerson (an abstract class)从这种形式我可以创建一个人,但奏鸣曲想要实例化一个 AbstractPerson (一个抽象类)

Cannot initialize abstract class: Foo\Bundle\BarBundle\Entity\AbstractPerson

If anyone can help me to instanciate a person or a society instead of an abstract person I will be delighted!如果有人能帮我实例化一个人或一个社会而不是一个抽象的人,我会很高兴!

Sorry for my bad english.对不起,我的英语不好。

Thanks!谢谢!

The problem is solved.问题已经解决了。 See #2917#2917

tl;dr: tl;博士:

1 admin for Person 1 个人管理员
1 admin for Society 1个社会管理员
1 admin (optional) for Person + Society个人 + 社会的 1 个管理员(可选)

Long story:很长的故事:

The only flaw in your logic is the "sonata.admin.abstract_contact".您逻辑中唯一的缺陷是“sonata.admin.abstract_contact”。 From an architecture point of view, Person and Society are two completely different entities, and the Single Table Inheritance you're using here is only meant to help your implementation look cleaner从架构的角度来看,Person 和 Society 是两个完全不同的实体,您在这里使用的 Single Table Inheritance 只是为了帮助您的实现看起来更清晰

In order to fix this, you need TWO separate admins, one for each entity.为了解决这个问题,您需要两个单独的管理员,每个实体一个。
And if you want a joined admin in which to manage BOTH entities simultaneously, you need to create a THIRD admin service which has two fields, both of "sonata_admin_type", in which you include the two admins.如果你想要一个加入的管理员同时管理两个实体,你需要创建一个第三个管理员服务,它有两个字段,两个字段都是“sonata_admin_type”,其中包含两个管理员。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM