Skip to content

Commit

Permalink
Marking properties as unset during unsetProperty, fixes #12
Browse files Browse the repository at this point in the history
  • Loading branch information
jeremyharris committed Feb 23, 2017
1 parent 2ac2b26 commit 15a9d58
Showing 1 changed file with 19 additions and 5 deletions.
24 changes: 19 additions & 5 deletions src/ORM/LazyLoadEntityTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
*
Expand All @@ -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];
}
Expand All @@ -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])) {
Expand Down

0 comments on commit 15a9d58

Please sign in to comment.