Entity not Found Exception on Doctrine2

Scenario: I have two entities. Product & ProductGroup. Product has many to one relation to ProductGroup. I soft delete the ProductGroup. Then I try to call getProductGroup on a product which has a product group which was soft deleted.

Problem: instead of expected null result I get an "Message: Entity was not found." exception message.

$productGroupName = $product->getProductGroup() !== null ? $product->getProductGroup()->getName() : '';

1 answer

  • answered 2018-01-17 06:31 Shahzaib Hayat Khan

    SOLVED IT It is because of proxies Doctrine2 was generating, not the case with soft-deletable though i'm using that. But in entities where this soft-deleteable wasn't implemented i was having the same issue. so what fixed for me is Adding fetch="EAGER" on Entities Annotations where relation was defined. like

    /** * @var \ReisesparerAPIs\Entity\Vouchers * * @ORM\OneToOne(targetEntity="ReisesparerAPIs\Entity\Vouchers", inversedBy="couponOrder", fetch="EAGER") * @ORM\JoinColumns({ * @ORM\JoinColumn(name="voucher_id", referencedColumnName="id") * }) */ private $voucher;

    Reference Link