Browse Source

Fixing some edge cases in Entity::set()

Jose Lorenzo Rodriguez 12 years ago
parent
commit
3db559edff
3 changed files with 11 additions and 7 deletions
  1. 3 2
      src/Datasource/EntityTrait.php
  2. 7 4
      src/ORM/Entity.php
  3. 1 1
      tests/TestCase/ORM/EntityTest.php

+ 3 - 2
src/Datasource/EntityTrait.php

@@ -199,7 +199,8 @@ trait EntityTrait {
  * @return \Cake\Datasource\EntityInterface this object
  */
 	public function set($property, $value = null, $options = []) {
-		if (is_string($property)) {
+		$isString = is_string($property);
+		if ($isString && $property !== '') {
 			$guard = false;
 			$property = [$property => $value];
 		} else {
@@ -207,10 +208,10 @@ trait EntityTrait {
 			$options = (array)$value;
 		}
 
-		$options += ['setter' => true, 'guard' => $guard];
 		if (!is_array($property)) {
 			throw new \InvalidArgumentException('Cannot set an empty property');
 		}
+		$options += ['setter' => true, 'guard' => $guard];
 
 		foreach ($property as $p => $value) {
 			if ($options['guard'] === true && !$this->accessible($p)) {

+ 7 - 4
src/ORM/Entity.php

@@ -54,10 +54,13 @@ class Entity implements EntityInterface {
 			'source' => null
 		];
 		$this->_className = get_class($this);
-		$this->set($properties, [
-			'setter' => $options['useSetters'],
-			'guard' => $options['guard']
-		]);
+
+		if (!empty($properties)) {
+			$this->set($properties, [
+				'setter' => $options['useSetters'],
+				'guard' => $options['guard']
+			]);
+		}
 
 		if ($options['markClean']) {
 			$this->clean();

+ 1 - 1
tests/TestCase/ORM/EntityTest.php

@@ -1066,7 +1066,7 @@ class EntityTest extends TestCase {
  */
 	public function testSetEmptyPropertyName($property) {
 		$entity = new Entity();
-		$entity->set($property, 'foo');
+		$entity->set($property, 'bar');
 	}
 
 }