Commit 35fa81db authored by romanb's avatar romanb

Removed static EntityManager lookup from productions. Entity refactorings.

parent 7206b1dd
...@@ -66,7 +66,7 @@ abstract class Doctrine_Entity extends Doctrine_Access implements Serializable ...@@ -66,7 +66,7 @@ abstract class Doctrine_Entity extends Doctrine_Access implements Serializable
* PROXY STATE * PROXY STATE
* An Entity is in proxy state when its properties are not fully loaded. * An Entity is in proxy state when its properties are not fully loaded.
*/ */
const STATE_PROXY = 4; //const STATE_PROXY = 4;
/** /**
* NEW TCLEAN * NEW TCLEAN
...@@ -963,9 +963,8 @@ abstract class Doctrine_Entity extends Doctrine_Access implements Serializable ...@@ -963,9 +963,8 @@ abstract class Doctrine_Entity extends Doctrine_Access implements Serializable
return $this->$setter($value); return $this->$setter($value);
} }
$class = $this->_class; if ($this->_class->hasField($fieldName)) {
if ($class->hasField($fieldName)) { /*if ($value instanceof Doctrine_Entity) {
if ($value instanceof Doctrine_Entity) {
$type = $class->getTypeOf($fieldName); $type = $class->getTypeOf($fieldName);
// FIXME: composite key support // FIXME: composite key support
$ids = $value->identifier(); $ids = $value->identifier();
...@@ -973,17 +972,15 @@ abstract class Doctrine_Entity extends Doctrine_Access implements Serializable ...@@ -973,17 +972,15 @@ abstract class Doctrine_Entity extends Doctrine_Access implements Serializable
if ($id !== null && $type !== 'object') { if ($id !== null && $type !== 'object') {
$value = $id; $value = $id;
} }
} }*/
$old = isset($this->_data[$fieldName]) ? $this->_data[$fieldName] : null; $old = isset($this->_data[$fieldName]) ? $this->_data[$fieldName] : null;
if ($old !== $value) { if ($old != $value) {
$this->_data[$fieldName] = $value; $this->_data[$fieldName] = $value;
$this->_modified[] = $fieldName; $this->_modified[] = $fieldName;
/* We can't do this currently because there are tests that change if ($this->isTransient() && $this->_class->isIdentifier($fieldName)) {
* the primary key of already persisted entities (ugh). */
if ($this->isTransient() && $class->isIdentifier($fieldName)) {
$this->_id[$fieldName] = $value; $this->_id[$fieldName] = $value;
} }
...@@ -996,7 +993,7 @@ abstract class Doctrine_Entity extends Doctrine_Access implements Serializable ...@@ -996,7 +993,7 @@ abstract class Doctrine_Entity extends Doctrine_Access implements Serializable
break; break;
} }
} }
} else if ($class->hasRelation($fieldName)) { } else if ($this->_class->hasRelation($fieldName)) {
$this->_rawSetReference($fieldName, $value); $this->_rawSetReference($fieldName, $value);
} else { } else {
throw Doctrine_Entity_Exception::invalidField($fieldName); throw Doctrine_Entity_Exception::invalidField($fieldName);
...@@ -1141,12 +1138,12 @@ abstract class Doctrine_Entity extends Doctrine_Access implements Serializable ...@@ -1141,12 +1138,12 @@ abstract class Doctrine_Entity extends Doctrine_Access implements Serializable
$dataSet[$field] = $this->_class->enumIndex($field, $this->_data[$field]); $dataSet[$field] = $this->_class->enumIndex($field, $this->_data[$field]);
break; break;
default: default:
if ($this->_data[$field] instanceof Doctrine_Entity) { /*if ($this->_data[$field] instanceof Doctrine_Entity) {
// FIXME: composite key support // FIXME: composite key support
$ids = $this->_data[$field]->identifier(); $ids = $this->_data[$field]->identifier();
$id = count($ids) > 0 ? array_pop($ids) : null; $id = count($ids) > 0 ? array_pop($ids) : null;
$this->_data[$field] = $id; $this->_data[$field] = $id;
} }*/
/** TODO: /** TODO:
if ($this->_data[$v] === null) { if ($this->_data[$v] === null) {
throw new Doctrine_Record_Exception('Unexpected null value.'); throw new Doctrine_Record_Exception('Unexpected null value.');
...@@ -1159,11 +1156,10 @@ abstract class Doctrine_Entity extends Doctrine_Access implements Serializable ...@@ -1159,11 +1156,10 @@ abstract class Doctrine_Entity extends Doctrine_Access implements Serializable
// @todo cleanup // @todo cleanup
// populates the discriminator field in Single & Class Table Inheritance // populates the discriminator field in Single & Class Table Inheritance
$class = $this->_class; if ($this->_class->getInheritanceType() == Doctrine::INHERITANCE_TYPE_JOINED ||
if ($class->getInheritanceType() == Doctrine::INHERITANCE_TYPE_JOINED || $this->_class->getInheritanceType() == Doctrine::INHERITANCE_TYPE_SINGLE_TABLE) {
$class->getInheritanceType() == Doctrine::INHERITANCE_TYPE_SINGLE_TABLE) { $discCol = $this->_class->getInheritanceOption('discriminatorColumn');
$discCol = $class->getInheritanceOption('discriminatorColumn'); $discMap = $this->_class->getInheritanceOption('discriminatorMap');
$discMap = $class->getInheritanceOption('discriminatorMap');
$old = $this->get($discCol, false); $old = $this->get($discCol, false);
$discValue = array_search($this->_entityName, $discMap); $discValue = array_search($this->_entityName, $discMap);
if ((string) $old !== (string) $discValue || $old === null) { if ((string) $old !== (string) $discValue || $old === null) {
......
...@@ -59,7 +59,6 @@ class Doctrine_EntityManager ...@@ -59,7 +59,6 @@ class Doctrine_EntityManager
*/ */
private $_config; private $_config;
/** /**
* The database connection used by the EntityManager. * The database connection used by the EntityManager.
* *
...@@ -409,7 +408,24 @@ class Doctrine_EntityManager ...@@ -409,7 +408,24 @@ class Doctrine_EntityManager
*/ */
public function save(Doctrine_Entity $entity) public function save(Doctrine_Entity $entity)
{ {
$state = $entity->_state();
if ($state == Doctrine_Entity::STATE_CLEAN || $state == Doctrine_Entity::STATE_LOCKED) {
return;
}
//... //...
//$this->_unitOfWork->
switch ($entity->_state()) {
case Doctrine_Entity::STATE_CLEAN:
//nothing to do
break;
case Doctrine_Entity::STATE_DIRTY:
$this->_unitOfWork->registerDirty($entity);
break;
case Doctrine_Entity::STATE_TCLEAN:
case Doctrine_Entity::STATE_TDIRTY:
//...
}
} }
/** /**
......
...@@ -248,14 +248,13 @@ abstract class Doctrine_EntityPersister_Abstract ...@@ -248,14 +248,13 @@ abstract class Doctrine_EntityPersister_Abstract
} }
/** /**
* Saves an entity and all it's related entities. * Saves an entity.
* *
* @param Doctrine_Entity $record The entity to save. * @param Doctrine_Entity $record The entity to save.
* @param Doctrine_Connection $conn The connection to use. Will default to the mapper's * @param Doctrine_Connection $conn The connection to use. Will default to the mapper's
* connection. * connection.
* @throws Doctrine_Mapper_Exception If the mapper is unable to save the given entity.
*/ */
public function save(Doctrine_Entity $record, Doctrine_Connection $conn = null) public function save(Doctrine_Entity $record)
{ {
if ( ! ($record instanceof $this->_domainClassName)) { if ( ! ($record instanceof $this->_domainClassName)) {
throw new Doctrine_Mapper_Exception("Mapper of type " . $this->_domainClassName . " throw new Doctrine_Mapper_Exception("Mapper of type " . $this->_domainClassName . "
...@@ -282,7 +281,7 @@ abstract class Doctrine_EntityPersister_Abstract ...@@ -282,7 +281,7 @@ abstract class Doctrine_EntityPersister_Abstract
if ($record->isValid()) { if ($record->isValid()) {
$this->_insertOrUpdate($record); $this->_insertOrUpdate($record);
} else { } else {
$conn->transaction->addInvalid($record); $conn->getTransaction()->addInvalid($record);
} }
$state = $record->_state(); $state = $record->_state();
...@@ -375,7 +374,7 @@ abstract class Doctrine_EntityPersister_Abstract ...@@ -375,7 +374,7 @@ abstract class Doctrine_EntityPersister_Abstract
// Protection against infinite function recursion before attempting to save // Protection against infinite function recursion before attempting to save
if ($obj instanceof Doctrine_Entity && $obj->isModified()) { if ($obj instanceof Doctrine_Entity && $obj->isModified()) {
$obj->save($this->_conn); $obj->save();
/** Can this be removed? /** Can this be removed?
$id = array_values($obj->identifier()); $id = array_values($obj->identifier());
......
...@@ -101,6 +101,13 @@ class Doctrine_Query_Parser ...@@ -101,6 +101,13 @@ class Doctrine_Query_Parser
* @var int The number of tokens read since last error in the input string. * @var int The number of tokens read since last error in the input string.
*/ */
protected $_errorDistance; protected $_errorDistance;
/**
* The EntityManager.
*
* @var EnityManager
*/
protected $_em;
// End of Error management stuff // End of Error management stuff
...@@ -113,8 +120,9 @@ class Doctrine_Query_Parser ...@@ -113,8 +120,9 @@ class Doctrine_Query_Parser
*/ */
public function __construct(Doctrine_Query $query) public function __construct(Doctrine_Query $query)
{ {
$this->_em = $query->getEntityManager();
$this->_scanner = new Doctrine_Query_Scanner($query->getDql()); $this->_scanner = new Doctrine_Query_Scanner($query->getDql());
$this->_sqlBuilder = Doctrine_Query_SqlBuilder::fromConnection($query->getEntityManager()); $this->_sqlBuilder = Doctrine_Query_SqlBuilder::fromConnection($this->_em);
$this->_keywordTable = new Doctrine_Query_Token(); $this->_keywordTable = new Doctrine_Query_Token();
$this->_parserResult = new Doctrine_Query_ParserResult( $this->_parserResult = new Doctrine_Query_ParserResult(
...@@ -333,5 +341,14 @@ class Doctrine_Query_Parser ...@@ -333,5 +341,14 @@ class Doctrine_Query_Parser
$this->_errorDistance = 0; $this->_errorDistance = 0;
} }
/**
* Gets the EntityManager used by the parser.
*
* @return EntityManager
*/
public function getEntityManager()
{
return $this->_em;
}
} }
<?php <?php
/* /*
* $Id$ * $Id$
* *
...@@ -53,6 +52,13 @@ abstract class Doctrine_Query_Production ...@@ -53,6 +52,13 @@ abstract class Doctrine_Query_Production
* @var Doctrine_Query_Parser * @var Doctrine_Query_Parser
*/ */
protected $_parser; protected $_parser;
/**
* The EntityManager.
*
* @var EntityManager
*/
protected $_em;
/** /**
...@@ -63,6 +69,7 @@ abstract class Doctrine_Query_Production ...@@ -63,6 +69,7 @@ abstract class Doctrine_Query_Production
public function __construct(Doctrine_Query_Parser $parser) public function __construct(Doctrine_Query_Parser $parser)
{ {
$this->_parser = $parser; $this->_parser = $parser;
$this->_em = $this->_parser->getEntityManager();
} }
......
...@@ -130,8 +130,7 @@ class Doctrine_Query_Production_PathExpression extends Doctrine_Query_Production ...@@ -130,8 +130,7 @@ class Doctrine_Query_Production_PathExpression extends Doctrine_Query_Production
$parserResult = $this->_parser->getParserResult(); $parserResult = $this->_parser->getParserResult();
// Retrieving connection // Retrieving connection
$manager = Doctrine_EntityManagerFactory::getManager(); $conn = $this->_em->getConnection();
$conn = $manager->getConnection();
// Looking for queryComponent to fetch // Looking for queryComponent to fetch
$queryComponent = $parserResult->getQueryComponent($this->_componentAlias); $queryComponent = $parserResult->getQueryComponent($this->_componentAlias);
......
...@@ -119,8 +119,7 @@ class Doctrine_Query_Production_PathExpressionEndingWithAsterisk extends Doctrin ...@@ -119,8 +119,7 @@ class Doctrine_Query_Production_PathExpressionEndingWithAsterisk extends Doctrin
$parserResult = $this->_parser->getParserResult(); $parserResult = $this->_parser->getParserResult();
// Retrieving connection // Retrieving connection
$manager = Doctrine_EntityManagerFactory::getManager(); $conn = $this->_em->getConnection();
$conn = $manager->getConnection();
// Looking for componentAlias to fetch // Looking for componentAlias to fetch
$componentAlias = implode('.', $this->_identifiers); $componentAlias = implode('.', $this->_identifiers);
......
...@@ -115,13 +115,12 @@ class Doctrine_Query_Production_RangeVariableDeclaration extends Doctrine_Query_ ...@@ -115,13 +115,12 @@ class Doctrine_Query_Production_RangeVariableDeclaration extends Doctrine_Query_
$parserResult = $this->_parser->getParserResult(); $parserResult = $this->_parser->getParserResult();
// Get the connection for the component // Get the connection for the component
$conn = $this->_parser->getSqlBuilder()->getConnection(); $conn = $this->_em->getConnection();
$manager = Doctrine_EntityManagerFactory::getManager();
$componentName = $this->_identifiers[0]; $componentName = $this->_identifiers[0];
// Retrieving ClassMetadata and Mapper // Retrieving ClassMetadata and Mapper
try { try {
$classMetadata = $manager->getClassMetadata($componentName); $classMetadata = $this->_em->getClassMetadata($componentName);
// Building queryComponent // Building queryComponent
$queryComponent = array( $queryComponent = array(
...@@ -155,8 +154,7 @@ class Doctrine_Query_Production_RangeVariableDeclaration extends Doctrine_Query_ ...@@ -155,8 +154,7 @@ class Doctrine_Query_Production_RangeVariableDeclaration extends Doctrine_Query_
$parserResult = $this->_parser->getParserResult(); $parserResult = $this->_parser->getParserResult();
// Get the connection for the component // Get the connection for the component
$conn = $this->_parser->getSqlBuilder()->getConnection(); $conn = $this->_em->getConnection();
$manager = Doctrine_EntityManagerFactory::getManager();
// Retrieve the base component // Retrieve the base component
try { try {
......
...@@ -136,8 +136,7 @@ class Doctrine_Query_Production_SelectExpression extends Doctrine_Query_Producti ...@@ -136,8 +136,7 @@ class Doctrine_Query_Production_SelectExpression extends Doctrine_Query_Producti
$parserResult = $this->_parser->getParserResult(); $parserResult = $this->_parser->getParserResult();
// Retrieving connection // Retrieving connection
$manager = Doctrine_EntityManagerFactory::getManager(); $conn = $this->_em->getConnection();
$conn = $manager->getConnection();
switch (get_class($this->_leftExpression)) { switch (get_class($this->_leftExpression)) {
case 'Doctrine_Query_Production_PathExpressionEndingWithAsterisk': case 'Doctrine_Query_Production_PathExpressionEndingWithAsterisk':
......
...@@ -83,12 +83,11 @@ class Doctrine_Query_Production_VariableDeclaration extends Doctrine_Query_Produ ...@@ -83,12 +83,11 @@ class Doctrine_Query_Production_VariableDeclaration extends Doctrine_Query_Produ
// No queryComponent was found. We will have to build it for the first time // No queryComponent was found. We will have to build it for the first time
// Get the connection for the component // Get the connection for the component
$conn = $this->_parser->getSqlBuilder()->getConnection(); $conn = $this->_em->getConnection();
$manager = Doctrine_EntityManagerFactory::getManager();
// Retrieving ClassMetadata and Mapper // Retrieving ClassMetadata and Mapper
try { try {
$classMetadata = $manager->getMetadata($this->_componentName); $classMetadata = $this->_em->getMetadata($this->_componentName);
// Building queryComponent // Building queryComponent
$queryComponent = array( $queryComponent = array(
...@@ -124,8 +123,7 @@ class Doctrine_Query_Production_VariableDeclaration extends Doctrine_Query_Produ ...@@ -124,8 +123,7 @@ class Doctrine_Query_Production_VariableDeclaration extends Doctrine_Query_Produ
$queryComponent = $parserResult->getQueryComponent($this->_componentAlias); $queryComponent = $parserResult->getQueryComponent($this->_componentAlias);
// Retrieving connection // Retrieving connection
$manager = Doctrine_EntityManagerFactory::getManager(); $conn = $this->_em->getConnection();
$conn = $manager->getConnection();
return $conn->quoteIdentifier($queryComponent['metadata']->getTableName()) . ' ' return $conn->quoteIdentifier($queryComponent['metadata']->getTableName()) . ' '
. $conn->quoteIdentifier($parserResult->getTableAliasFromComponentAlias($this->_componentAlias)); . $conn->quoteIdentifier($parserResult->getTableAliasFromComponentAlias($this->_componentAlias));
......
<?php
if (!defined('PHPUnit_MAIN_METHOD')) {
define('PHPUnit_MAIN_METHOD', 'Orm_Entity_AllTests::main');
}
require_once 'lib/DoctrineTestInit.php';
// Tests
require_once 'Orm/Entity/AccessorTestCase.php';
require_once 'Orm/Entity/ConstructorTest.php';
class Orm_Entity_AllTests
{
public static function main()
{
PHPUnit_TextUI_TestRunner::run(self::suite());
}
public static function suite()
{
$suite = new Doctrine_TestSuite('Doctrine Orm Entity Tests');
$suite->addTestSuite('Orm_Entity_AccessorTestCase');
$suite->addTestSuite('Orm_Entity_ConstructorTest');
return $suite;
}
}
if (PHPUnit_MAIN_METHOD == 'Orm_Entity_AllTests::main') {
Orm_Entity_AllTests::main();
}
\ No newline at end of file
<?php
require_once 'lib/DoctrineTestInit.php';
class Orm_Entity_ConstructorTest extends Doctrine_OrmTestCase
{
public function testFieldInitializationInConstructor()
{
$entity = new ConstructorTestEntity1("romanb");
$this->assertTrue($entity->isTransient());
$this->assertEquals("romanb", $entity->username);
}
}
class ConstructorTestEntity1 extends Doctrine_Entity
{
public function __construct($username = null)
{
parent::__construct();
if ($this->isTransient()) {
$this->username = $username;
}
}
/* The mapping definition */
public static function initMetadata($class)
{
$class->mapColumn('username', 'string', 50, array());
}
}
?>
\ No newline at end of file
<?php
require_once 'lib/DoctrineTestInit.php';
class Orm_UnitOfWorkTest extends Doctrine_OrmTestCase
{
private $_unitOfWork;
private $_user;
protected function setUp() {
parent::setUp();
$this->_user = new ForumUser();
$this->_unitOfWork = $this->_em->getUnitOfWork();
}
protected function tearDown() {
$this->_user->free();
}
public function testRegisterNew()
{
$this->_user->username = 'romanb';
$this->_user->id = 1;
// registerNew() is normally called in save()/persist()
$this->_unitOfWork->registerNew($this->_user);
$this->assertTrue($this->_unitOfWork->isRegisteredNew($this->_user));
$this->assertTrue($this->_unitOfWork->contains($this->_user));
$this->assertFalse($this->_unitOfWork->isRegisteredDirty($this->_user));
$this->assertFalse($this->_unitOfWork->isRegisteredRemoved($this->_user));
}
public function testRegisterDirty()
{
$this->_user->username = 'romanb';
$this->_user->id = 1;
$this->assertEquals(Doctrine_Entity::STATE_TDIRTY, $this->_user->_state());
$this->assertFalse($this->_unitOfWork->contains($this->_user));
$this->_unitOfWork->registerDirty($this->_user);
$this->assertTrue($this->_unitOfWork->isRegisteredDirty($this->_user));
$this->assertFalse($this->_unitOfWork->isRegisteredNew($this->_user));
$this->assertFalse($this->_unitOfWork->isRegisteredRemoved($this->_user));
}
public function testRegisterRemovedOnTransientEntityIsIgnored()
{
$this->_user->username = 'romanb';
$this->_user->id = 1;
$this->assertFalse($this->_unitOfWork->isRegisteredRemoved($this->_user));
$this->_unitOfWork->registerRemoved($this->_user);
$this->assertFalse($this->_unitOfWork->isRegisteredRemoved($this->_user));
}
/*public function testSavedEntityHasIdentityAndIsManaged()
{
$this->_user->username = 'romanb';
$this->_user->save();
$this->assertTrue($this->_unitOfWork->hasIdentity($this->_user));
$this->assertTrue($this->_unitOfWork->isManaged($this->_user));
}*/
}
\ 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