Sonata Admin - CollectionType and Dynamic add Abstract class

时间:2018-06-19 11:13:44

标签: symfony abstract-class sonata-admin sonata

I work on a Symfony 3.4 project and I have a issue with a specific Sonata Admin case, here is my situation :

I have a Lesson entity who have elements :

/**
 * @ORM\OneToMany(targetEntity="App\Entity\Lessons\ElementLesson", mappedBy="lesson", cascade={"persist"})
 */
protected $elements;

Elements is abstract class with Inheritance

/**
 * @ORM\Entity
 * @ORM\InheritanceType("JOINED")
 * @ORM\DiscriminatorColumn(name="discr", type="string")
 * @ORM\DiscriminatorMap({
 *     "element_lesson" = "ElementLesson",
 *     "popup_element_lesson" = "PopupElementLesson",
 *     "advice_element_lesson" = "AdviceElementLesson",
 *     "accordion_element_lesson" = "AccordionElementLesson",
 *     "title_element_lesson" = "TitleElementLesson",
 *     "text_element_lesson" = "TextElementLesson",
 *     "separator_element_lesson" = "SeparatorElementLesson"
 * })
 */
abstract class ElementLesson
{
    /**
     * @ORM\Id
     * @ORM\Column(type="integer")
     * @ORM\GeneratedValue
     */
    protected $id;

    /**
     * @ORM\ManyToOne(targetEntity="App\Entity\Lessons\Lesson", inversedBy="elements")
     * @ORM\JoinColumn(nullable=false, onDelete="CASCADE")
     */
    protected $lesson;

So I want in my Sonata admin, create a Lesson and add some Element to her, for that I process like this :

I define this 2 services :

app.admin.lesson.all:
    class: App\Admin\Lessons\LessonAdmin
    arguments: [~, App\Entity\Lessons\Lesson, ~]
    tags:
        - { name: sonata.admin, manager_type: orm, label: Cours }
    public: true

app.admin.lesson.element:
    class: App\Admin\Lessons\ElementLessonAdmin
    arguments: [~, App\Entity\Lessons\ElementLesson, ~]
    tags:
        - { name: sonata.admin, manager_type: orm, group: lesson, label: Element  }
    calls:
        - [setSubclasses, [{'TitleElementLesson': App\Entity\Lessons\TitleElementLesson, 'TextElementLesson': App\Entity\Lessons\TextElementLesson}]]
    public: true

And create a ElementLessonAdmin :

class ElementLessonAdmin extends AbstractAdmin
{

protected function configureFormFields(FormMapper $form)
{
    $subject = $this->getSubject();

    if ($subject instanceof TitleElementLesson) {
        $form->add('title');
    }

    if ($subject instanceof TextElementLesson) {
        $form->add('text');
    }
}

And LessonAdmin :

class LessonAdmin extends AbstractAdmin
{

protected function configureFormFields(FormMapper $formMapper)
{
    $formMapper

        ->with('Contenu', ['class' => 'col-md-9'])

        ->add('elements', CollectionType::class, array(
            'btn_add' => 'New Element',
            'type_options' => array(
                // Prevents the "Delete" option from being displayed
                'delete' => false,
                'delete_options' => array(
                    // You may otherwise choose to put the field but hide it
                    'type'         => 'hidden',
                    // In that case, you need to fill in the options as well
                    'type_options' => array(
                        'mapped'   => false,
                        'required' => false,
                    )
                )
            )
        ), array(
            'edit' => 'standard',
            'inline' => 'natural',
            'sortable' => 'position'
        ))
        ->end()
        ;
    }

    public function toString($lesson)
    {
        return $lesson instanceof Lesson
            ? $lesson->getTitle()
            : 'Cours'; // shown in the breadcrumb on the create view
    }

    public function prePersist($lesson)
    {
        foreach ($lesson->getElements() as $element) {
            $element->setLesson($lesson);
        }
    }

    public function preUpdate($lesson)
    {
        foreach ($lesson->getElements() as $element) {
            $element->setLesson($lesson);
        }
    }
}

The result looks like what I want :

Result 1

Result 2

Result 3

But I have no connection between the 2 Entities :

Error 1

Error 2

I'm not sure than what I want it's possible like that or if I need to do that with a other method.

Thanks

0 个答案:

没有答案