CollectionType and class inheritance

I am struggling with a Symfony project, and since I am not that knowledgeable about this structure, I cannot understand if I am wrong with my system or if Symfony is not doing its job or I just need to find the right method.

Here he is:

I have a Row entity that needs to contain 1 to n content that has different content such as "title", "text", "image", etc .... Since each content has different characteristics, I expanded each content type from the abstract RowContent class by inheriting one table. Here is the edited version of the objects: Row class

class Row
{
    //.....

    /**
     * @var ArrayCollection $rowContents
     *
     * @ORM\OneToMany(targetEntity="RowContent", mappedBy="row", cascade={"persist", "remove", "merge"})
     */
    private $rowContents;

    //...
}

      

RowContent class:

/**
 * RowContent
 *
 * @ORM\Table(name="row_content")
 * @ORM\InheritanceType("SINGLE_TABLE")
 * @ORM\DiscriminatorColumn(name="discr", type="string")
 * @ORM\DiscriminatorMap({
 *     "title" = "Kinkinweb\BaseBundle\Entity\Content\Title",
 *     "text" = "Kinkinweb\BaseBundle\Entity\Content\Text",
 *     "image" = "Kinkinweb\BaseBundle\Entity\Content\Image",
 * })
 * @ORM\Entity(repositoryClass="Kinkinweb\BaseBundle\Repository\RowContentRepository")
 */

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

    //...
}

      

and for example the text class:

/**
 * Text
 *
 * @ORM\Table(name="content_text")
 * @ORM\Entity(repositoryClass="Kinkinweb\BaseBundle\Repository\Content\TextRepository")
 */
class Text extends RowContent
{
    /**
     * @var string
     *
     * @ORM\Column(name="text", type="string", length=255)
     */
    private $text;

    //...
}

      

So far so good, but I can't handle form submissions with all this ... In order to handle forms with these objects, I still have the following FormType:

class RowType extends AbstractType
{
    public function buildForm(FormBuilderInterface $builder, array $options)
    {
        $builder
            //...
            ->add('rowContents', CollectionType::class, array(
                'entry_type'   => RowContentType::class,
                'allow_add'    => true,
                'allow_delete' => true,
                'label' => 'Contenu Flexible',
                'by_reference' => false,
                'block_name' => 'rows',
            ))
        ;
    }

    public function configureOptions(OptionsResolver $resolver)
    {
        $resolver->setDefaults(array(
            'data_class' => 'Kinkinweb\BaseBundle\Entity\Row',
        ));
    }
}

      

I covered the buildForm part as follows:

    public function buildForm(FormBuilderInterface $builder, array $options)
        {
            $builder->addEventListener(FormEvents::PRE_SET_DATA, function($e) {
                if (null === $e->getData()) { return; }
                $form = $e->getForm();
                if ($e->getData() instanceof Text){
                    $form->add('text',TextType::class);
                }
            });
        }
public function configureOptions(OptionsResolver $resolver)
    {
        $resolver->setDefaults(array(
            'data_class' => 'Kinkinweb\BaseBundle\Entity\RowContent';
        ));
    }

      

but when I add content to the form using jQuery and submit the form, the framework cannot handle the presented RowContent object (seems logical to me since RowContent is abstract).

So, before I boldly pulling the hairs one by one, I wonder if someone has already encountered a situation like this or has an idea of ​​how to make their own shape.

Thank!

+3


source to share





All Articles