如何在 Symfony 中的多对多关系的中介 table 中创建其他列?
How to create other columns inside the intermediary table of a many-to-many relationship in Symfony?
有两个实体具有多对多关系:
/**
* @ORM\Entity(repositoryClass=KbFavorisRepository::class)
*/
class KbFavoris
{
/**
* @ORM\Id()
* @ORM\GeneratedValue()
* @ORM\Column(type="integer")
*/
private $id;
...
/**
* @var \Doctrine\Common\Collections\Collection|KbCollection[]
*
* @ORM\ManyToMany(targetEntity="KbCollection", mappedBy="kb_favoris",cascade={"persist"})
*/
protected $collection;
...
}
/**
* @ORM\Entity(repositoryClass=KbCollectionRepository::class)
*/
class KbCollection
{
/**
* @ORM\Id()
* @ORM\GeneratedValue()
* @ORM\Column(type="integer")
* @JMS\Groups({"list_collection"})
*/
private $id;
...
/**
* @var KbFavoris
* @ORM\ManyToMany(targetEntity="KbFavoris",inversedBy="collection", cascade={"remove"})
* @ORM\JoinTable(
* name="kb_favoris_collection",
* joinColumns={
* @ORM\JoinColumn(name="collection_id", referencedColumnName="id")
* },
* inverseJoinColumns={
* @ORM\JoinColumn(name="favoris_id", referencedColumnName="id")
* }
* )
*/
private $kb_favoris;
...
}
如何在多对多关系的中介table里面添加另一列?
如果你想添加一些额外的字段,你需要创建另一个实体;
例如:
class KbFavoris
{
// OneToMany here to KbFavorisKbCollection, is array|collection
}
class KbCollection
{
// OneToMany here to KbFavorisKbCollection , is array|collection
}
和新的 class :
class KbFavorisKbCollection
{
// ManyToOne here to KbFavoris (represent one KbFavoris)
// ManyToOne here to KbCollection (represent one KbCollection )
}
有两个实体具有多对多关系:
/**
* @ORM\Entity(repositoryClass=KbFavorisRepository::class)
*/
class KbFavoris
{
/**
* @ORM\Id()
* @ORM\GeneratedValue()
* @ORM\Column(type="integer")
*/
private $id;
...
/**
* @var \Doctrine\Common\Collections\Collection|KbCollection[]
*
* @ORM\ManyToMany(targetEntity="KbCollection", mappedBy="kb_favoris",cascade={"persist"})
*/
protected $collection;
...
}
/**
* @ORM\Entity(repositoryClass=KbCollectionRepository::class)
*/
class KbCollection
{
/**
* @ORM\Id()
* @ORM\GeneratedValue()
* @ORM\Column(type="integer")
* @JMS\Groups({"list_collection"})
*/
private $id;
...
/**
* @var KbFavoris
* @ORM\ManyToMany(targetEntity="KbFavoris",inversedBy="collection", cascade={"remove"})
* @ORM\JoinTable(
* name="kb_favoris_collection",
* joinColumns={
* @ORM\JoinColumn(name="collection_id", referencedColumnName="id")
* },
* inverseJoinColumns={
* @ORM\JoinColumn(name="favoris_id", referencedColumnName="id")
* }
* )
*/
private $kb_favoris;
...
}
如何在多对多关系的中介table里面添加另一列?
如果你想添加一些额外的字段,你需要创建另一个实体; 例如:
class KbFavoris
{
// OneToMany here to KbFavorisKbCollection, is array|collection
}
class KbCollection
{
// OneToMany here to KbFavorisKbCollection , is array|collection
}
和新的 class :
class KbFavorisKbCollection
{
// ManyToOne here to KbFavoris (represent one KbFavoris)
// ManyToOne here to KbCollection (represent one KbCollection )
}