Skip to content

Commit

Permalink
Remove accessor cache.
Browse files Browse the repository at this point in the history
While this feature was intended to help with performance and make
implementing lazy loaded associations easier, it causes a number of
annoying subtle bugs that we'd rather not have.

Refs #6574
  • Loading branch information
markstory committed May 19, 2015
1 parent 2a7e9a2 commit 28af8cd
Show file tree
Hide file tree
Showing 2 changed files with 4 additions and 38 deletions.
14 changes: 0 additions & 14 deletions src/Datasource/EntityTrait.php
Expand Up @@ -115,13 +115,6 @@ trait EntityTrait
*/
protected $_registryAlias;

/**
* Holds a list of properties that were mutated using the get accessor
*
* @var array
*/
protected $_mutated = [];

/**
* Magic getter to access properties that have been set in this entity
*
Expand Down Expand Up @@ -263,7 +256,6 @@ public function set($property, $value = null, array $options = [])
$this->_properties[$p] = $value;
}

$this->_mutated = [];
return $this;
}

Expand All @@ -280,10 +272,6 @@ public function &get($property)
throw new InvalidArgumentException('Cannot get an empty property');
}

if (array_key_exists($property, $this->_mutated)) {
return $this->_mutated[$property];
}

$value = null;
$method = '_get' . Inflector::camelize($property);

Expand All @@ -293,7 +281,6 @@ public function &get($property)

if ($this->_methodExists($method)) {
$result = $this->{$method}($value);
$this->_mutated[$property] = $result;
return $result;
}
return $value;
Expand Down Expand Up @@ -367,7 +354,6 @@ public function unsetProperty($property)
unset($this->_dirty[$p]);
}

$this->_mutated = [];
return $this;
}

Expand Down
28 changes: 4 additions & 24 deletions tests/TestCase/ORM/EntityTest.php
Expand Up @@ -227,7 +227,8 @@ public function testGetNoGetters()
public function testGetCustomGetters()
{
$entity = $this->getMock('\Cake\ORM\Entity', ['_getName']);
$entity->expects($this->once())->method('_getName')
$entity->expects($this->any())
->method('_getName')
->with('Jones')
->will($this->returnCallback(function ($name) {
return 'Dr. ' . $name;
Expand All @@ -245,7 +246,8 @@ public function testGetCustomGetters()
public function testGetCustomGettersAfterSet()
{
$entity = $this->getMock('\Cake\ORM\Entity', ['_getName']);
$entity->expects($this->exactly(2))->method('_getName')
$entity->expects($this->any())
->method('_getName')
->will($this->returnCallback(function ($name) {
return 'Dr. ' . $name;
}));
Expand Down Expand Up @@ -277,28 +279,6 @@ public function testGetCacheClearedByUnset()
$this->assertEquals('Dr. ', $entity->get('name'));
}

/**
* Tests that the get cache is cleared by setting any property.
* This is because virtual properties can often rely on other
* properties in the entity.
*
* @return void
*/
public function testGetCacheClearedBySet()
{
$entity = $this->getMock('\Cake\ORM\Entity', ['_getName']);
$entity->last_name = 'Smith';
$entity->name = 'John';
$entity->expects($this->any())->method('_getName')
->will($this->returnCallback(function ($name) use ($entity) {
return 'Dr. ' . $name . ' ' . $entity->last_name;
}));
$this->assertEquals('Dr. John Smith', $entity->get('name'));

$entity->last_name = 'Jones';
$this->assertEquals('Dr. John Jones', $entity->get('name'));
}

/**
* Test magic property setting with no custom setter
*
Expand Down

0 comments on commit 28af8cd

Please sign in to comment.