Doctrine2检查相关实体是否存在

I'm really tired to figuring out how I can check in Doctrine 2 if related entity record exists in DB. Help me please.

For example I have two entities. One is the order status of certain delivery company. Another one is order.

Order.php

/**
 * @ORM\OneToOne(targetEntity="Application\DeliveryBundle\Entity\DpdOrderStatus", mappedBy="order")
 * @var DpdOrderStatus
 */
$dpdOrderStatus;

DpdOrderStatus.php

/**
 * @ORM\Id
 * @ORM\OneToOne(targetEntity="\Application\FrontendBundle\Entity\Order", inversedBy="dpdOrderStatus")
 * @ORM\JoinColumn(onDelete="CASCADE")
 * @var Order
 */
$order;

Order entity sometimes doesn't have status and I need to check if it has.

AFAIK if I will try to use is_null($order->getDpdOrderStatus()) it will always be false because Doctrine always create Proxy objects for its entities if EAGER mode is not specified.

So what is the most proper way to check if my status entity exists in database?

Add a method that checks if the order has an order status:

Order.php

public function hasOrderStatus(){
 return ! is_null($this->dpdOrderStatus);
}

More information: Techniques to check if relationship exists in Doctrine2

This worked for me.

public function hasOrderStatus() {
    return !is_null($this->dpdOrderStatus) && (bool) $this->dpdOrderStatus->getId();
}