diff --git a/lib/Doctrine/ClassMetadata.php b/lib/Doctrine/ClassMetadata.php index 80973138f..f66a7e15f 100644 --- a/lib/Doctrine/ClassMetadata.php +++ b/lib/Doctrine/ClassMetadata.php @@ -951,7 +951,7 @@ class Doctrine_ClassMetadata extends Doctrine_Configurable implements Serializab { $columnName = $this->getColumnName($fieldName); return isset($this->_mappedColumns[$columnName]['mutator']) ? - $this->_mappedColumns[$columnName]['mutator'] : null; + $this->_mappedColumns[$columnName]['mutator'] : null; } /** diff --git a/lib/Doctrine/Entity.php b/lib/Doctrine/Entity.php index 6e25f4cde..0218f7a1a 100644 --- a/lib/Doctrine/Entity.php +++ b/lib/Doctrine/Entity.php @@ -248,7 +248,7 @@ abstract class Doctrine_Entity extends Doctrine_Access implements Countable, Ite } //-- - self::$_useAutoAccessorOverride = false; // @todo read from attribute the first time + self::$_useAutoAccessorOverride = true; // @todo read from attribute the first time } /** @@ -994,6 +994,9 @@ abstract class Doctrine_Entity extends Doctrine_Access implements Countable, Ite */ public function get($fieldName, $load = false) { + if ($getter = $this->_getCustomAccessor($fieldName)) { + return $this->$getter(); + } $this->_invokeCustomAccessor($fieldName); // Use built-in accessor functionality @@ -1027,7 +1030,29 @@ abstract class Doctrine_Entity extends Doctrine_Access implements Countable, Ite } } - private function _invokeCustomAccessor($fieldName) + private function _getCustomMutator($fieldName) + { + if ( ! isset(self::$_mutatorCache[$this->_entityName][$fieldName])) { + if (self::$_useAutoAccessorOverride) { + $setterMethod = 'set' . Doctrine::classify($fieldName); + if (method_exists($this, $setterMethod)) { + self::$_mutatorCache[$this->_entityName][$fieldName] = $setterMethod; + } else { + self::$_mutatorCache[$this->_entityName][$fieldName] = false; + } + } + + if ($setter = $this->_class->getCustomMutator($fieldName)) { + self::$_mutatorCache[$this->_entityName][$fieldName] = $setter; + } else if ( ! isset(self::$_mutatorCache[$this->_entityName][$fieldName])) { + self::$_mutatorCache[$this->_entityName][$fieldName] = false; + } + } + + return self::$_mutatorCache[$this->_entityName][$fieldName]; + } + + private function _getCustomAccessor($fieldName) { if ( ! isset(self::$_accessorCache[$this->_entityName][$fieldName])) { if (self::$_useAutoAccessorOverride) { @@ -1044,10 +1069,8 @@ abstract class Doctrine_Entity extends Doctrine_Access implements Countable, Ite self::$_accessorCache[$this->_entityName][$fieldName] = false; } } - // invoke custom accessor, if it exists. - if ($getter = self::$_accessorCache[$this->_entityName][$fieldName]) { - return $this->$getter(); - } + + return self::$_accessorCache[$this->_entityName][$fieldName]; } public function getClassName() @@ -1070,7 +1093,11 @@ abstract class Doctrine_Entity extends Doctrine_Access implements Countable, Ite * @return Doctrine_Entity */ public function set($fieldName, $value, $load = false) - { + { + if ($setter = $this->_getCustomMutator($fieldName)) { + return $this->$setter($value); + } + if ($this->_class->hasField($fieldName)) { if ($value instanceof Doctrine_Entity) { $type = $this->_class->getTypeOf($fieldName); diff --git a/lib/Doctrine/EntityManager.php b/lib/Doctrine/EntityManager.php index 823175d35..e846a3f7e 100644 --- a/lib/Doctrine/EntityManager.php +++ b/lib/Doctrine/EntityManager.php @@ -19,6 +19,8 @@ * . */ +#namespace Doctrine::ORM; + /** * The EntityManager is a central access point to ORM functionality. * diff --git a/tests/Orm/Entity/AccessorTestCase.php b/tests/Orm/Entity/AccessorTestCase.php new file mode 100644 index 000000000..d2d076fda --- /dev/null +++ b/tests/Orm/Entity/AccessorTestCase.php @@ -0,0 +1,57 @@ +username = 'romanb'; + $this->assertEquals('romanb?!', $entity1->username); + + $entity2 = new MagicAccessorMutatorTestEntity(); + $entity2->username = 'romanb'; + $this->assertEquals('romanb?!', $entity1->username); + + } +} + +class CustomAccessorMutatorTestEntity extends Doctrine_Entity +{ + public static function initMetadata($class) + { + $class->mapColumn('username', 'string', 50, array( + 'accessor' => 'getUsernameCustom', + 'mutator' => 'setUsernameCustom')); + } + + public function getUsernameCustom() + { + return $this->rawGetField('username') . "!"; + } + + public function setUsernameCustom($username) + { + $this->rawSetField('username', $username . "?"); + } +} + +class MagicAccessorMutatorTestEntity extends Doctrine_Entity +{ + public static function initMetadata($class) + { + $class->mapColumn('username', 'string', 50, array()); + } + + public function getUsername() + { + return $this->rawGetField('username') . "!"; + } + + public function setUsername($username) + { + $this->rawSetField('username', $username . "?"); + } +} \ No newline at end of file diff --git a/tests/models/forum/ForumUser.php b/tests/models/forum/ForumUser.php index 216e9023c..125eddfbb 100644 --- a/tests/models/forum/ForumUser.php +++ b/tests/models/forum/ForumUser.php @@ -20,7 +20,9 @@ class ForumUser extends Doctrine_Entity $class->mapColumn('id', 'integer', 4, array( 'primary' => true, 'autoincrement' => true)); - $class->mapColumn('username', 'string', 50, array('accessor' => 'getUsernameCustom')); + $class->mapColumn('username', 'string', 50, array( + 'accessor' => 'getUsernameCustom', + 'mutator' => 'setUsernameCustom')); } @@ -29,4 +31,9 @@ class ForumUser extends Doctrine_Entity return $this->rawGetField('username') . "!"; } + public function setUsernameCustom($username) + { + $this->rawSetField('username', $username . "?"); + } + } \ No newline at end of file