簡體   English   中英

Symfony2學說ManyToMany關系未調和

[英]Symfony2 Doctrine ManyToMany relationship not reconized

我正在嘗試創建ManyToMany關系。 這是代碼:

Article.php

namespace Heitor\ProjetoBundle\Entity;

use Doctrine\ORM\Mapping as ORM;

/**
 * Article
 *
 * @ORM\Table()
 * @ORM\Entity
 */
class Article
{
    /**
     * @var integer
     *
     * @ORM\Column(name="id", type="integer")
     * @ORM\Id
     * @ORM\GeneratedValue(strategy="AUTO")
     */
    protected $id;

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

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

    /**
     * @ORM\ManyToOne(targetEntity="Author", inversedBy="articles")
     */
    protected $author;

    /*
     * @ORM\ManyToMany(targetEntity="Tag", inversedBy="articles")
     * @ORM\JoinTable(name="articles_tags")
     */
    protected $tags;

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

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

        return $this;
    }

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

    /**
     * Set text
     *
     *
     * @param string $text
     * @return Article
     */
    public function setText($text)
    {
        $this->text = $text;

        return $this;
    }

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

    /**
     * Set author
     *
     * @param \Heitor\ProjetoBundle\Entity\Author $author
     * @return Article
     */
    public function setAuthor(\Heitor\ProjetoBundle\Entity\Author $author = null)
    {
        $this->author = $author;

        return $this;
    }

    /**
     * Get author
     *
     * @return \Heitor\ProjetoBundle\Entity\Author 
     */
    public function getAuthor()
    {
        return $this->author;
    }
}

Tag.php

namespace Heitor\ProjetoBundle\Entity;

use Doctrine\ORM\Mapping as ORM;

/**
 * Tag
 *
 * @ORM\Table()
 * @ORM\Entity
 */
class Tag
{
    /**
     * @var integer
     *
     * @ORM\Column(name="id", type="integer")
     * @ORM\Id
     * @ORM\GeneratedValue(strategy="AUTO")
     */
    protected $id;

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

    /**
     * @ORM\ManyToMany(targetEntity="Article", mappedBy="tags")
     */
    protected $articles;

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

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

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

        return $this;
    }

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

    /**
     * Add articles
     *
     * @param \Heitor\ProjetoBundle\Entity\Article $articles
     * @return Tag
     */
    public function addArticle(\Heitor\ProjetoBundle\Entity\Article $articles)
    {
        $this->articles[] = $articles;

        return $this;
    }

    /**
     * Remove articles
     *
     * @param \Heitor\ProjetoBundle\Entity\Article $articles
     */
    public function removeArticle(\Heitor\ProjetoBundle\Entity\Article $articles)
    {
        $this->articles->removeElement($articles);
    }

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

每個類上的方法都是通過doctrine:generate:entites命令自動生成的,我只是事先進行了屬性和映射。

然后,當我嘗試使用doctrine:schema:update --dump-sql創建表時,它不會嘗試創建articles_tags關系:

CREATE TABLE Tag (id INT AUTO_INCREMENT NOT NULL, description VARCHAR(255) NOT NULL, PRIMARY KEY(id)) DEFAULT CHARACTER SET utf8 COLLATE utf8_unicode_ci ENGINE = InnoDB;
CREATE TABLE Author (id INT AUTO_INCREMENT NOT NULL, name VARCHAR(255) NOT NULL, PRIMARY KEY(id)) DEFAULT CHARACTER SET utf8 COLLATE utf8_unicode_ci ENGINE = InnoDB;
CREATE TABLE Article (id INT AUTO_INCREMENT NOT NULL, author_id INT DEFAULT NULL, title VARCHAR(255) NOT NULL, text LONGTEXT NOT NULL, INDEX IDX_CD8737FAF675F31B (author_id), PRIMARY KEY(id)) DEFAULT CHARACTER SET utf8 COLLATE utf8_unicode_ci ENGINE = InnoDB;
ALTER TABLE Article ADD CONSTRAINT FK_CD8737FAF675F31B FOREIGN KEY (author_id) REFERENCES Author (id);

為什么不嘗試在addTag上創建addTag等方法? 為什么不嘗試創建ManyToMany joinTable?

您是否嘗試添加聯接列? 也許有幫助。

/**
 * @ORM\ManyToMany(targetEntity="Tag", inversedBy="articles")
 * @ORM\JoinTable(name="articles_tags",
 *      joinColumns={@JoinColumn(name="tag_id", referencedColumnName="id")},
 *      inverseJoinColumns={@JoinColumn(name="article_id", referencedColumnName="id")}
 *      )
 */
 protected $tags;

另外,似乎您后來添加了標簽,卻忘記了再次調用generate實體,因為我在代碼中找不到標簽的getter和setter方法。 嘗試再次運行doctrine:generate:entites

最后,您是否檢查過數據庫中沒有該表? (這是不可能的,但是檢查您是否沒有其他想法也沒有關系。)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM