AutoincrementedIdTrait.php
3 years ago
CreatedAtTrait.php
3 years ago
DeletedAtTrait.php
3 years ago
SafeToOneAssociationLoadTrait.php
3 years ago
UpdatedAtTrait.php
3 years ago
index.php
3 years ago
SafeToOneAssociationLoadTrait.php
34 lines
| 1 | <?php // phpcs:ignore SlevomatCodingStandard.TypeHints.DeclareStrictTypes.DeclareStrictTypesMissing |
| 2 | |
| 3 | namespace MailPoet\Doctrine\EntityTraits; |
| 4 | |
| 5 | if (!defined('ABSPATH')) exit; |
| 6 | |
| 7 | |
| 8 | use MailPoetVendor\Doctrine\ORM\EntityNotFoundException; |
| 9 | use MailPoetVendor\Doctrine\ORM\Proxy\Proxy; |
| 10 | |
| 11 | trait SafeToOneAssociationLoadTrait { |
| 12 | private function safelyLoadToOneAssociation(string $propertyName, $emptyValue = null) { |
| 13 | if (!property_exists($this, $propertyName)) { |
| 14 | throw new \InvalidArgumentException("Property '$propertyName' does not exist on class '" . get_class($this) . "'"); |
| 15 | } |
| 16 | |
| 17 | if (!$this->$propertyName instanceof Proxy) { |
| 18 | return; |
| 19 | } |
| 20 | |
| 21 | if ($this->$propertyName->__isInitialized()) { |
| 22 | return; |
| 23 | } |
| 24 | |
| 25 | // if a proxy exists (= we have related entity ID), try to load it |
| 26 | // to see if it is a valid ID referencing an existing entity |
| 27 | try { |
| 28 | $this->$propertyName->__load(); |
| 29 | } catch (EntityNotFoundException $e) { |
| 30 | $this->$propertyName = $emptyValue; |
| 31 | } |
| 32 | } |
| 33 | } |
| 34 |