Hi Javier,

First off, don't put `cascade=...` everywhere blindly. You hardly ever need it 
to be at both sides of the association. Looking at these entities, I would say 
you only need it at the Task side, not at te Tag side.

Secondly, when working with biderectional associations, you should make sure 
both sides are kept in sync. This means that when A is set on / added to B, 
then B must also be set on / added to A.

I suggest you change the `addTag()` and `removeTag()` methods of Task to:

    public function addTag(Tag $tag)
    {
        if (!$this->tags->contains($tag)) {
            $this->tags->add($tag);
            $tag->setTask($this);
        }
    }
 
    public function removeTag(Tag $tag)
    {
        if ($this->tags->contains($tag)) {
            $this->tags->removeElement($tag);
            $tag->setTask(null);
        }
    }

Now whenever you add/remove a Tag from a Task, that Task is also set/unset on 
the Tag.
This should resolve your problem.

--  
Jasper N. Brouwer
(@jaspernbrouwer)


On 16 October 2014 at 12:28:25, Javier Garcia ([email protected]) wrote:
> Hi,
>  
> I mean I have these two classes (one to many relationship). As you can
> appreciate in both I have included `cascade={"persist"}`.
>  
>  
> >  
>  
> namespace Project\FrontendBundle\Entity;
>  
> use Doctrine\ORM\Mapping as ORM;
>  
> /**
> * @ORM\Entity
> * @ORM\Table(name="task")
> */
> class Task
> {
> /**
> * @ORM\Id
> * @ORM\Column(type="integer")
> * @ORM\GeneratedValue(strategy="AUTO")
> */
> protected $id;
>  
> /**
> * @ORM\Column(type="string", length=255, name="description",
> nullable=true)
> */
> protected $description;
>  
> /**
> * @ORM\OneToMany(targetEntity="Tag", mappedBy="task",
> cascade={"persist"})
> **/
> protected $tags;
>  
>  
> /*************************************/
> public function __toString()
> {
> return $this->description;
> }
>  
> /**
> * Constructor
> */
> public function __construct()
> {
> $this->tags = new \Doctrine\Common\Collections\ArrayCollection
> ();
> }
>  
> /**
> * Get id
> *
> * @return integer
> */
> public function getId()
> {
> return $this->id;
> }
>  
> /**
> * Set description
> *
> * @param string $description
> * @return Task
> */
> public function setDescription($description)
> {
> $this->description = $description;
>  
> return $this;
> }
>  
> /**
> * Get description
> *
> * @return string
> */
> public function getDescription()
> {
> return $this->description;
> }
>  
> /**
> * Add tags
> *
> * @param \Project\FrontendBundle\Entity\Tag $tags
> * @return Task
> */
> public function addTag(\Project\FrontendBundle\Entity\Tag $tags)
> {
> $this->tags[] = $tags;
>  
> return $this;
> }
>  
> /**
> * Remove tags
> *
> * @param \Project\FrontendBundle\Entity\Tag $tags
> */
> public function removeTag(\Project\FrontendBundle\Entity\Tag $tags)
> {
> $this->tags->removeElement($tags);
> }
>  
> /**
> * Get tags
> *
> * @return \Doctrine\Common\Collections\Collection
> */
> public function getTags()
> {
> return $this->tags;
> }
> }
>  
>  
>  
>  
>  
> >  
>  
> namespace Project\FrontendBundle\Entity;
>  
> use Doctrine\ORM\Mapping as ORM;
>  
> /**
> * @ORM\Entity
> * @ORM\Table(name="tag")
> */
> class Tag
> {
> /**
> * @ORM\Id
> * @ORM\Column(type="integer")
> * @ORM\GeneratedValue(strategy="AUTO")
> */
> protected $id;
>  
> /**
> * @ORM\Column(type="string", length=255, name="name",
> nullable=true)
> */
> protected $name;
>  
> /**
> * @ORM\ManyToOne(targetEntity="Task", inversedBy="tags",
> cascade={"persist"})
> * @ORM\JoinColumn(name="task_id", referencedColumnName="id")
> **/
> private $task;
>  
>  
>  
> /**
> * Get id
> *
> * @return integer
> */
> public function getId()
> {
> return $this->id;
> }
>  
> /**
> * Set name
> *
> * @param string $name
> * @return Tag
> */
> public function setName($name)
> {
> $this->name = $name;
>  
> return $this;
> }
>  
> /**
> * Get name
> *
> * @return string
> */
> public function getName()
> {
> return $this->name;
> }
>  
> /**
> * Set task
> *
> * @param \Project\FrontendBundle\Entity\Task $task
> * @return Tag
> */
> public function setTask(\Project\FrontendBundle\Entity\Task $task =
> null)
> {
> $this->task = $task;
>  
> return $this;
> }
>  
> /**
> * Get task
> *
> * @return \Project\FrontendBundle\Entity\Task
> */
> public function getTask()
> {
> return $this->task;
> }
> }
>  
>  
> In my controller I have this:
>  
> $task = new Task();
> $tag = new Tag();
> $task->addTag($tag);
>  
>  
> $em = $this->getDoctrine()->getManager();
> $em->persist($item);
> $em->flush();
>  
>  
> but the foreign key is always NULL, why?


-- 
You received this message because you are subscribed to the Google Groups 
"doctrine-user" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To post to this group, send email to [email protected].
Visit this group at http://groups.google.com/group/doctrine-user.
For more options, visit https://groups.google.com/d/optout.

Reply via email to