Symfony 3 – Collection of forms only sends data to one table

I am running Symfony 3.4 with 2 relevant entities – Entry and Track with a OneToMany relationship. I have been through this arcticle – I am able to get an ArrayCollection to be added to the Entry table, but nothing is added to the Track table.

I have tried to exclude as much irrelevant code as possible for ease of reading.

Entry

<?php

namespace BloggerBlogBundleEntity;

use DoctrineCommonCollectionsArrayCollection;
use DoctrineORMMapping as ORM;

/**
 * @ORMEntity
 * @ORMTable(name="`workshop_entry`")
 */
class Entry
{
private $id;

/**
 * @ORMOneToMany(targetEntity="BloggerBlogBundleEntityTrack", cascade={"persist"})
 */
private $tracklist;

public function __construct()
{
    $this->tracklist = new ArrayCollection();
}

public function getId()
{
    return $this->id;
}

public function getTracklist()
{
    return $this->tracklist;
}

public function addTrack(Track $track) {
    $this->tracklist->add($track);
}

public function removeTrack(Track $track) {
    $this->tracklist->removeElement($track);
}
}

Track

<?php

namespace BloggerBlogBundleEntity;

use DoctrineCommonCollectionsArrayCollection;
use DoctrineORMMapping as ORM;

/**
 * Track
 */
class Track
{
/**
 * @var int
 * @ORMId
 * @ORMGeneratedValue(strategy="AUTO")
 * @ORMColumn(type="integer")
 */
protected $id;

/**
 * @var string
 * @ORMColumn(type="string")
 */
protected $name;

EntryType

    class EntryType extends AbstractType
{
    /**
     * {@inheritdoc}
     */
    public function buildForm(FormBuilderInterface $builder, array $options)
    {
        $builder->add('tracklist', CollectionType::class, [
                'entry_type' => TrackType::class,
                'entry_options' => ['label' => false],
                'allow_add' => true,
                'by_reference' => false,
                'allow_delete' => true,
                'mapped' => false,
            ])
            ->add('submit', SubmitType::class);

EntryController

public function createAction(Request $request)
    {
        $reviewEntry = new Entry();

        $form = $this->createForm(EntryType::class, $reviewEntry, ['action' => $request->getUri()]);
        $form->handleRequest($request);

        //Validates the form
        if($form->isValid())
            //
        $em->persist($reviewEntry);

        $em->flush();
    }

    return $this->render('BloggerBlogBundle:Blog:create.html.twig', ['form'=>$form->createView()]);
  }

Source: Symfony Questions

Was this helpful?

0 / 0

Leave a Reply 0

Your email address will not be published. Required fields are marked *