我真的希望有人可以帮助我,因为这会让我发疯,这应该很简单
数据库表示例(ID是所有表的主列)
Exhibitors
id | name | address_line_1
3 | Test Name | Test 123 Street
4 | Test Name 2 | Test 123 Street 1
5 | Test Name 3 | Test 123 Street 2
6 | Test Name 4 | Test 123 Street 2
Shows
id | name | location
7 | The Greatest Show | USA
8 | Super Show | London, UK
9 | A Great Show | Toronto, CA
10 | Fab Show | NEC, Birmingham, UK
Links
id | show_id | exhibitor_id | agent_id | type
1 | 7 | 3 | null | 1
1 | 7 | 5 | null | 1
1 | 8 | 3 | null | 1
1 | 10 | 6 | null | 1
现在我要建立的链接是很多很多,所以基本上,每个展览可以有多个参展商,反之亦然,每个参展商可以分配给多个展览。当前实体正在创建一个名为ppShowExhibLinks的全新表,我真的不希望它这样做,但是我真的很卡住...
“链接”表将保存不同类型的链接,因此包含类型和agent_id,因此我试图以某种方式使关系执行以下操作:
$ show = new Show(); $ show-> getExhibitors();
然后,该操作类似于“获取所有链接,其中show_id是当前节目,类型是1”
然后相反
$ show = new Exhibitor(); $ show-> getShows(); 然后,该操作类似于“获取所有链接,其中exhibitor_id是当前参展商,类型是1”
PS:我知道该对象的新实例不会显示/显示任何链接,而只是演示该对象。
Entities
/**
* @ORM\Table(name="ppShows")
* @ORM\Entity(repositoryClass="App\Repository\ShowRepository")
*/
class Show
{
/**
* @ORM\Id()
* @ORM\GeneratedValue()
* @ORM\Column(type="integer")
*/
private $id;
/**
* @ORM\Column(type="string", length=255, nullable=false)
*/
protected $name;
/**
* @ORM\Column(type="string", length=255, nullable=false)
*/
protected $location;
/**
* @var \Doctrine\Common\Collections\Collection|Exhibitor[]
*
* @ORM\ManyToMany(targetEntity="Exhibitor", inversedBy="shows")
* @ORM\JoinTable(
* name="ppShowExhibLinks",
* joinColumns={
* @ORM\JoinColumn(name="show_id", referencedColumnName="id")
* },
* inverseJoinColumns={
* @ORM\JoinColumn(name="exhibitor_id", referencedColumnName="id")
* }
* )
*/
protected $exhibitors;
********************************************
class Exhibitor
{
/**
* @ORM\Id()
* @ORM\GeneratedValue()
* @ORM\Column(type="integer")
*/
private $id;
/**
* @ORM\Column(type="string", length=255, nullable=false)
*/
protected $name;
/**
* @ORM\Column(type="string", length=255, nullable=false)
*/
protected $address_line_1;
/**
* @var \Doctrine\Common\Collections\Collection|Show[]
*
* @ORM\ManyToMany(targetEntity="Show", inversedBy="exhibitors")
* @ORM\JoinTable(
* name="ppShowExhibLinks",
* joinColumns={
* @ORM\JoinColumn(name="show_id", referencedColumnName="id")
* },
* inverseJoinColumns={
* @ORM\JoinColumn(name="exhibitor_id", referencedColumnName="id")
* }
* )
*/
protected $shows;
class Link
{
/**
* @ORM\Id()
* @ORM\GeneratedValue()
* @ORM\Column(type="integer")
*/
private $id;
/**
* @ORM\Column(type="integer", options={"default" : 0}, nullable=true)
*/
protected $show_id;
/**
* @ORM\Column(type="integer", options={"default" : 0}, nullable=true)
*/
protected $exhibitor_id;
/**
* @ORM\Column(type="integer", options={"default" : 0}, nullable=true)
*/
protected $agent_id;
/**
* @ORM\Column(type="integer", options={"default" : 1}, nullable=false)
*/
protected $type;
答案 0 :(得分:0)
您必须同时为Link
和Show
表添加到Exhibitor
的关系。
显示实体:
/**
* One show may have many links
* @OneToMany(targetEntity="Link", mappedBy="show")
*/
private $links;
// ...
public function __construct() {
$this->links = new ArrayCollection();
}
参展商实体
/**
* One Exhibitor may have many links
* @OneToMany(targetEntity="Link", mappedBy="exhibitor")
*/
private $links;
// ...
public function __construct() {
$this->links = new ArrayCollection();
}
链接实体:
/**
* Many links have one product.
* @ManyToOne(targetEntity="Show", inversedBy="links")
* @JoinColumn(name="show_id", referencedColumnName="id")
*/
private $show;
/**
* Many links have one exhibitor.
* @ManyToOne(targetEntity="Exhibitor", inversedBy="links")
* @JoinColumn(name="exhibitor_id", referencedColumnName="id")
*/
private $exhibitor;
现在,Show
和Exhibitor
现在通过多对多关系相互关联,并且都通过多对一关系与第三Link
实体相关。那可能是您在寻找。现在,您可以像这样从参展商那里获得所有展览
$exhibitor->getShows()
反之亦然
$show->getExibitors();
最后,从存储库获取所有链接
$this->linkRepository->findBy([ "show" => %show_id%, "type" => %type% ]);