A
A
Anton Dyshkant2017-07-01 18:29:15
symfony
Anton Dyshkant, 2017-07-01 18:29:15

How to administer Doctrine ORM Class Table Inheritance via SonataAdminBundle (Symfony)?

Good afternoon!
I have a Doctrine entity Foo that is related One-To-One to an AbstractBar entity:

/**
 * @ORM\Table(name="foo")
 * @ORM\Entity
 */
class Foo
{
    /**
     * @var int
     *
     * @ORM\Column(name="id", type="integer")
     * @ORM\Id
     * @ORM\GeneratedValue(strategy="AUTO")
     */
    private $id;

    /**
     * @var AbstractBar
     *
     * @ORM\OneToOne(targetEntity="AbstractBar", cascade={"persist"})
     * @ORM\JoinColumn(name="bar_id", referencedColumnName="id")
     */
    private $bar;
}

An AbstractBar entity is an abstract entity that has two concrete entities associated with it using the Class Table Inheritance model .
Thus, I have this:
/**
 * @ORM\Table(name="bar")
 * @ORM\Entity
 * @ORM\InheritanceType("JOINED")
 * @ORM\DiscriminatorColumn(name="bar_type", type="string")
 * @ORM\DiscriminatorMap({"one" = "BarOne", "two" = "BarTwo"})
 */
abstract class AbstractBar
{
    /**
     * @var int
     *
     * @ORM\Column(name="id", type="integer")
     * @ORM\Id
     * @ORM\GeneratedValue(strategy="AUTO")
     */
    private $id;
}

/**
 * @ORM\Table(name="bar_one")
 * @ORM\Entity
 */
class BarOne extends AbstractBar
{
    // свойства класса BarOne
}

/**
 * @ORM\Table(name="bar_two")
 * @ORM\Entity
 */
class BarTwo extends AbstractBar
{
    // свойства класса BarTwo
}

At the same time, the admin (SonataAdminBundle) of the Foo entity is configured as follows:
class FooAdmin extends AbstractAdmin
{
    protected function configureFormFields(FormMapper $formMapper)
    {
        $formMapper
            ->add('bar', AdminType::class, array(), array(
                'admin_code' => 'key.of.bar_one.admin.service'
            ))
        ;
    }
}

At the same time, the admin class service itself looks like this:
key.of.bar_one.admin.service:
        class: MyBundle\Admin\BarAdmin
        arguments: [~, MyBundle\Entity\BarOne, ~]
        tags:
            - name: sonata.admin
              manager_type: orm
              show_in_dashboard: false

The above code allows me to edit and create BarOne entities by editing the Foo entity.
Question: how can I make it so that I can switch between BarOne and BarTwo in the admin panel? Those. so that you can implement the multiple selection provided by Doctrine ORM (Class Table Inheritance).

Answer the question

In order to leave comments, you need to log in

1 answer(s)
D
Denis, 2017-07-02
@prototype_denis

Add a subclass to the base admin service.
calls: ["addSubClass",[...]]

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question