您可以通过将单向一对多与连接表结合使用来将所有附件放在一个表中。 In doctrine this is done with a unidirectional Many-To-Many with a unique constraint on the join column。这意味着一个带有附件的表,但连接到每个父级的连接表不同。
此解决方案的缺点是它是单向的,这意味着您的附件不知道关系的拥有方。
完整的代码如下所示:
AttachmentsTrait 带有用于附件的 setter 和 getter,以防止代码重复:
<?php
namespace Application\Entity;
use Doctrine\Common\Collections\Collection;
/**
* @property Collection $attachments
*/
trait AttachmentTrait
{
/**
* Add attachment.
*
* @param Attachment $attachment
* @return self
*/
public function addAttachment(Attachment $attachment)
{
$this->attachments[] = $attachment;
return $this;
}
/**
* Add attachments.
*
* @param Collection $attachments
* @return self
*/
public function addAttachments(Collection $attachments)
{
foreach ($attachments as $attachment) {
$this->addAttachment($attachment);
}
return $this;
}
/**
* Remove attachment.
*
* @param Attachment $attachments
*/
public function removeAttachment(Attachment $attachment)
{
$this->attachments->removeElement($attachment);
}
/**
* Remove attachments.
*
* @param Collection $attachments
* @return self
*/
public function removeAttachments(Collection $attachments)
{
foreach ($attachments as $attachment) {
$this->removeAttachment($attachment);
}
return $this;
}
/**
* Get attachments.
*
* @return Collection
*/
public function getAttachments()
{
return $this->attachments;
}
}
您的Mail 实体:
<?php
namespace Application\Entity;
use Doctrine\Common\Collections\Collection;
use Doctrine\Common\Collections\ArrayCollection;
class Mail
{
use AttachmentsTrait;
/**
* @var integer
* @ORM\Id
* @ORM\Column(type="integer", nullable=false)
* @ORM\GeneratedValue(strategy="IDENTITY")
*/
protected $id;
/**
* @ORM\ManyToMany(targetEntity="Attachment")
* @ORM\JoinTable(name="mail_attachments",
* inverseJoinColumns={@ORM\JoinColumn(name="attachment_id", referencedColumnName="id")},
* joinColumns={@ORM\JoinColumn(name="mail_id", referencedColumnName="id", unique=true)}
* )
*/
$attachments;
/**
* Constructor
*/
public function __construct()
{
$this->attachments = new ArrayCollection();
}
}
您的Order 实体:
<?php
namespace Application\Entity;
use Doctrine\Common\Collections\Collection;
use Doctrine\Common\Collections\ArrayCollection;
class Order
{
use AttachmentsTrait;
/**
* @var integer
* @ORM\Id
* @ORM\Column(type="integer", nullable=false)
* @ORM\GeneratedValue(strategy="IDENTITY")
*/
protected $id;
/**
* @ORM\ManyToMany(targetEntity="Attachment")
* @ORM\JoinTable(name="order_attachment",
* inverseJoinColumns={@ORM\JoinColumn(name="attachment_id", referencedColumnName="id")},
* joinColumns={@ORM\JoinColumn(name="order_id", referencedColumnName="id", unique=true)}
* )
*/
$attachments;
/**
* Constructor
*/
public function __construct()
{
$this->attachments = new ArrayCollection();
}
}
您的Ticket 实体:
<?php
namespace Application\Entity;
use Doctrine\Common\Collections\Collection;
use Doctrine\Common\Collections\ArrayCollection;
class Ticket
{
use AttachmentsTrait;
/**
* @var integer
* @ORM\Id
* @ORM\Column(type="integer", nullable=false)
* @ORM\GeneratedValue(strategy="IDENTITY")
*/
protected $id;
/**
* @ORM\ManyToMany(targetEntity="Attachment")
* @ORM\JoinTable(name="ticket_attachment",
* inverseJoinColumns={@ORM\JoinColumn(name="attachment_id", referencedColumnName="id")},
* joinColumns={@ORM\JoinColumn(name="ticket_id", referencedColumnName="id", unique=true)}
* )
*/
$attachments;
/**
* Constructor
*/
public function __construct()
{
$this->attachments= new ArrayCollection();
}
}
编辑:
如果您真的希望Attachment 了解另一方,您可以在两者之间添加一个额外的实体来管理它。这意味着将连接表本身制作为实体,例如:MailAttachment、TicketAttachment 和 OrderAttachment。