Removed static EntityManager lookup from productions. Entity refactorings.
This commit is contained in:
parent
7206b1dd51
commit
35fa81dbab
13 changed files with 192 additions and 42 deletions
|
@ -66,7 +66,7 @@ abstract class Doctrine_Entity extends Doctrine_Access implements Serializable
|
|||
* PROXY STATE
|
||||
* An Entity is in proxy state when its properties are not fully loaded.
|
||||
*/
|
||||
const STATE_PROXY = 4;
|
||||
//const STATE_PROXY = 4;
|
||||
|
||||
/**
|
||||
* NEW TCLEAN
|
||||
|
@ -963,9 +963,8 @@ abstract class Doctrine_Entity extends Doctrine_Access implements Serializable
|
|||
return $this->$setter($value);
|
||||
}
|
||||
|
||||
$class = $this->_class;
|
||||
if ($class->hasField($fieldName)) {
|
||||
if ($value instanceof Doctrine_Entity) {
|
||||
if ($this->_class->hasField($fieldName)) {
|
||||
/*if ($value instanceof Doctrine_Entity) {
|
||||
$type = $class->getTypeOf($fieldName);
|
||||
// FIXME: composite key support
|
||||
$ids = $value->identifier();
|
||||
|
@ -973,17 +972,15 @@ abstract class Doctrine_Entity extends Doctrine_Access implements Serializable
|
|||
if ($id !== null && $type !== 'object') {
|
||||
$value = $id;
|
||||
}
|
||||
}
|
||||
}*/
|
||||
|
||||
$old = isset($this->_data[$fieldName]) ? $this->_data[$fieldName] : null;
|
||||
|
||||
if ($old !== $value) {
|
||||
if ($old != $value) {
|
||||
$this->_data[$fieldName] = $value;
|
||||
$this->_modified[] = $fieldName;
|
||||
|
||||
/* We can't do this currently because there are tests that change
|
||||
* the primary key of already persisted entities (ugh). */
|
||||
if ($this->isTransient() && $class->isIdentifier($fieldName)) {
|
||||
if ($this->isTransient() && $this->_class->isIdentifier($fieldName)) {
|
||||
$this->_id[$fieldName] = $value;
|
||||
}
|
||||
|
||||
|
@ -996,7 +993,7 @@ abstract class Doctrine_Entity extends Doctrine_Access implements Serializable
|
|||
break;
|
||||
}
|
||||
}
|
||||
} else if ($class->hasRelation($fieldName)) {
|
||||
} else if ($this->_class->hasRelation($fieldName)) {
|
||||
$this->_rawSetReference($fieldName, $value);
|
||||
} else {
|
||||
throw Doctrine_Entity_Exception::invalidField($fieldName);
|
||||
|
@ -1141,12 +1138,12 @@ abstract class Doctrine_Entity extends Doctrine_Access implements Serializable
|
|||
$dataSet[$field] = $this->_class->enumIndex($field, $this->_data[$field]);
|
||||
break;
|
||||
default:
|
||||
if ($this->_data[$field] instanceof Doctrine_Entity) {
|
||||
/*if ($this->_data[$field] instanceof Doctrine_Entity) {
|
||||
// FIXME: composite key support
|
||||
$ids = $this->_data[$field]->identifier();
|
||||
$id = count($ids) > 0 ? array_pop($ids) : null;
|
||||
$this->_data[$field] = $id;
|
||||
}
|
||||
}*/
|
||||
/** TODO:
|
||||
if ($this->_data[$v] === null) {
|
||||
throw new Doctrine_Record_Exception('Unexpected null value.');
|
||||
|
@ -1159,11 +1156,10 @@ abstract class Doctrine_Entity extends Doctrine_Access implements Serializable
|
|||
|
||||
// @todo cleanup
|
||||
// populates the discriminator field in Single & Class Table Inheritance
|
||||
$class = $this->_class;
|
||||
if ($class->getInheritanceType() == Doctrine::INHERITANCE_TYPE_JOINED ||
|
||||
$class->getInheritanceType() == Doctrine::INHERITANCE_TYPE_SINGLE_TABLE) {
|
||||
$discCol = $class->getInheritanceOption('discriminatorColumn');
|
||||
$discMap = $class->getInheritanceOption('discriminatorMap');
|
||||
if ($this->_class->getInheritanceType() == Doctrine::INHERITANCE_TYPE_JOINED ||
|
||||
$this->_class->getInheritanceType() == Doctrine::INHERITANCE_TYPE_SINGLE_TABLE) {
|
||||
$discCol = $this->_class->getInheritanceOption('discriminatorColumn');
|
||||
$discMap = $this->_class->getInheritanceOption('discriminatorMap');
|
||||
$old = $this->get($discCol, false);
|
||||
$discValue = array_search($this->_entityName, $discMap);
|
||||
if ((string) $old !== (string) $discValue || $old === null) {
|
||||
|
|
|
@ -59,7 +59,6 @@ class Doctrine_EntityManager
|
|||
*/
|
||||
private $_config;
|
||||
|
||||
|
||||
/**
|
||||
* The database connection used by the EntityManager.
|
||||
*
|
||||
|
@ -409,7 +408,24 @@ class Doctrine_EntityManager
|
|||
*/
|
||||
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
|
|||
}
|
||||
|
||||
/**
|
||||
* Saves an entity and all it's related entities.
|
||||
* Saves an entity.
|
||||
*
|
||||
* @param Doctrine_Entity $record The entity to save.
|
||||
* @param Doctrine_Connection $conn The connection to use. Will default to the mapper's
|
||||
* 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)) {
|
||||
throw new Doctrine_Mapper_Exception("Mapper of type " . $this->_domainClassName . "
|
||||
|
@ -282,7 +281,7 @@ abstract class Doctrine_EntityPersister_Abstract
|
|||
if ($record->isValid()) {
|
||||
$this->_insertOrUpdate($record);
|
||||
} else {
|
||||
$conn->transaction->addInvalid($record);
|
||||
$conn->getTransaction()->addInvalid($record);
|
||||
}
|
||||
|
||||
$state = $record->_state();
|
||||
|
@ -375,7 +374,7 @@ abstract class Doctrine_EntityPersister_Abstract
|
|||
|
||||
// Protection against infinite function recursion before attempting to save
|
||||
if ($obj instanceof Doctrine_Entity && $obj->isModified()) {
|
||||
$obj->save($this->_conn);
|
||||
$obj->save();
|
||||
|
||||
/** Can this be removed?
|
||||
$id = array_values($obj->identifier());
|
||||
|
|
|
@ -101,6 +101,13 @@ class Doctrine_Query_Parser
|
|||
* @var int The number of tokens read since last error in the input string.
|
||||
*/
|
||||
protected $_errorDistance;
|
||||
|
||||
/**
|
||||
* The EntityManager.
|
||||
*
|
||||
* @var EnityManager
|
||||
*/
|
||||
protected $_em;
|
||||
|
||||
// End of Error management stuff
|
||||
|
||||
|
@ -113,8 +120,9 @@ class Doctrine_Query_Parser
|
|||
*/
|
||||
public function __construct(Doctrine_Query $query)
|
||||
{
|
||||
$this->_em = $query->getEntityManager();
|
||||
$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->_parserResult = new Doctrine_Query_ParserResult(
|
||||
|
@ -333,5 +341,14 @@ class Doctrine_Query_Parser
|
|||
|
||||
$this->_errorDistance = 0;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Gets the EntityManager used by the parser.
|
||||
*
|
||||
* @return EntityManager
|
||||
*/
|
||||
public function getEntityManager()
|
||||
{
|
||||
return $this->_em;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,5 +1,4 @@
|
|||
<?php
|
||||
|
||||
/*
|
||||
* $Id$
|
||||
*
|
||||
|
@ -53,6 +52,13 @@ abstract class Doctrine_Query_Production
|
|||
* @var Doctrine_Query_Parser
|
||||
*/
|
||||
protected $_parser;
|
||||
|
||||
/**
|
||||
* The EntityManager.
|
||||
*
|
||||
* @var EntityManager
|
||||
*/
|
||||
protected $_em;
|
||||
|
||||
|
||||
/**
|
||||
|
@ -63,6 +69,7 @@ abstract class Doctrine_Query_Production
|
|||
public function __construct(Doctrine_Query_Parser $parser)
|
||||
{
|
||||
$this->_parser = $parser;
|
||||
$this->_em = $this->_parser->getEntityManager();
|
||||
}
|
||||
|
||||
|
||||
|
|
|
@ -130,8 +130,7 @@ class Doctrine_Query_Production_PathExpression extends Doctrine_Query_Production
|
|||
$parserResult = $this->_parser->getParserResult();
|
||||
|
||||
// Retrieving connection
|
||||
$manager = Doctrine_EntityManagerFactory::getManager();
|
||||
$conn = $manager->getConnection();
|
||||
$conn = $this->_em->getConnection();
|
||||
|
||||
// Looking for queryComponent to fetch
|
||||
$queryComponent = $parserResult->getQueryComponent($this->_componentAlias);
|
||||
|
|
|
@ -119,8 +119,7 @@ class Doctrine_Query_Production_PathExpressionEndingWithAsterisk extends Doctrin
|
|||
$parserResult = $this->_parser->getParserResult();
|
||||
|
||||
// Retrieving connection
|
||||
$manager = Doctrine_EntityManagerFactory::getManager();
|
||||
$conn = $manager->getConnection();
|
||||
$conn = $this->_em->getConnection();
|
||||
|
||||
// Looking for componentAlias to fetch
|
||||
$componentAlias = implode('.', $this->_identifiers);
|
||||
|
|
|
@ -115,13 +115,12 @@ class Doctrine_Query_Production_RangeVariableDeclaration extends Doctrine_Query_
|
|||
$parserResult = $this->_parser->getParserResult();
|
||||
|
||||
// Get the connection for the component
|
||||
$conn = $this->_parser->getSqlBuilder()->getConnection();
|
||||
$manager = Doctrine_EntityManagerFactory::getManager();
|
||||
$conn = $this->_em->getConnection();
|
||||
$componentName = $this->_identifiers[0];
|
||||
|
||||
// Retrieving ClassMetadata and Mapper
|
||||
try {
|
||||
$classMetadata = $manager->getClassMetadata($componentName);
|
||||
$classMetadata = $this->_em->getClassMetadata($componentName);
|
||||
|
||||
// Building queryComponent
|
||||
$queryComponent = array(
|
||||
|
@ -155,8 +154,7 @@ class Doctrine_Query_Production_RangeVariableDeclaration extends Doctrine_Query_
|
|||
$parserResult = $this->_parser->getParserResult();
|
||||
|
||||
// Get the connection for the component
|
||||
$conn = $this->_parser->getSqlBuilder()->getConnection();
|
||||
$manager = Doctrine_EntityManagerFactory::getManager();
|
||||
$conn = $this->_em->getConnection();
|
||||
|
||||
// Retrieve the base component
|
||||
try {
|
||||
|
|
|
@ -136,8 +136,7 @@ class Doctrine_Query_Production_SelectExpression extends Doctrine_Query_Producti
|
|||
$parserResult = $this->_parser->getParserResult();
|
||||
|
||||
// Retrieving connection
|
||||
$manager = Doctrine_EntityManagerFactory::getManager();
|
||||
$conn = $manager->getConnection();
|
||||
$conn = $this->_em->getConnection();
|
||||
|
||||
switch (get_class($this->_leftExpression)) {
|
||||
case 'Doctrine_Query_Production_PathExpressionEndingWithAsterisk':
|
||||
|
|
|
@ -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
|
||||
|
||||
// Get the connection for the component
|
||||
$conn = $this->_parser->getSqlBuilder()->getConnection();
|
||||
$manager = Doctrine_EntityManagerFactory::getManager();
|
||||
$conn = $this->_em->getConnection();
|
||||
|
||||
// Retrieving ClassMetadata and Mapper
|
||||
try {
|
||||
$classMetadata = $manager->getMetadata($this->_componentName);
|
||||
$classMetadata = $this->_em->getMetadata($this->_componentName);
|
||||
|
||||
// Building queryComponent
|
||||
$queryComponent = array(
|
||||
|
@ -124,8 +123,7 @@ class Doctrine_Query_Production_VariableDeclaration extends Doctrine_Query_Produ
|
|||
$queryComponent = $parserResult->getQueryComponent($this->_componentAlias);
|
||||
|
||||
// Retrieving connection
|
||||
$manager = Doctrine_EntityManagerFactory::getManager();
|
||||
$conn = $manager->getConnection();
|
||||
$conn = $this->_em->getConnection();
|
||||
|
||||
return $conn->quoteIdentifier($queryComponent['metadata']->getTableName()) . ' '
|
||||
. $conn->quoteIdentifier($parserResult->getTableAliasFromComponentAlias($this->_componentAlias));
|
||||
|
|
32
tests/Orm/Entity/AllTests.php
Normal file
32
tests/Orm/Entity/AllTests.php
Normal file
|
@ -0,0 +1,32 @@
|
|||
<?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();
|
||||
}
|
31
tests/Orm/Entity/ConstructorTest.php
Normal file
31
tests/Orm/Entity/ConstructorTest.php
Normal file
|
@ -0,0 +1,31 @@
|
|||
<?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());
|
||||
}
|
||||
}
|
||||
|
||||
?>
|
59
tests/Orm/UnitOfWorkTest.php
Normal file
59
tests/Orm/UnitOfWorkTest.php
Normal file
|
@ -0,0 +1,59 @@
|
|||
<?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));
|
||||
}*/
|
||||
}
|
Loading…
Add table
Reference in a new issue