Sin descripción

SafeToOneAssociationLoadTrait.php 917B

12345678910111213141516171819202122232425262728293031323334
  1. <?php
  2. namespace MailPoet\Doctrine\EntityTraits;
  3. if (!defined('ABSPATH')) exit;
  4. use MailPoetVendor\Doctrine\ORM\EntityNotFoundException;
  5. use MailPoetVendor\Doctrine\ORM\Proxy\Proxy;
  6. trait SafeToOneAssociationLoadTrait {
  7. private function safelyLoadToOneAssociation(string $propertyName, $emptyValue = null) {
  8. if (!property_exists($this, $propertyName)) {
  9. throw new \InvalidArgumentException("Property '$propertyName' does not exist on class '" . get_class($this) . "'");
  10. }
  11. if (!$this->$propertyName instanceof Proxy) {
  12. return;
  13. }
  14. if ($this->$propertyName->__isInitialized()) {
  15. return;
  16. }
  17. // if a proxy exists (= we have related entity ID), try to load it
  18. // to see if it is a valid ID referencing an existing entity
  19. try {
  20. $this->$propertyName->__load();
  21. } catch (EntityNotFoundException $e) {
  22. $this->$propertyName = $emptyValue;
  23. }
  24. }
  25. }