Symfony2 OneToMany Embed Form does not save object on

I have persistent problems with OneToMany inline forms in Symfony2.

This time the objects are preserved, but not a reference to the parent class.

My table looks like this:

| id | position | content | about_id |
| 29 |        1 |  test 1 |     NULL |

      

I can't figure out why this about_id is still NULL.

My relationship:

Essence:

class About {
    /*
     ....
    */

    /**
     * @ORM\OneToMany(targetEntity="AboutContent", mappedBy="about", cascade={"persist", "remove"})
     */
    private $content;

    /**
     * Add content
     *
     * @param AboutContent $content
     * @return About
     */
    public function addContent(AboutContent $content)
    {
        $this->content[] = $content;

        return $this;
    }

    /**
     * Remove content
     *
     * @param AboutContent $content
     */
    public function removeContent(AboutContent $content)
    {
        $this->content->removeElement($content);
    }

    /**
     * Get content
     *
     * @return \Doctrine\Common\Collections\Collection 
     */
    public function getContent()
    {
        return $this->content;
    }
}

      

My About Content:

class AboutContent {
    /**
     * @ORM\ManyToOne(targetEntity="About", inversedBy="content")
     */
    private $about;

    /**
     * Set about
     *
     * @param About $about
     * @return AboutContent
     */
    public function setAbout(About $about = null)
    {
        $this->about = $about;

        return $this;
    }

    /**
     * Get about
     *
     * @return About 
     */
    public function getAbout()
    {
        return $this->about;
    }
}

      

My controller was auto-generated by my crud:

/**
 * Creates a new About entity.
 *
 */
public function createAction(Request $request)
{
    $entity = new About();
    $form = $this->createCreateForm($entity);
    $form->handleRequest($request);

    if ($form->isValid()) {
        $em = $this->getDoctrine()->getManager();

        $em->persist($entity);
        $em->flush();

        return $this->redirect($this->generateUrl('about_show', array('id' => $entity->getId())));
    }

    return $this->render('AdminBundle:About:new.html.twig', array(
        'entity' => $entity,
        'form'   => $form->createView(),
    ));
}

      

My form constructor:

$builder
  ->add('lang', 'choice', array(
                    'choices'   => $this->langs,
                    'preferred_choices' => array('en'),
        ))            ->add('media')
    ->add('content', 'collection', array(
        'type'         => new AboutContentType(),
        'allow_add'    => true,
        'allow_delete' => true,
        'cascade_validation' => true
      ), array('label'=>'Texts'))       
 ;

 }

      

Thanks a lot if you find a solution.

+3


source to share


2 answers


Hello everyone after home searching, I finally found the answer. The problem didn't come from my entities or the ships of my relationship. It came from my form.

In my type, I just didn't add the tag: 'by_reference' => false

He decided everything.



To help you if you have the same problem, below is my builder:

$builder->add(
    'aboutContents',
    'collection',
    [
        'type' => new AboutContentType(),
        'allow_add' => true,
        'allow_delete' => true,
        'cascade_validation' => true,
        'by_reference' => false,
    ],
    [
        'label' => 'Texts'
    ]
);

      

+6


source


Your new objects AboutContent

are unaware of the created object About

because the form:
- runs a method addContent

on the object About

,
- doesn't work setAbout

on AboutContent

.

You need to update the association field on the owner ( AboutContent

) side to keep the association.

Look at the definition of Association Update .



In your code:
- Own side is an AboutContent

entity (because it is related to About

)
- Back side of About

entity

So in addContent

you must install About

to add AboutContent

.

class About {
    //...

    /**
     * Add content
     *
     * @param AboutContent $content
     * @return About
     */
    public function addContent(AboutContent $content)
    {
        $this->content[] = $content;

        $content->setAbout($this); // set the relation on the owning-side

        return $this;
    }

    //...
}

      

+4


source







All Articles