0
有兩個實體 - Asset
和Attachment
,雙向映射OneToOne
。每個Asset
可以0..1
Asset
:
Asset
class Asset
{
/**
* @var string @ORM\Column(name="asset_uuid", type="string", length=36, nullable=false)
* @ORM\Id
*/
private $uuid;
/**
* @var \MyLib\Model\Entity\Attachment
* @ORM\OneToOne(targetEntity="MyLib\Model\Entity\Attachment", mappedBy="asset", cascade={"remove", "persist"}, orphanRemoval=true)
**/
private $attachment;
...
}
Attachment
class Attachment
{
/**
* @var string @ORM\Column(name="attachment_uuid", type="string", length=36, nullable=false)
* @ORM\Id
*/
private $uuid;
/**
* @var \MyLib\Model\Entity\Asset
* @ORM\OneToOne(targetEntity="\MyLib\Model\Entity\Asset", inversedBy="attachment", cascade={"persist"})
* @ORM\JoinColumn(name="attachment_linkassetuuid", referencedColumnName="asset_uuid")
*/
private $asset;
...
}
現在我想find()
的Attachment
通過Asset.uuid
:
class AssetService ...
{
...
private function finAttachmentByAssetUuid($assetUuid)
{
$entityManager = $this->getEntityManager();
$attachmentRepository = $entityManager->getRepository('MyLib\Model\Entity\Attachment');
$attachment = $attachmentRepository->findBy([
'attachment_linkassetuuid' => $assetUuid
]);
return $attachment;
}
...
}
但它沒有也不能工作,因爲Doctrine期望實體屬性名稱而不是表格列名稱。那麼,但是這裏的實體對外鍵列沒有特別的要求。
如何在這種情況下使用Doctrine\Common\Persistence#findBy(...)
?或者,如果不可能:如何通過Asset.uuid
在這個具體情況下檢索Attachment
另一種方式?