【发布时间】:2015-09-25 15:21:19
【问题描述】:
有两个实体——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;
...
}
现在我想通过Asset.uuidfind() 和Attachment:
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?
【问题讨论】:
标签: orm doctrine-orm foreign-keys one-to-one bidirectional