Without knowing whether cascade:persist should do the job, i guess it
could be an issue with owning and inverse side of your relation.[1]
> "The owning side of a relationship determines the updates to the relationship 
> in the database."
The owning side is your comment (since it is the one with the
inversedBy property), so maybe you should try to save it this way:

$comment->setAuthor($user);

and then look whether it gets persisted.
what i would love to know myself is, how the symfony form framework
handles bidirectional relations like that...

[1] 
http://www.doctrine-project.org/docs/orm/2.0/en/reference/association-mapping.html#owning-side-and-inverse-side

BR Stephan

On May 28, 6:54 pm, Ruben de Vries <rubensay...@gmail.com> wrote:
> I've been going at this problem for hours now so I figured I better
> just ask for help to avoid my head exploding soon ...
>
> I'll paste some snippets and then try to explain.
>
> >> yaml mapping for doctrime ORM
>
> GoGreat\DemoBundle\Entity\DemoUser:
>   oneToMany:
>     comments:
>       targetEntity: DemoComment
>       mappedBy: author
>       cascade: ["persist"]
>
> GoGreat\DemoBundle\Entity\DemoComment:
>   manyToOne:
>     author:
>       targetEntity: DemoUser
>       inversedBy: comments
>
> >> DemoUser entity
>
> class GoGreat\DemoBundle\Entity\DemoUser {
>
>     public function addComments($comments)
>     {
>         $comments->setAuthor($this);
>         $this->comments[] = $comments;
>     }
>
> }
> >> DemoUserType form
>
> class GoGreat\DemoBundle\Form\DemoUserType {
>         public function buildForm(FormBuilder $builder, array $options)
>         {
>                 $builder->add('name', 'text');
>                 $builder->add('comments', 'entity', array(
>                         'class'         => 
> 'GoGreat\DemoBundle\Entity\DemoComment',
>                         'property'              => 'body',
>                         'expanded'              => true,
>                         'multiple'              => true,
>                 ));
>         }
>
> }
> >> this does work
>
> $user           = new DemoUser();
> $comment                = new DemoComment();
>
> $user->addComments($comment);
>
> $em->persist($user);
> $em->flush();
>
> >> this doesn't work
>
> $form           = $this->get('form.factory')->create(new DemoUserType());
> $request                = $this->get('request');
> $user           = new DemoUser();
> $em                     = $this->get('doctrine.orm.entity_manager');
>
> $form->setData($user);
>
> if ($request->getMethod() == 'POST') {
>         $form->bindRequest($request);
>         if ($form->isValid()) {
>
>                 foreach ($user->getComments() as $comment) {
>                         var_dump($comment->getBody(), $comment->getAuthor());
>                 }
>
>                 $em->persist($user);
>                 $em->flush();
>         }
>
> }
>
> the var_dump just before the persist does throw out the body, but it
> doesn't have an author.
> I think the form isn't using DemoUser->addComments to set the
> comments, but how else could it set the comments?
>
> What am I doing wrong?
>
> If the examples aren't clear enough I can put the demo bundle on
> github for you to check ...

-- 
If you want to report a vulnerability issue on symfony, please send it to 
security at symfony-project.com

You received this message because you are subscribed to the Google
Groups "symfony users" group.
To post to this group, send email to symfony-users@googlegroups.com
To unsubscribe from this group, send email to
symfony-users+unsubscr...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/symfony-users?hl=en

Reply via email to