Note that there are some explanatory texts on larger screens.

plurals
  1. PODoctrine2 EntityNotFoundException when getting an entity from relation
    primarykey
    data
    text
    <p>In a Symfony2 project using Doctrine2. I have a Lead entity related with Promotion entity 1-N. A Lead con have a related Promotion or not.</p> <pre><code>//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-&gt;promotionCode = $promotionCode; } public function getPromotionCode() { return $this-&gt;promotionCode; } public function setPromotion($promotion) { $this-&gt;promotion = $promotion; } public function getPromotion() { return $this-&gt;promotion; } </code></pre> <p>When I want to obtain the related promotion (if any) y do</p> <pre><code>$lead = $em-&gt;getRepository('BuvMarketplaceBundle:Lead')-&gt;find($id); $promotion = $lead-&gt;getPromotion(); </code></pre> <p>If the lead has a promotion this is OK. But if not this code returns a Promotion "entity", but when I try to use if I get an EntityNotFoundException.</p> <p>So I have to test if the related promotion exists like this: </p> <pre><code>if (is_object($promotion) &amp;&amp; method_exists($promotion, 'getDiscount')) { try { $promotion-&gt;getDiscount(); } catch(EntityNotFoundException $e) { $promotion = null; } } else { $promotion = null; } </code></pre> <p>I know that I can use a findBy in the Promotion Repository, and may be another methods to check this.</p> <p><strong>But the question is if this is a bug or a feature in Doctrine2</strong>, so I'm getting a "false entity" when I think it may be a null.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload