Commit f9938ea6 authored by romanb's avatar romanb

custom/magic accessors + test

parent 2429605f
......@@ -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()
......@@ -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);
......
......@@ -19,6 +19,8 @@
* <http://www.phpdoctrine.org>.
*/
#namespace Doctrine::ORM;
/**
* The EntityManager is a central access point to ORM functionality.
*
......
<?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 . "?");
}
}
\ No newline at end of file
......@@ -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
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment