Discussion:
[Symfony2] Form + oneToMany + EntityType; cascade not working
Ruben de Vries
2011-05-28 16:54:23 UTC
Permalink
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-/***@public.gmane.org
To unsubscribe from this group, send email to
symfony-users+unsubscribe-/***@public.gmane.org
For more options, visit this group at
http://groups.google.com/group/symfony-users?hl=en
Ruben de Vries
2011-05-28 16:57:17 UTC
Permalink
https://github.com/rubensayshi/DemoBundle I placed the whole bundle on
github so you can take a look
--
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-/***@public.gmane.org
To unsubscribe from this group, send email to
symfony-users+unsubscribe-/***@public.gmane.org
For more options, visit this group at
http://groups.google.com/group/symfony-users?hl=en
Ruben de Vries
2011-05-30 16:35:11 UTC
Permalink
Sorry, but after another few hours of digging I still havent fixed
this and its really holding me back so: *bump*
--
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-/***@public.gmane.org
To unsubscribe from this group, send email to
symfony-users+unsubscribe-/***@public.gmane.org
For more options, visit this group at
http://groups.google.com/group/symfony-users?hl=en
Stephan Petzl
2011-05-31 17:20:25 UTC
Permalink
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
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
      targetEntity: DemoComment
      mappedBy: author
      cascade: ["persist"]
      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-/***@public.gmane.org
To unsubscribe from this group, send email to
symfony-users+unsubscribe-/***@public.gmane.org
For more options, visit this group at
http://groups.google.com/group/symfony-users?hl=en
Ruben de Vries
2011-06-04 09:21:00 UTC
Permalink
cascade persist works like a charm (check out my '>> this does work'
example) when called stand alone, but it just doesn't work when going
through the form.
because it seems the form API doesn't use the get/set methods to set
the properties o.O?
Without knowing whethercascade:persistshould 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
$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...
BR Stephan
Post by Ruben de Vries
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
      targetEntity: DemoComment
      mappedBy: author
     cascade: ["persist"]
      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 thepersistdoes 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-/***@public.gmane.org
To unsubscribe from this group, send email to
symfony-users+unsubscribe-/***@public.gmane.org
For more options, visit this group at
http://groups.google.com/group/symfony-users?hl=en
Ruben de Vries
2011-06-06 07:06:42 UTC
Permalink
*bump* I'm still stuck here :-(
Post by Ruben de Vries
cascade persist works like a charm (check out my '>> this does work'
example) when called stand alone, but it just doesn't work when going
through the form.
because it seems the form API doesn't use the get/set methods to set
the properties o.O?
Without knowing whethercascade:persistshould 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
$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...
BR Stephan
Post by Ruben de Vries
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
      targetEntity: DemoComment
      mappedBy: author
     cascade: ["persist"]
      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 thepersistdoes 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-/***@public.gmane.org
To unsubscribe from this group, send email to
symfony-users+unsubscribe-/***@public.gmane.org
For more options, visit this group at
http://groups.google.com/group/symfony-users?hl=en
Loading...