简体   繁体   中英

SonataAdminBundle inheritance issue

I'm facing an issue about the inheritance with SonataAdminBundle

I have 2 entities that linked each other : AbstractPerson & Vehicle and 2 entities that inherit of 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:

<?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 :

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. From this form I can create a person but Sonata want to instanciate an AbstractPerson (an abstract class)

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

tl;dr:

1 admin for Person
1 admin for Society
1 admin (optional) for Person + Society

Long story:

The only flaw in your logic is the "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

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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