我有这两个模型:
新闻:
/** @Entity @Table(name="news") */
class News {
/**
* @Id @GeneratedValue @Column(type="integer")
* @var integer
*/
protected $id;
/**
* @Column(type="string", length=100)
* @var string
*/
protected $title;
/**
* @Column(type="text")
* @var string
*/
protected $content;
/**
* @ManyToMany(targetEntity="Tag", mappedBy="news_tags")
*/
protected $tags;
public function __construct() {
$this->tags = new \Doctrine\Common\Collections\ArrayCollection();
}
}
代码:
/** @Entity @Table(name="tags") */
class Tag {
/**
* @Id @GeneratedValue @Column(type="integer")
*/
protected $id;
/**
* @Column(type="string", length=100, unique=TRUE)
* @var string
*/
protected $name;
/**
* @Column(type="integer")
* @var integer
*/
protected $amount = 1;
/**
* @ManyToMany(targetEntity="News", inversedBy="tags")
* @JoinTable(name="news_tags",
* joinColumns={@JoinColumn(name="tag_id", referencedColumnName="id")},
* inverseJoinColumns={@JoinColumn(name="news_id", referencedColumnName="id")}
* )
*/
private $news_tags;
public function __construct() {
$this->news_tags = new \Doctrine\Common\Collections\ArrayCollection;
}
}
使用上面的代码,我有三个表:news
,tags
和news_tags
。任何人都可以告诉我如何绑定,例如,三个标签与一个新闻,我已经创建(一些行将被添加到news_tags
)?