Commit 3cd7b954 authored by romanb's avatar romanb

Refactorings. Merged hydrator fixes from 0.11/1.0

parent 4d752746
This diff is collapsed.
......@@ -34,12 +34,19 @@
*/
class Doctrine_ClassMetadata_CodeDriver
{
/**
* Name of the callback method.
*
* @todo We could make the name of the callback methods customizable for users.
*/
const CALLBACK_METHOD = 'initMetadata';
/**
* Loads the metadata for the specified class into the provided container.
*/
public function loadMetadataForClass($className, Doctrine_ClassMetadata $metadata)
{
if ( ! method_exists($className, 'initMetadata')) {
if ( ! method_exists($className, self::CALLBACK_METHOD)) {
throw new Doctrine_ClassMetadata_Exception("Unable to load metadata for class"
. " '$className'. Callback method 'initMetadata' not found.");
}
......
......@@ -133,7 +133,10 @@ class Doctrine_ClassMetadata_Factory
foreach ($parentClass->getColumns() as $name => $definition) {
$fullName = "$name as " . $parentClass->getFieldName($name);
$definition['inherited'] = true;
$subClass->mapColumn($fullName, $definition['type'], $definition['length'],
$subClass->mapColumn(
$fullName,
$definition['type'],
$definition['length'],
$definition);
}
}
......@@ -154,11 +157,6 @@ class Doctrine_ClassMetadata_Factory
protected function _loadMetadata(Doctrine_ClassMetadata $class, $name)
{
if ( ! class_exists($name) || empty($name)) {
/*try {
throw new Exception();
} catch (Exception $e) {
echo $e->getTraceAsString();
}*/
throw new Doctrine_Exception("Couldn't find class " . $name . ".");
}
......@@ -175,11 +173,6 @@ class Doctrine_ClassMetadata_Factory
} while ($className = get_parent_class($className));
if ($className === false) {
try {
throw new Exception();
} catch (Exception $e) {
echo $e->getTraceAsString() . "<br />";
}
throw new Doctrine_ClassMetadata_Factory_Exception("Unknown component '$className'.");
}
......@@ -207,10 +200,11 @@ class Doctrine_ClassMetadata_Factory
protected function _initIdentifier(Doctrine_ClassMetadata $class)
{
switch (count((array)$class->getIdentifier())) {
case 0:
case 0: // No identifier in the class mapping yet
// If its a subclass, inherit the identifier from the parent.
if ($class->getInheritanceType() == Doctrine::INHERITANCE_TYPE_JOINED &&
count($class->getParentClasses()) > 0) {
count($class->getParentClasses()) > 0) {
$parents = $class->getParentClasses();
$root = end($parents);
$rootClass = $class->getConnection()->getMetadata($root);
......@@ -237,17 +231,20 @@ class Doctrine_ClassMetadata_Factory
$definition, true);
}
} else {
throw Doctrine_MappingException::identifierRequired($class->getClassName());
/* Legacy behavior of auto-adding an id field
$definition = array('type' => 'integer',
'length' => 20,
'autoincrement' => true,
'primary' => true);
$class->setColumn('id', $definition['type'], $definition['length'], $definition, true);
$class->mapColumn('id', $definition['type'], $definition['length'], $definition, true);
$class->setIdentifier(array('id'));
$class->setIdentifierType(Doctrine::IDENTIFIER_AUTOINC);
*/
}
break;
case 1:
foreach ((array)$class->getIdentifier() as $pk) {
case 1: // A single identifier is in the mapping
foreach ($class->getIdentifier() as $pk) {
$columnName = $class->getColumnName($pk);
$thisColumns = $class->getColumns();
$e = $thisColumns[$columnName];
......@@ -294,7 +291,7 @@ class Doctrine_ClassMetadata_Factory
$class->setIdentifier(array($pk));
break;
default:
default: // Multiple identifiers are in the mapping so its a composite id
$class->setIdentifierType(Doctrine::IDENTIFIER_COMPOSITE);
}
}
......
This diff is collapsed.
This diff is collapsed.
......@@ -31,10 +31,8 @@
/**
* The EntityManager is a central access point to ORM functionality.
* The EntityManager is the central access point to ORM functionality.
*
* @package Doctrine
* @subpackage EntityManager
* @license http://www.opensource.org/licenses/lgpl-license.php LGPL
* @link www.phpdoctrine.org
* @since 2.0
......@@ -88,7 +86,6 @@ class Doctrine_EntityManager
/**
* The EntityPersister instances.
* @todo Implementation.
*
* @var array
*/
......@@ -109,7 +106,7 @@ class Doctrine_EntityManager
private $_flushMode = 'commit';
/**
* The unit of work.
* The unit of work used to coordinate object-level transactions.
*
* @var UnitOfWork
*/
......@@ -122,13 +119,6 @@ class Doctrine_EntityManager
*/
private $_eventManager;
/**
* Enter description here...
*
* @var unknown_type
*/
//private $_dataTemplates = array();
/**
* Container that is used temporarily during hydration.
*
......@@ -163,7 +153,7 @@ class Doctrine_EntityManager
}
/**
* Returns the metadata for a class. Alias for getClassMetadata().
* Gets the metadata for a class. Alias for getClassMetadata().
*
* @return Doctrine_Metadata
* @todo package:orm
......@@ -172,6 +162,17 @@ class Doctrine_EntityManager
{
return $this->getClassMetadata($className);
}
/**
* Gets the transaction object used by the EntityManager to manage
* database transactions.
*
* @return Doctrine::DBAL::Transaction
*/
public function getTransaction()
{
return $this->_conn->getTransaction();
}
/**
* Returns the metadata for a class.
......@@ -184,8 +185,8 @@ class Doctrine_EntityManager
}
/**
* Sets the driver that is used to obtain metadata informations about entity
* classes.
* Sets the driver that is used to obtain metadata mapping information
* about Entities.
*
* @param $driver The driver to use.
*/
......@@ -197,7 +198,8 @@ class Doctrine_EntityManager
/**
* Creates a new Doctrine_Query object that operates on this connection.
*
* @return Doctrine_Query
* @param string The DQL string.
* @return Doctrine::ORM::Query
* @todo package:orm
*/
public function createQuery($dql = "")
......@@ -211,10 +213,12 @@ class Doctrine_EntityManager
}
/**
* Enter description here...
* Gets the EntityPersister for an Entity.
*
* This is usually not of interest for users, mainly for internal use.
*
* @param unknown_type $entityName
* @return unknown
* @param string $entityName The name of the Entity.
* @return Doctrine::ORM::Internal::EntityPersister
*/
public function getEntityPersister($entityName)
{
......@@ -241,30 +245,6 @@ class Doctrine_EntityManager
return $this->_unitOfWork->unregisterIdentity($entity);
}
/**
* Returns the current internal transaction nesting level.
*
* @return integer The nesting level. A value of 0 means theres no active transaction.
* @todo package:orm???
*/
public function getInternalTransactionLevel()
{
return $this->transaction->getInternalTransactionLevel();
}
/**
* Initiates a transaction.
*
* This method must only be used by Doctrine itself to initiate transactions.
* Userland-code must use {@link beginTransaction()}.
*
* @todo package:orm???
*/
public function beginInternalTransaction($savepoint = null)
{
return $this->transaction->beginInternalTransaction($savepoint);
}
/**
* Creates a query with the specified name.
*
......@@ -307,16 +287,16 @@ class Doctrine_EntityManager
*/
public function flush()
{
$this->beginInternalTransaction();
$this->_unitOfWork->flush();
$this->commit();
}
/**
* Enter description here...
* Finds an Entity by its identifier.
* This is just a convenient shortcut for getRepository()->find().
*
* @param unknown_type $entityName
* @param unknown_type $identifier
* @param string $entityName
* @param mixed $identifier
* @return Doctrine::ORM::Entity
*/
public function find($entityName, $identifier)
{
......@@ -356,11 +336,8 @@ class Doctrine_EntityManager
{
if ($entityName === null) {
$this->_unitOfWork->detachAll();
foreach ($this->_mappers as $mapper) {
$mapper->clear(); // clear identity map of each mapper
}
} else {
$this->getMapper($entityName)->clear();
//...
}
}
......@@ -370,7 +347,7 @@ class Doctrine_EntityManager
*/
public function close()
{
//Doctrine_EntityManagerFactory::releaseManager($this);
}
/**
......@@ -408,24 +385,7 @@ 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:
//...
}
$this->_unitOfWork->save($entity);
}
/**
......@@ -433,14 +393,14 @@ class Doctrine_EntityManager
*/
public function delete(Doctrine_Entity $entity)
{
//...
$this->_unitOfWork->delete($entity);
}
/**
* Gets the repository for the given entity name.
* Gets the repository for an Entity.
*
* @return Doctrine_EntityRepository The repository.
* @todo Implementation.
* @param string $entityName The name of the Entity.
* @return Doctrine::ORM::EntityRepository The repository.
*/
public function getRepository($entityName)
{
......
......@@ -62,21 +62,11 @@ abstract class Doctrine_EntityPersister_Abstract
*/
protected $_em;
/**
* The concrete mapping strategy that is used.
*/
protected $_mappingStrategy;
/**
* Null object.
*/
private $_nullObject;
/**
* A list of registered entity listeners.
*/
private $_entityListeners = array();
/**
* Enter description here...
*
......@@ -318,8 +308,8 @@ abstract class Doctrine_EntityPersister_Abstract
*/
protected function _insertOrUpdate(Doctrine_Entity $record)
{
$record->preSave();
$this->notifyEntityListeners($record, 'preSave', Doctrine_Event::RECORD_SAVE);
//$record->preSave();
//$this->notifyEntityListeners($record, 'preSave', Doctrine_Event::RECORD_SAVE);
switch ($record->_state()) {
case Doctrine_Entity::STATE_TDIRTY:
......@@ -335,8 +325,8 @@ abstract class Doctrine_EntityPersister_Abstract
break;
}
$record->postSave();
$this->notifyEntityListeners($record, 'postSave', Doctrine_Event::RECORD_SAVE);
//$record->postSave();
//$this->notifyEntityListeners($record, 'postSave', Doctrine_Event::RECORD_SAVE);
}
/**
......@@ -351,7 +341,6 @@ abstract class Doctrine_EntityPersister_Abstract
}
/**
* _saveRelated
* saves all related records to $record
*
* @throws PDOException if something went wrong at database level
......
......@@ -20,12 +20,10 @@
*/
/**
* The joined mapping strategy maps a single entity instance to several tables in the
* The joined subclass persister maps a single entity instance to several tables in the
* database as it is defined by <tt>Class Table Inheritance</tt>.
*
* @author Roman Borschel <roman@code-factory.org>
* @package Doctrine
* @subpackage JoinedSubclass
* @license http://www.opensource.org/licenses/lgpl-license.php LGPL
* @version $Revision$
* @link www.phpdoctrine.org
......@@ -33,7 +31,7 @@
*/
class Doctrine_EntityPersister_JoinedSubclass extends Doctrine_EntityPersister_Abstract
{
protected $_columnNameFieldNameMap = array();
//protected $_columnNameFieldNameMap = array();
/**
* Inserts an entity that is part of a Class Table Inheritance hierarchy.
......@@ -94,7 +92,6 @@ class Doctrine_EntityPersister_JoinedSubclass extends Doctrine_EntityPersister_A
*
* @param Doctrine_Entity $record record to be updated
* @return boolean whether or not the update was successful
* @todo Move to Doctrine_Table (which will become Doctrine_Mapper).
*/
protected function _doUpdate(Doctrine_Entity $record)
{
......@@ -223,34 +220,6 @@ class Doctrine_EntityPersister_JoinedSubclass extends Doctrine_EntityPersister_A
return $fieldNames;
}
/**
*
*/
/*public function getFieldName($columnName)
{
if (isset($this->_columnNameFieldNameMap[$columnName])) {
return $this->_columnNameFieldNameMap[$columnName];
}
$classMetadata = $this->_classMetadata;
$conn = $this->_conn;
if ($classMetadata->hasColumn($columnName)) {
$this->_columnNameFieldNameMap[$columnName] = $classMetadata->getFieldName($columnName);
return $this->_columnNameFieldNameMap[$columnName];
}
foreach ($classMetadata->getSubclasses() as $subClass) {
$subTable = $conn->getClassMetadata($subClass);
if ($subTable->hasColumn($columnName)) {
$this->_columnNameFieldNameMap[$columnName] = $subTable->getFieldName($columnName);
return $this->_columnNameFieldNameMap[$columnName];
}
}
throw new Doctrine_Mapper_Exception("No field name found for column name '$columnName'.");
}*/
/**
*
* @todo Looks like this better belongs into the ClassMetadata class.
......
......@@ -22,6 +22,7 @@
#namespace Doctrine::ORM;
/**
* A repository provides the illusion of an in-memory Entity store.
* Base class for all custom user-defined repositories.
* Provides basic finder methods, common to all repositories.
*
......@@ -48,7 +49,6 @@ class Doctrine_EntityRepository
}
/**
* createQuery
* creates a new Doctrine_Query object and adds the component name
* of this table as the query 'from' part
*
......
......@@ -29,93 +29,11 @@
* @subpackage EventListener
* @license http://www.opensource.org/licenses/lgpl-license.php LGPL
* @link www.phpdoctrine.org
* @since 1.0
* @since 2.0
* @version $Revision$
* @todo Remove. The 2.0 event system has no listener interfaces.
*/
class Doctrine_EventListener implements Doctrine_EventListener_Interface
interface Doctrine_EventSubscriber
{
public function preClose(Doctrine_Event $event)
{ }
public function postClose(Doctrine_Event $event)
{ }
public function onCollectionDelete(Doctrine_Collection $collection)
{ }
public function onPreCollectionDelete(Doctrine_Collection $collection)
{ }
public function onOpen(Doctrine_Connection $connection)
{ }
public function preTransactionCommit(Doctrine_Event $event)
{ }
public function postTransactionCommit(Doctrine_Event $event)
{ }
public function preTransactionRollback(Doctrine_Event $event)
{ }
public function postTransactionRollback(Doctrine_Event $event)
{ }
public function preTransactionBegin(Doctrine_Event $event)
{ }
public function postTransactionBegin(Doctrine_Event $event)
{ }
public function preSavepointCommit(Doctrine_Event $event)
{ }
public function postSavepointCommit(Doctrine_Event $event)
{ }
public function preSavepointRollback(Doctrine_Event $event)
{ }
public function postSavepointRollback(Doctrine_Event $event)
{ }
public function preSavepointCreate(Doctrine_Event $event)
{ }
public function postSavepointCreate(Doctrine_Event $event)
{ }
public function postConnect(Doctrine_Event $event)
{ }
public function preConnect(Doctrine_Event $event)
{ }
public function preQuery(Doctrine_Event $event)
{ }
public function postQuery(Doctrine_Event $event)
{ }
public function prePrepare(Doctrine_Event $event)
{ }
public function postPrepare(Doctrine_Event $event)
{ }
public function preExec(Doctrine_Event $event)
{ }
public function postExec(Doctrine_Event $event)
{ }
public function preError(Doctrine_Event $event)
{ }
public function postError(Doctrine_Event $event)
{ }
public function preFetch(Doctrine_Event $event)
{ }
public function postFetch(Doctrine_Event $event)
{ }
public function preFetchAll(Doctrine_Event $event)
{ }
public function postFetchAll(Doctrine_Event $event)
{ }
public function preStmtExecute(Doctrine_Event $event)
{ }
public function postStmtExecute(Doctrine_Event $event)
{ }
public function getSubscribedEvents();
}
......@@ -56,9 +56,9 @@ class Doctrine_EventManager
foreach ($this->_listeners[$callback] as $listener) {
$listener->$callback($event);
}
return ! $event->getDefaultPrevented();
}
return ! $event->getDefaultPrevented();
return true;
}
/**
......
......@@ -227,6 +227,7 @@ class Doctrine_HydratorNew extends Doctrine_Hydrator_Abstract
} else if (isset($resultPointers[$parent])) {
$baseElement =& $resultPointers[$parent];
} else {
unset($prev[$dqlAlias]); // Ticket #1228
continue;
}
......@@ -256,7 +257,8 @@ class Doctrine_HydratorNew extends Doctrine_Hydrator_Abstract
} else {
// x-1 relation
$oneToOne = true;
if ( ! isset($nonemptyComponents[$dqlAlias])) {
if ( ! isset($nonemptyComponents[$dqlAlias]) &&
! $driver->isFieldSet($baseElement, $relationAlias)) {
$driver->setRelatedElement($baseElement, $relationAlias,
$driver->getNullPointer());
} else if ( ! $driver->isFieldSet($baseElement, $relationAlias)) {
......@@ -293,8 +295,6 @@ class Doctrine_HydratorNew extends Doctrine_Hydrator_Abstract
}
/**
* _setLastElement
*
* sets the last element of given data array / collection
* as previous element
*
......@@ -308,7 +308,8 @@ class Doctrine_HydratorNew extends Doctrine_Hydrator_Abstract
*/
protected function _setLastElement(&$resultPointers, &$coll, $index, $dqlAlias, $oneToOne)
{
if ($coll === $this->_nullObject) {
if ($coll === $this->_nullObject || $coll === null) {
unset($resultPointers[$dqlAlias]); // Ticket #1228
return false;
}
......@@ -330,8 +331,6 @@ class Doctrine_HydratorNew extends Doctrine_Hydrator_Abstract
$resultPointers[$dqlAlias] = $coll;
} else if (count($coll) > 0) {
$resultPointers[$dqlAlias] = $coll->getLast();
} else if (isset($resultPointers[$dqlAlias])) {
unset($resultPointers[$dqlAlias]);
}
}
......
......@@ -77,7 +77,7 @@ class Doctrine_Query_Production_IndexBy extends Doctrine_Query_Production
}
// The INDEXBY field must be either the (primary && not part of composite pk) || (unique && notnull)
$columnMapping = $classMetadata->getColumnMapping($this->_fieldName);
$columnMapping = $classMetadata->getFieldMapping($this->_fieldName);
if ( ! $classMetadata->isIdentifier($this->_fieldName) && ! $classMetadata->isUniqueField($this->_fieldName) && ! $classMetadata->isNotNull($this->_fieldName)) {
$this->_parser->semanticalError(
......
......@@ -22,6 +22,7 @@ class CustomAccessorMutatorTestEntity extends Doctrine_Entity
{
public static function initMetadata($class)
{
$class->mapColumn('id', 'integer', 4, array('primary'));
$class->mapColumn('username', 'string', 50, array(
'accessor' => 'getUsernameCustom',
'mutator' => 'setUsernameCustom'));
......@@ -42,6 +43,7 @@ class MagicAccessorMutatorTestEntity extends Doctrine_Entity
{
public static function initMetadata($class)
{
$class->mapColumn('id', 'integer', 4, array('primary'));
$class->mapColumn('username', 'string', 50, array());
}
......
......@@ -6,7 +6,7 @@ class Orm_Entity_ConstructorTest extends Doctrine_OrmTestCase
public function testFieldInitializationInConstructor()
{
$entity = new ConstructorTestEntity1("romanb");
$this->assertTrue($entity->isTransient());
$this->assertTrue($entity->isNew());
$this->assertEquals("romanb", $entity->username);
}
}
......@@ -16,7 +16,7 @@ class ConstructorTestEntity1 extends Doctrine_Entity
public function __construct($username = null)
{
parent::__construct();
if ($this->isTransient()) {
if ($this->isNew()) {
$this->username = $username;
}
}
......@@ -24,6 +24,7 @@ class ConstructorTestEntity1 extends Doctrine_Entity
/* The mapping definition */
public static function initMetadata($class)
{
$class->mapColumn('id', 'integer', 4, array('primary'));
$class->mapColumn('username', 'string', 50, array());
}
}
......
<?php
class ForumBoard extends Doctrine_Entity {
public static function initMetadata($class) {
$class->mapColumn('position', 'integer');
$class->mapColumn('category_id', 'integer');
$class->hasOne('ForumCategory as category',
public static function initMetadata($metadata) {
/*$metadata->mapField(array(
'fieldName' => 'id',
'id' => true,
'type' => 'integer',
'length' => 4
));
*/
$metadata->mapColumn('id', 'integer', 4, array('primary'));
$metadata->mapColumn('position', 'integer');
$metadata->mapColumn('category_id', 'integer');
$metadata->hasOne('ForumCategory as category',
array('local' => 'category_id', 'foreign' => 'id'));
/*
$metadata->mapOneToOne(array(
'fieldName' => 'category', // optional, defaults to targetEntity
'targetEntity' => 'ForumCategory',
'joinColumns' => array('category_id' => 'id')
));
*/
}
}
<?php
class ForumCategory extends Doctrine_Entity {
public static function initMetadata($class) {
$class->mapColumn('id', 'integer', 4, array('primary'));
$class->mapColumn('position', 'integer');
$class->mapColumn('name', 'string', 255);
$class->hasMany('ForumBoard as boards', array(
......
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