symfony 2 多对多关系的固定装置

symfony 2 Fixtures for many to many relationship

如何在多对多关系中为 symfony 2 设置固定装置, 以下 2 个实体由命令行创建, 然后为多对多关系添加一些行

实体 1:博客类

<?php

namespace Acme\MainBundle\Entity;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\ORM\Mapping as ORM;

/**
 * Blog
 *
 * @ORM\Table()
 * @ORM\Entity(repositoryClass="Acme\MainBundle\Entity\BlogRepository")
*/
 class Blog {

/**
 * @var integer
 *
 * @ORM\Column(name="id", type="integer")
 * @ORM\Id
 * @ORM\GeneratedValue(strategy="AUTO")
 */
private $id;

/**
 * @ORM\ManyToMany(targetEntity="tag", inversedBy="blogs")
 * @ORM\JoinTable(name="blog_tag",
 *      joinColumns={@ORM\JoinColumn(name="blog_id", referencedColumnName="id")},
 *      inverseJoinColumns={@ORM\JoinColumn(name="tag_id", referencedColumnName="id")}
 *      )
 **/
protected $tags;

public function __construct()
{
    $this->tags = new ArrayCollection();
}


/**
 * @var string
 *
 * @ORM\Column(name="title", type="string", length=255)
 */
private $title;

/**
 * @var string
 *
 * @ORM\Column(name="subTitle", type="text")
 */
private $subTitle;

/**
 * @var string
 *
 * @ORM\Column(name="author", type="string", length=255)
 */

private $author;

/**
 * @var \DateTime
 *
 * @ORM\Column(name="createdAt", type="datetime")
 */
private $createdAt;

/**
 * @var \DateTime
 *
 * @ORM\Column(name="updatedAt", type="datetime")
 */
private $updatedAt;

/**
 * @var string
 *
 * @ORM\Column(name="content", type="text")
 */
private $content;

/**
 * @var boolean
 *
 * @ORM\Column(name="isPublished", type="boolean")
 */
private $isPublished;

/**
 * Get id
 *
 * @return integer 
 */
public function getId() {
    return $this->id;
}

/**
 * Set title
 *
 * @param string $title
 * @return Blog
 */
public function setTitle($title) {
    $this->title = $title;

    return $this;
}

/**
 * Get title
 *
 * @return string 
 */
public function getTitle() {
    return $this->title;
}

/**
 * Set author
 *
 * @param string $author
 * @return Blog
 */
public function setAuthor($author) {
    $this->author = $author;

    return $this;
}

/**
 * Get author
 *
 * @return string 
 */
public function getAuthor() {
    return $this->author;
}

/**
 * Set createdAt
 *
 * @param \DateTime $createdAt
 * @return Blog
 */
public function setCreatedAt($createdAt) {
    $this->createdAt = $createdAt;

    return $this;
}

/**
 * Get createdAt
 *
 * @return \DateTime 
 */
public function getCreatedAt() {
    return $this->createdAt;
}

/**
 * Set updatedAt
 *
 * @param \DateTime $updatedAt
 * @return Blog
 */
public function setUpdatedAt($updatedAt) {
    $this->updatedAt = $updatedAt;

    return $this;
}

/**
 * Get updatedAt
 *
 * @return \DateTime 
 */
public function getUpdatedAt() {
    return $this->updatedAt;
}

/**
 * Set content
 *
 * @param string $content
 * @return Blog
 */
public function setContent($content) {
    $this->content = $content;

    return $this;
}

/**
 * Get content
 *
 * @return string 
 */
public function getContent() {
    return $this->content;
}

/**
 * Set isPublished
 *
 * @param boolean $isPublished
 * @return Blog
 */
public function setIsPublished($isPublished) {
    $this->isPublished = $isPublished;

    return $this;
}

/**
 * Get isPublished
 *
 * @return boolean 
 */
public function getIsPublished() {
    return $this->isPublished;
}

public function __toString() {
    return strval($this->id);
}


/**
 * Set subTitle
 *
 * @param string $subTitle
 * @return Blog
 */
public function setSubTitle($subTitle)
{
    $this->subTitle = $subTitle;

    return $this;
}

/**
 * Get subTitle
 *
 * @return string 
 */
public function getSubTitle()
{
    return $this->subTitle;
}

/**
 * Add tags
 *
 * @param \Acme\MainBundle\Entity\tag $tags
 * @return Blog
 */
public function addTag(\Acme\MainBundle\Entity\tag $tags)
{
    $this->tags[] = $tags;
    $tags->addBlog($this);

    return $this;
}

/**
 * Remove tags
 *
 * @param \Acme\MainBundle\Entity\tag $tags
 */
public function removeTag(\Acme\MainBundle\Entity\tag $tags)
{
    $this->tags->removeElement($tags);
}

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

实体 2:标记类

<?php

namespace Acme\MainBundle\Entity;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\ORM\Mapping as ORM;

/**
 * Tag
 *
 * @ORM\Table()
 * @ORM\Entity(repositoryClass="Acme\MainBundle\Entity\TagRepository")
 */
class Tag {

/**
 * @var integer
 *
 * @ORM\Column(name="id", type="integer")
 * @ORM\Id
 * @ORM\GeneratedValue(strategy="AUTO")
 */
private $id;

/**
 * @ORM\ManyToMany(targetEntity="blog", mappedBy="tags")
 **/

protected $blogs;

/**
 * @var string
 *
 * @ORM\Column(name="tag", type="string", length=255)
 */
private $tag;

/**
 * @var boolean
 *
 * @ORM\Column(name="isPublished", type="boolean")
 */
private $isPublished;

/**
 * Get id
 *
 * @return integer 
 */
public function getId() {
    return $this->id;
}

/**
 * Set tag
 *
 * @param string $tag
 * @return Tag
 */
public function setTag($tag) {
    $this->tag = $tag;

    return $this;
}

/**
 * Get tag
 *
 * @return string 
 */
public function getTag() {
    return $this->tag;
}

/**
 * Set isPublished
 *
 * @param boolean $isPublished
 * @return Tag
 */
public function setIsPublished($isPublished) {
    $this->isPublished = $isPublished;

    return $this;
}

/**
 * Get isPublished
 *
 * @return boolean 
 */
public function getIsPublished() {
    return $this->isPublished;
}

public function __toString() {
    return strval($this->id);
}

/**
 * Constructor
 */
public function __construct()
{
    $this->blogs = new \Doctrine\Common\Collections\ArrayCollection();
}

/**
 * Add blogs
 *
 * @param \Acme\MainBundle\Entity\blog $blogs
 * @return Tag
 */
public function addBlog(\Acme\MainBundle\Entity\blog $blogs)
{
    $this->blogs[] = $blogs;
    $blogs->addTag($this);


    return $this;
}

/**
 * Remove blogs
 *
 * @param \Acme\MainBundle\Entity\blog $blogs
 */
public function removeBlog(\Acme\MainBundle\Entity\blog $blogs)
{
    $this->blogs->removeElement($blogs);
}

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

注意!!!

您的代码中存在循环引用!!! 当你给博客添加标签时,会调用add blog to tag等,请修改为:

博客

/**
 * Add tags
 *
 * @param \Acme\MainBundle\Entity\tag $tags
 * @return Blog
 */
public function addTag(\Acme\MainBundle\Entity\tag $tags)
{
    if (!$this->tags->contains($tags))
    {
        $this->tags[] = $tags;
        $tags->addBlog($this);
    }

    return $this;
}

标签

/**
 * Add blogs
 *
 * @param \Acme\MainBundle\Entity\blog $blogs
 * @return Tag
 */
public function addBlog(\Acme\MainBundle\Entity\blog $blogs)
{
    if (!$this->blogs->contains($blogs))
    {
        $this->blogs[] = $blogs;
        $blogs->addTag($this);
    }


    return $this;
}

对于你的问题:

首先,安装并配置 DoctrineFixturesBundle。 使用有序的夹具功能首先加载标签,然后加载博客。

那我写给你这个例子fixtures:

TagFixtures

<?php

namespace Acme\MainBundle\DataFixtures\ORM;

use Acme\MainBundle Bundle\Entity\Tag;
use Doctrine\Common\DataFixtures\AbstractFixture;
use Doctrine\Common\Persistence\ObjectManager;
use Doctrine\Common\DataFixtures\OrderedFixtureInterface;

class TagFixtures  extends AbstractFixture implements OrderedFixtureInterface {

    /**
     * Load data fixtures with the passed EntityManager
     *
     * @param \Doctrine\Common\Persistence\ObjectManager $manager
     */
    function load(ObjectManager $manager)
    {
        $tag1 = new Tag();
        $tag1->setTag("tag name 1");
        $tag1->setIsPublished(true);

        $tag2 = new Tag();
        $tag2->setTag("tag name 2");
        $tag2->setIsPublished(true);

        $manager->persist($tag1);
        $manager->persist($tag2);
        $this->addReference('tag-tag_1', $tag1);
        $this->addReference('tag-tag_2', $tag2);

        $manager->flush();

    }

    /**
     * Get the order of this fixture
     *
     * @return integer
     */
    function getOrder()
    {
        return 1;
    }
}

BlogFixtures

<?php

namespace Acme\MainBundle\DataFixtures\ORM;

use Acme\MainBundle\Entity\Blog;
use Doctrine\Common\DataFixtures\AbstractFixture;
use Doctrine\Common\Persistence\ObjectManager;
use Doctrine\Common\DataFixtures\OrderedFixtureInterface;

class BlogFixtures  extends AbstractFixture implements OrderedFixtureInterface {

    /**
     * Load data fixtures with the passed EntityManager
     *
     * @param \Doctrine\Common\Persistence\ObjectManager $manager
     */
    function load(ObjectManager $manager)
    {

        $blog = new Blog();
        $blog->setTitle('Happy new year');
        $blog->setAuthor("me");
        $blog->setIsPublished(true);
        $blog->setContent("Happy new year");
        $blog->setSubTitle("2015");
        $blog->setCreatedAt(new \DateTime('now'));
        $blog->setUpdatedAt(new \DateTime('now'));

        $blog->addTag($this->getReference('tag-tag_1'));
//        $blog->addTag($this->getReference('tag-tag_2'));

        $manager->persist($blog);
//        $this->addReference('blog-blog_1', $blog);

        $manager->flush();

    }

    /**
     * Get the order of this fixture
     *
     * @return integer
     */
    function getOrder()
    {
        return 2;
    }
}

希望对您有所帮助,新年快乐!

对于简单的情况,我会创建我的固定装置:

Acme/DemoBundle/Entity/Tag.php

public function addBlog(\Acme\MainBundle\Entity\blog $blog)
{
    $this->blogs[] = $blog;

    return $this;
}

Acme/DemoBundle/Entity/Blog.php

public function addTag(\Acme\MainBundle\Entity\tag $tags)
{
    $this->tags[] = $tags;
    $tags->addBlog($this);

    return $this;
}

Acme/DemoBundle/DataFixtures/ORM/TagBlogFixture.php

class TagBlogFixture extends AbstractFixture implements OrderedFixtureInterface {
    function load(ObjectManager $manager)
    {
        $tag1 = new Tag();
        // setters

        $tag2 = new Tag();
        // setters

        $blog1 = new Blog();
        // setters

        $blog1
            ->addTag($tag1)
            ->addTag($tag2)
        ;

        $blog2 = new Blog();
        // setters

        $blog2->addTag($tag2);

        $manager->persist($tag1);
        $manager->persist($tag2);
        $manager->persist($blog1);
        $manager->persist($blog2);

        $manager->flush();
    }

    function getOrder()
    {
        return 1;
    }
}

这不会在对象之间共享任何引用,而是在单个文件中创建它们..