学说:一对多的关系和级联持久性

时间:2014-10-16 10:20:47

标签: doctrine-orm doctrine

我的意思是我有这两个类(一对多的关系)。正如你可以理解的那样,我已经包括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;
    }
}

在我的控制器中我有这个:

    $task = new Task();
    $tag = new Tag();
    $task->addTag($tag);

    $em = $this->getDoctrine()->getManager();
    $em->persist($item);
    $em->flush();

但外键总是NULL,为什么?

1 个答案:

答案 0 :(得分:0)

我认为在Task课程中,addTagremoveTag应如下所示:

public function addTag($tag)
{
    $tag->setTask($this);
    $this->tags->add($tag);
}

public function removeTag($tag)
{
    $tag->setTask(null);
    $this->tags->removeElement($tag);
}

或者,如果您想在一个函数调用中添加和删除许多标记,请使用以下命令:

public function addTags(Collection $tags)
{
    foreach ($tags as $tag) {
        $tag->setTask($this);
        $this->tags->add($tag);
    }
}

public function removeTags(Collection $tags)
{
    foreach ($tags as $tag) {
        $tag->setTask(null);
        $this->tags->removeElement($tag);
    }
}