diff --git a/src/ORM/LazyLoadEntityTrait.php b/src/ORM/LazyLoadEntityTrait.php index 619ee35..fddfd87 100644 --- a/src/ORM/LazyLoadEntityTrait.php +++ b/src/ORM/LazyLoadEntityTrait.php @@ -16,11 +16,11 @@ trait LazyLoadEntityTrait { /** - * Array of properties that have been lazily loaded + * Array of properties that have been unset * * @var array */ - protected $_lazyLoaded = []; + protected $_unsetProperties = []; /** * Overrides get() to check for associated data to lazy load, if that @@ -84,6 +84,21 @@ protected function _parentHas($property) return parent::has($property); } + /** + * Unsets a property, marking it as not to be lazily loaded in the future + * + * @param array|string $property Property + * @return $this + */ + public function unsetProperty($property) + { + $property = (array)$property; + foreach ($property as $prop) { + $this->_unsetProperties[] = $prop; + } + return parent::unsetProperty($property); + } + /** * Lazy loads association data onto the entity * @@ -92,8 +107,8 @@ protected function _parentHas($property) */ protected function _lazyLoad($property) { - // check if the property has been lazy loaded already (even if the result was null) - if (array_search($property, $this->_lazyLoaded) !== false) { + // check if the property has been unset at some point + if (array_search($property, $this->_unsetProperties) !== false) { if (isset($this->_properties[$property])) { return $this->_properties[$property]; } @@ -120,7 +135,6 @@ protected function _lazyLoad($property) } $repository->loadInto($this, [$association->name()]); - $this->_lazyLoaded[] = $property; // check if the association didn't exist and therefore didn't load if (!isset($this->_properties[$property])) {