Search code examples
phpsymfonyyamlsonata-admin

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!


Solution

  • The problem is now solved on their end.

    See #2917