Skip to content

Commit

Permalink
Changing the constructor for Entity, now it is possible to create an
Browse files Browse the repository at this point in the history
instance that is automatically marked as persisted and clean on
construction.

The amout of things that needed to be done after creating an
instance was increasing and where things that were a natural fit
for a constructor
  • Loading branch information
lorenzo committed Oct 26, 2013
1 parent 22c7523 commit 264c907
Show file tree
Hide file tree
Showing 3 changed files with 69 additions and 9 deletions.
25 changes: 21 additions & 4 deletions Cake/ORM/Entity.php
Original file line number Diff line number Diff line change
Expand Up @@ -72,12 +72,29 @@ class Entity implements \ArrayAccess, \JsonSerializable {
* ``$entity = new Entity(['id' => 1, 'name' => 'Andrew'])``
*
* @param array $properties hash of properties to set in this entity
* @param boolean $useSetters whether use internal setters for properties or not
* @return void
* @param array $options list of options to use when creating this entity
* the following list of options can be used:
*
* - useSetters: whether use internal setters for properties or not
* - markClean: whether to mark all properties as clean after setting them
* - markNew: whether this instance has not yet been persisted
*/
public function __construct(array $properties = [], $useSetters = true) {
public function __construct(array $properties = [], array $options = []) {
$options += [
'useSetters' => true,
'markClean' => false,
'markNew' => null
];
$this->_className = get_class($this);
$this->set($properties, $useSetters);
$this->set($properties, $options['useSetters']);

if ($options['markClean']) {
$this->clean();
}

if ($options['markNew'] !== null) {
$this->isNew($options['markNew']);
}
}

/**
Expand Down
6 changes: 3 additions & 3 deletions Cake/ORM/ResultSet.php
Original file line number Diff line number Diff line change
Expand Up @@ -265,6 +265,7 @@ protected function _groupResult($row) {
$results[$defaultAlias]
);

$options = ['useSetters' => false, 'markClean' => true];
foreach (array_reverse($this->_associationMap) as $alias => $assoc) {
if (!isset($results[$alias])) {
continue;
Expand All @@ -273,7 +274,7 @@ protected function _groupResult($row) {
$results[$alias] = $this->_castValues($instance->target(), $results[$alias]);

if ($this->_hydrate && $assoc['canBeJoined']) {
$entity = new $assoc['entityClass']($results[$alias], false);
$entity = new $assoc['entityClass']($results[$alias], $options);
$entity->clean();
$results[$alias] = $entity;
}
Expand All @@ -282,8 +283,7 @@ protected function _groupResult($row) {

$results = $results[$defaultAlias];
if ($this->_hydrate && !($results instanceof Entity)) {
$results = new $this->_entityClass($results, false);
$results->clean();
$results = new $this->_entityClass($results, $options);
}
return $results;
}
Expand Down
47 changes: 45 additions & 2 deletions Cake/Test/TestCase/ORM/EntityTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ public function testSetOneParamNoSetters() {
*
* @return void
*/
public function testSetMultiplePropertiesNOSetters() {
public function testSetMultiplePropertiesNoSetters() {
$entity = new Entity;
$entity->set(['foo' => 'bar', 'id' => 1]);
$this->assertEquals('bar', $entity->foo);
Expand Down Expand Up @@ -138,7 +138,7 @@ public function testConstructor() {
->with(['foo' => 'bar'], false);

$entity->__construct(['a' => 'b', 'c' => 'd']);
$entity->__construct(['foo' => 'bar'], false);
$entity->__construct(['foo' => 'bar'], ['useSetters' => false]);
}

/**
Expand Down Expand Up @@ -530,4 +530,47 @@ public function testIsNew() {
$entity->isNew(false);
$this->assertFalse($entity->isNew());
}

/**
* Tests the constructor when passing the markClean option
*
* @return void
*/
public function testConstructorWithClean() {
$entity = $this->getMockBuilder('\Cake\ORM\Entity')
->setMethods(['clean'])
->disableOriginalConstructor()
->getMock();
$entity->expects($this->never())->method('clean');
$entity->__construct(['a' => 'b', 'c' => 'd']);

$entity = $this->getMockBuilder('\Cake\ORM\Entity')
->setMethods(['clean'])
->disableOriginalConstructor()
->getMock();
$entity->expects($this->once())->method('clean');
$entity->__construct(['a' => 'b', 'c' => 'd'], ['markClean' => true]);
}

/**
* Tests the constructor when passing the markClean option
*
* @return void
*/
public function testConstructorWithMarkNew() {
$entity = $this->getMockBuilder('\Cake\ORM\Entity')
->setMethods(['isNew'])
->disableOriginalConstructor()
->getMock();
$entity->expects($this->never())->method('clean');
$entity->__construct(['a' => 'b', 'c' => 'd']);

$entity = $this->getMockBuilder('\Cake\ORM\Entity')
->setMethods(['isNew'])
->disableOriginalConstructor()
->getMock();
$entity->expects($this->once())->method('isNew');
$entity->__construct(['a' => 'b', 'c' => 'd'], ['markNew' => true]);
}

}

0 comments on commit 264c907

Please sign in to comment.