我试图创建一个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
& Article.php等方法?为什么它没有尝试创建ManyToMany joinTable?
答案 0 :(得分:1)
您是否尝试过添加连接列?也许有帮助。
/**
* @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;
此外,您似乎稍后添加了标记并忘记再次调用生成实体,因为我无法在代码中找到标记的getter和setter。尝试再次运行doctrine:generate:entites
。
最后,您是否检查过您的数据库是否已存在该表? (这不应该是可能的,但检查你是否没有任何其他想法并没有坏处。)