custom/magic accessors + test
This commit is contained in:
parent
2429605fbd
commit
f9938ea6fd
@ -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;
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -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()
|
||||
@ -1071,6 +1094,10 @@ abstract class Doctrine_Entity extends Doctrine_Access implements Countable, Ite
|
||||
*/
|
||||
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);
|
||||
|
@ -19,6 +19,8 @@
|
||||
* <http://www.phpdoctrine.org>.
|
||||
*/
|
||||
|
||||
#namespace Doctrine::ORM;
|
||||
|
||||
/**
|
||||
* The EntityManager is a central access point to ORM functionality.
|
||||
*
|
||||
|
57
tests/Orm/Entity/AccessorTestCase.php
Normal file
57
tests/Orm/Entity/AccessorTestCase.php
Normal file
@ -0,0 +1,57 @@
|
||||
<?php
|
||||
require_once 'lib/DoctrineTestInit.php';
|
||||
|
||||
class Orm_Entity_AccessorTestCase extends Doctrine_OrmTestCase
|
||||
{
|
||||
public function testGetterSetterOverride()
|
||||
{
|
||||
$em = new Doctrine_EntityManager(new Doctrine_Connection_Mock());
|
||||
|
||||
$entity1 = new CustomAccessorMutatorTestEntity();
|
||||
$entity1->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 . "?");
|
||||
}
|
||||
}
|
@ -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 . "?");
|
||||
}
|
||||
|
||||
}
|
Loading…
Reference in New Issue
Block a user