Symfony 从关系获取实体时Doctrine2 EntityNotFoundException
在使用Doctrine2的Symfony2项目中。我有一个与促销实体1-N相关的牵头实体。牵头人是否有相关促销Symfony 从关系获取实体时Doctrine2 EntityNotFoundException,symfony,doctrine-orm,Symfony,Doctrine Orm,在使用Doctrine2的Symfony2项目中。我有一个与促销实体1-N相关的牵头实体。牵头人是否有相关促销 //Lead.php ... /** * @var string $promotionCode * @ORM\Column(name="promotion_code", type="string", length=16) */ private $promotionCode; /** * @var Promotion $promotion * @ORM\ManyToOne
//Lead.php
...
/**
* @var string $promotionCode
* @ORM\Column(name="promotion_code", type="string", length=16)
*/
private $promotionCode;
/**
* @var Promotion $promotion
* @ORM\ManyToOne(targetEntity="Promotion")
* @ORM\JoinColumn(name="promotion_code", referencedColumnName="id")
*/
private $promotion;
...
public function setPromotionCode($promotionCode) {
$this->promotionCode = $promotionCode;
}
public function getPromotionCode() {
return $this->promotionCode;
}
public function setPromotion($promotion) {
$this->promotion = $promotion;
}
public function getPromotion() {
return $this->promotion;
}
当我想获得相关的晋升(如果有的话)时,我会做什么
如果潜在客户有促销活动,这是可以的。但如果不是,则此代码返回一个提升“实体”,但当我尝试使用时,如果我得到EntityNotFoundException
因此,我必须测试相关促销是否存在如下情况:
if (is_object($promotion) && method_exists($promotion, 'getDiscount')) {
try {
$promotion->getDiscount();
} catch(EntityNotFoundException $e) {
$promotion = null;
}
} else {
$promotion = null;
}
我知道我可以使用升级存储库中的findBy,可能还有其他方法可以检查这一点
但问题是这是Doctrine2中的一个bug还是一个功能,所以当我认为它可能是空的时候,我得到了一个“假实体”。显示Lead::getPromotion()的代码;如果没有升级,它实际上应该返回null。您还有其他问题。只是添加了更多关于关系中使用的字段的代码。如您所见,getter中没有任何其他代码。如何检索Lead实体?我使用存储库中的find()方法。
if (is_object($promotion) && method_exists($promotion, 'getDiscount')) {
try {
$promotion->getDiscount();
} catch(EntityNotFoundException $e) {
$promotion = null;
}
} else {
$promotion = null;
}