Commit d2360791 authored by romanb's avatar romanb

[2.0] Formatting issues.

parent 6e760bac
...@@ -15,15 +15,15 @@ use Doctrine\DBAL\Platforms\AbstractPlatform; ...@@ -15,15 +15,15 @@ use Doctrine\DBAL\Platforms\AbstractPlatform;
*/ */
abstract class Type abstract class Type
{ {
/* The following constants represent type codes and mirror the PDO::PARAM_X constants /* The following constants represent type codes and mirror the PDO::PARAM_X constants
* to decouple ourself from PDO. * to decouple ourself from PDO.
*/ */
const CODE_BOOL = 5; const CODE_BOOL = 5;
const CODE_NULL = 0; const CODE_NULL = 0;
const CODE_INT = 1; const CODE_INT = 1;
const CODE_STR = 2; const CODE_STR = 2;
const CODE_LOB = 3; const CODE_LOB = 3;
private static $_typeObjects = array(); private static $_typeObjects = array();
private static $_typesMap = array( private static $_typesMap = array(
'integer' => 'Doctrine\DBAL\Types\IntegerType', 'integer' => 'Doctrine\DBAL\Types\IntegerType',
...@@ -61,7 +61,7 @@ abstract class Type ...@@ -61,7 +61,7 @@ abstract class Type
public function getTypeCode() public function getTypeCode()
{ {
return self::CODE_STR; return self::CODE_STR;
} }
/** /**
......
...@@ -36,252 +36,252 @@ use Doctrine\Common\DoctrineException; ...@@ -36,252 +36,252 @@ use Doctrine\Common\DoctrineException;
*/ */
class JoinedSubclassPersister extends StandardEntityPersister class JoinedSubclassPersister extends StandardEntityPersister
{ {
/** Map that maps column names to the table names that own them. /** Map that maps column names to the table names that own them.
* This is mainly a temporary cache, used during a single request. * This is mainly a temporary cache, used during a single request.
*/ */
private $_owningTableMap = array(); private $_owningTableMap = array();
/** /**
* {@inheritdoc} * {@inheritdoc}
* *
* @override * @override
*/ */
protected function _prepareData($entity, array &$result, $isInsert = false) protected function _prepareData($entity, array &$result, $isInsert = false)
{ {
parent::_prepareData($entity, $result, $isInsert); parent::_prepareData($entity, $result, $isInsert);
// Populate the discriminator column // Populate the discriminator column
if ($isInsert) { if ($isInsert) {
$discColumn = $this->_class->discriminatorColumn; $discColumn = $this->_class->discriminatorColumn;
$rootClass = $this->_em->getClassMetadata($this->_class->rootEntityName); $rootClass = $this->_em->getClassMetadata($this->_class->rootEntityName);
$result[$rootClass->primaryTable['name']][$discColumn['name']] = $result[$rootClass->primaryTable['name']][$discColumn['name']] =
$this->_class->discriminatorValue; $this->_class->discriminatorValue;
} }
} }
/** /**
* {@inheritdoc} * {@inheritdoc}
* *
* @override * @override
*/ */
public function getOwningTable($fieldName) public function getOwningTable($fieldName)
{ {
if ( ! isset($this->_owningTableMap[$fieldName])) { if ( ! isset($this->_owningTableMap[$fieldName])) {
if (isset($this->_class->associationMappings[$fieldName])) { if (isset($this->_class->associationMappings[$fieldName])) {
if (isset($this->_class->inheritedAssociationFields[$fieldName])) { if (isset($this->_class->inheritedAssociationFields[$fieldName])) {
$this->_owningTableMap[$fieldName] = $this->_em->getClassMetadata( $this->_owningTableMap[$fieldName] = $this->_em->getClassMetadata(
$this->_class->inheritedAssociationFields[$fieldName])->primaryTable['name']; $this->_class->inheritedAssociationFields[$fieldName])->primaryTable['name'];
} else { } else {
$this->_owningTableMap[$fieldName] = $this->_class->primaryTable['name']; $this->_owningTableMap[$fieldName] = $this->_class->primaryTable['name'];
} }
} else if (isset($this->_class->fieldMappings[$fieldName]['inherited'])) { } else if (isset($this->_class->fieldMappings[$fieldName]['inherited'])) {
$this->_owningTableMap[$fieldName] = $this->_em->getClassMetadata( $this->_owningTableMap[$fieldName] = $this->_em->getClassMetadata(
$this->_class->fieldMappings[$fieldName]['inherited'])->primaryTable['name']; $this->_class->fieldMappings[$fieldName]['inherited'])->primaryTable['name'];
} else { } else {
$this->_owningTableMap[$fieldName] = $this->_class->primaryTable['name']; $this->_owningTableMap[$fieldName] = $this->_class->primaryTable['name'];
} }
} }
return $this->_owningTableMap[$fieldName]; return $this->_owningTableMap[$fieldName];
} }
/** /**
* {@inheritdoc} * {@inheritdoc}
* *
* @override * @override
*/ */
public function executeInserts() public function executeInserts()
{ {
if ( ! $this->_queuedInserts) { if ( ! $this->_queuedInserts) {
return; return;
} }
$postInsertIds = array(); $postInsertIds = array();
$idGen = $this->_class->idGenerator; $idGen = $this->_class->idGenerator;
$isPostInsertId = $idGen->isPostInsertGenerator(); $isPostInsertId = $idGen->isPostInsertGenerator();
$sqlLogger = $this->_conn->getConfiguration()->getSqlLogger(); $sqlLogger = $this->_conn->getConfiguration()->getSqlLogger();
// Prepare statements for all tables // Prepare statements for all tables
$stmts = $classes = array(); $stmts = $classes = array();
$stmts[$this->_class->primaryTable['name']] = $this->_conn->prepare($this->_class->insertSql); $stmts[$this->_class->primaryTable['name']] = $this->_conn->prepare($this->_class->insertSql);
$sql[$this->_class->primaryTable['name']] = $this->_class->insertSql; $sql[$this->_class->primaryTable['name']] = $this->_class->insertSql;
foreach ($this->_class->parentClasses as $parentClass) { foreach ($this->_class->parentClasses as $parentClass) {
$parentClass = $this->_em->getClassMetadata($parentClass); $parentClass = $this->_em->getClassMetadata($parentClass);
$sql[$parentClass->primaryTable['name']] = $parentClass->insertSql; $sql[$parentClass->primaryTable['name']] = $parentClass->insertSql;
$stmts[$parentClass->primaryTable['name']] = $this->_conn->prepare($parentClass->insertSql); $stmts[$parentClass->primaryTable['name']] = $this->_conn->prepare($parentClass->insertSql);
} }
$rootTableName = $this->_em->getClassMetadata($this->_class->rootEntityName)->primaryTable['name']; $rootTableName = $this->_em->getClassMetadata($this->_class->rootEntityName)->primaryTable['name'];
foreach ($this->_queuedInserts as $entity) { foreach ($this->_queuedInserts as $entity) {
$insertData = array(); $insertData = array();
$this->_prepareData($entity, $insertData, true); $this->_prepareData($entity, $insertData, true);
// Execute insert on root table // Execute insert on root table
$stmt = $stmts[$rootTableName]; $stmt = $stmts[$rootTableName];
$paramIndex = 1; $paramIndex = 1;
if ($sqlLogger) { if ($sqlLogger) {
$params = array(); $params = array();
foreach ($insertData[$rootTableName] as $columnName => $value) { foreach ($insertData[$rootTableName] as $columnName => $value) {
$params[$paramIndex] = $value; $params[$paramIndex] = $value;
$stmt->bindValue($paramIndex++, $value/*, TODO: TYPE*/); $stmt->bindValue($paramIndex++, $value/*, TODO: TYPE*/);
} }
$sqlLogger->logSql($sql[$rootTableName], $params); $sqlLogger->logSql($sql[$rootTableName], $params);
} else { } else {
foreach ($insertData[$rootTableName] as $columnName => $value) { foreach ($insertData[$rootTableName] as $columnName => $value) {
$stmt->bindValue($paramIndex++, $value/*, TODO: TYPE*/); $stmt->bindValue($paramIndex++, $value/*, TODO: TYPE*/);
} }
} }
$stmt->execute(); $stmt->execute();
unset($insertData[$rootTableName]); unset($insertData[$rootTableName]);
if ($isPostInsertId) { if ($isPostInsertId) {
$id = $idGen->generate($this->_em, $entity); $id = $idGen->generate($this->_em, $entity);
$postInsertIds[$id] = $entity; $postInsertIds[$id] = $entity;
} else { } else {
$id = $this->_em->getUnitOfWork()->getEntityIdentifier($entity); $id = $this->_em->getUnitOfWork()->getEntityIdentifier($entity);
} }
// Execute inserts on subtables // Execute inserts on subtables
foreach ($insertData as $tableName => $data) { foreach ($insertData as $tableName => $data) {
$stmt = $stmts[$tableName]; $stmt = $stmts[$tableName];
$paramIndex = 1; $paramIndex = 1;
if ($sqlLogger) { if ($sqlLogger) {
//TODO: Log type //TODO: Log type
$params = array(); $params = array();
foreach ((array)$id as $idVal) { foreach ((array)$id as $idVal) {
$params[$paramIndex] = $idVal; $params[$paramIndex] = $idVal;
$stmt->bindValue($paramIndex++, $idVal/*, TODO: TYPE*/); $stmt->bindValue($paramIndex++, $idVal/*, TODO: TYPE*/);
} }
foreach ($data as $columnName => $value) { foreach ($data as $columnName => $value) {
$params[$paramIndex] = $value; $params[$paramIndex] = $value;
$stmt->bindValue($paramIndex++, $value/*, TODO: TYPE*/); $stmt->bindValue($paramIndex++, $value/*, TODO: TYPE*/);
} }
$sqlLogger->logSql($sql[$tableName], $params); $sqlLogger->logSql($sql[$tableName], $params);
} else { } else {
foreach ((array)$id as $idVal) { foreach ((array)$id as $idVal) {
$stmt->bindValue($paramIndex++, $idVal/*, TODO: TYPE*/); $stmt->bindValue($paramIndex++, $idVal/*, TODO: TYPE*/);
} }
foreach ($data as $columnName => $value) { foreach ($data as $columnName => $value) {
$stmt->bindValue($paramIndex++, $value/*, TODO: TYPE*/); $stmt->bindValue($paramIndex++, $value/*, TODO: TYPE*/);
} }
} }
$stmt->execute(); $stmt->execute();
} }
} }
foreach ($stmts as $stmt) foreach ($stmts as $stmt)
$stmt->closeCursor(); $stmt->closeCursor();
$this->_queuedInserts = array(); $this->_queuedInserts = array();
return $postInsertIds; return $postInsertIds;
} }
/** /**
* Updates an entity. * Updates an entity.
* *
* @param object $entity The entity to update. * @param object $entity The entity to update.
* @override * @override
*/ */
public function update($entity) public function update($entity)
{ {
$updateData = array(); $updateData = array();
$this->_prepareData($entity, $updateData); $this->_prepareData($entity, $updateData);
$id = array_combine( $id = array_combine(
$this->_class->getIdentifierFieldNames(), $this->_class->getIdentifierFieldNames(),
$this->_em->getUnitOfWork()->getEntityIdentifier($entity) $this->_em->getUnitOfWork()->getEntityIdentifier($entity)
); );
foreach ($updateData as $tableName => $data) { foreach ($updateData as $tableName => $data) {
$this->_conn->update($tableName, $updateData[$tableName], $id); $this->_conn->update($tableName, $updateData[$tableName], $id);
} }
} }
/** /**
* Deletes an entity. * Deletes an entity.
* *
* @param object $entity The entity to delete. * @param object $entity The entity to delete.
* @override * @override
*/ */
public function delete($entity) public function delete($entity)
{ {
$id = array_combine( $id = array_combine(
$this->_class->getIdentifierFieldNames(), $this->_class->getIdentifierFieldNames(),
$this->_em->getUnitOfWork()->getEntityIdentifier($entity) $this->_em->getUnitOfWork()->getEntityIdentifier($entity)
); );
// If the database platform supports FKs, just // If the database platform supports FKs, just
// delete the row from the root table. Cascades do the rest. // delete the row from the root table. Cascades do the rest.
if ($this->_conn->getDatabasePlatform()->supportsForeignKeyConstraints()) { if ($this->_conn->getDatabasePlatform()->supportsForeignKeyConstraints()) {
$this->_conn->delete($this->_em->getClassMetadata($this->_class->rootEntityName) $this->_conn->delete($this->_em->getClassMetadata($this->_class->rootEntityName)
->primaryTable['name'], $id); ->primaryTable['name'], $id);
} else { } else {
// Delete the parent tables, starting from this class' table up to the root table // Delete the parent tables, starting from this class' table up to the root table
$this->_conn->delete($this->_class->primaryTable['name'], $id); $this->_conn->delete($this->_class->primaryTable['name'], $id);
foreach ($this->_class->parentClasses as $parentClass) { foreach ($this->_class->parentClasses as $parentClass) {
$this->_conn->delete($this->_em->getClassMetadata($parentClass)->primaryTable['name'], $id); $this->_conn->delete($this->_em->getClassMetadata($parentClass)->primaryTable['name'], $id);
} }
} }
} }
/** /**
* Gets the SELECT SQL to select a single entity by a set of field criteria. * Gets the SELECT SQL to select a single entity by a set of field criteria.
* *
* @param array $criteria * @param array $criteria
* @return string The SQL. * @return string The SQL.
* @todo Quote identifier. * @todo Quote identifier.
* @override * @override
*/ */
protected function _getSelectSingleEntitySql(array $criteria) protected function _getSelectSingleEntitySql(array $criteria)
{ {
$tableAliases = array(); $tableAliases = array();
$aliasIndex = 1; $aliasIndex = 1;
$idColumns = $this->_class->getIdentifierColumnNames(); $idColumns = $this->_class->getIdentifierColumnNames();
$baseTableAlias = 't0'; $baseTableAlias = 't0';
foreach (array_merge($this->_class->subClasses, $this->_class->parentClasses) as $className) { foreach (array_merge($this->_class->subClasses, $this->_class->parentClasses) as $className) {
$tableAliases[$className] = 't' . $aliasIndex++; $tableAliases[$className] = 't' . $aliasIndex++;
} }
$columnList = ''; $columnList = '';
foreach ($this->_class->fieldMappings as $fieldName => $mapping) { foreach ($this->_class->fieldMappings as $fieldName => $mapping) {
$tableAlias = isset($mapping['inherited']) ? $tableAlias = isset($mapping['inherited']) ?
$tableAliases[$mapping['inherited']] : $baseTableAlias; $tableAliases[$mapping['inherited']] : $baseTableAlias;
if ($columnList != '') $columnList .= ', '; if ($columnList != '') $columnList .= ', ';
$columnList .= $tableAlias . '.' . $this->_class->columnNames[$fieldName]; $columnList .= $tableAlias . '.' . $this->_class->columnNames[$fieldName];
} }
$sql = 'SELECT ' . $columnList . ' FROM ' . $this->_class->primaryTable['name']. ' ' . $baseTableAlias; $sql = 'SELECT ' . $columnList . ' FROM ' . $this->_class->primaryTable['name']. ' ' . $baseTableAlias;
// INNER JOIN parent tables // INNER JOIN parent tables
foreach ($this->_class->parentClasses as $parentClassName) { foreach ($this->_class->parentClasses as $parentClassName) {
$parentClass = $this->_em->getClassMetadata($parentClassName); $parentClass = $this->_em->getClassMetadata($parentClassName);
$tableAlias = $tableAliases[$parentClassName]; $tableAlias = $tableAliases[$parentClassName];
$sql .= ' INNER JOIN ' . $parentClass->primaryTable['name'] . ' ' . $tableAlias . ' ON '; $sql .= ' INNER JOIN ' . $parentClass->primaryTable['name'] . ' ' . $tableAlias . ' ON ';
$first = true; $first = true;
foreach ($idColumns as $idColumn) { foreach ($idColumns as $idColumn) {
if ($first) $first = false; else $sql .= ' AND '; if ($first) $first = false; else $sql .= ' AND ';
$sql .= $baseTableAlias . '.' . $idColumn . ' = ' . $tableAlias . '.' . $idColumn; $sql .= $baseTableAlias . '.' . $idColumn . ' = ' . $tableAlias . '.' . $idColumn;
} }
} }
// OUTER JOIN sub tables // OUTER JOIN sub tables
foreach ($this->_class->subClasses as $subClassName) { foreach ($this->_class->subClasses as $subClassName) {
$subClass = $this->_em->getClassMetadata($subClassName); $subClass = $this->_em->getClassMetadata($subClassName);
$tableAlias = $tableAliases[$subClassName]; $tableAlias = $tableAliases[$subClassName];
$sql .= ' LEFT JOIN ' . $subClass->primaryTable['name'] . ' ' . $tableAlias . ' ON '; $sql .= ' LEFT JOIN ' . $subClass->primaryTable['name'] . ' ' . $tableAlias . ' ON ';
$first = true; $first = true;
foreach ($idColumns as $idColumn) { foreach ($idColumns as $idColumn) {
if ($first) $first = false; else $sql .= ' AND '; if ($first) $first = false; else $sql .= ' AND ';
$sql .= $baseTableAlias . '.' . $idColumn . ' = ' . $tableAlias . '.' . $idColumn; $sql .= $baseTableAlias . '.' . $idColumn . ' = ' . $tableAlias . '.' . $idColumn;
} }
} }
$conditionSql = ''; $conditionSql = '';
foreach ($criteria as $field => $value) { foreach ($criteria as $field => $value) {
if ($conditionSql != '') $conditionSql .= ' AND '; if ($conditionSql != '') $conditionSql .= ' AND ';
$conditionSql .= $baseTableAlias . '.' . $this->_class->columnNames[$field] . ' = ?'; $conditionSql .= $baseTableAlias . '.' . $this->_class->columnNames[$field] . ' = ?';
} }
return $sql . ' WHERE ' . $conditionSql; return $sql . ' WHERE ' . $conditionSql;
} }
} }
\ No newline at end of file
...@@ -38,348 +38,348 @@ use Doctrine\ORM\Mapping\ClassMetadata; ...@@ -38,348 +38,348 @@ use Doctrine\ORM\Mapping\ClassMetadata;
*/ */
class StandardEntityPersister class StandardEntityPersister
{ {
/** /**
* Metadata object that describes the mapping of the mapped entity class. * Metadata object that describes the mapping of the mapped entity class.
* *
* @var Doctrine\ORM\Mapping\ClassMetadata * @var Doctrine\ORM\Mapping\ClassMetadata
*/ */
protected $_class; protected $_class;
/** /**
* The name of the entity the persister is used for. * The name of the entity the persister is used for.
* *
* @var string * @var string
*/ */
protected $_entityName; protected $_entityName;
/** /**
* The Connection instance. * The Connection instance.
* *
* @var Doctrine\DBAL\Connection $conn * @var Doctrine\DBAL\Connection $conn
*/ */
protected $_conn; protected $_conn;
/** /**
* The EntityManager instance. * The EntityManager instance.
* *
* @var Doctrine\ORM\EntityManager * @var Doctrine\ORM\EntityManager
*/ */
protected $_em; protected $_em;
/** /**
* Queued inserts. * Queued inserts.
* *
* @var array * @var array
*/ */
protected $_queuedInserts = array(); protected $_queuedInserts = array();
/** /**
* Initializes a new instance of a class derived from AbstractEntityPersister * Initializes a new instance of a class derived from AbstractEntityPersister
* that uses the given EntityManager and persists instances of the class described * that uses the given EntityManager and persists instances of the class described
* by the given class metadata descriptor. * by the given class metadata descriptor.
*/ */
public function __construct(EntityManager $em, ClassMetadata $class) public function __construct(EntityManager $em, ClassMetadata $class)
{ {
$this->_em = $em; $this->_em = $em;
$this->_entityName = $class->name; $this->_entityName = $class->name;
$this->_conn = $em->getConnection(); $this->_conn = $em->getConnection();
$this->_class = $class; $this->_class = $class;
} }
/** /**
* Adds an entity to the queued inserts. * Adds an entity to the queued inserts.
* *
* @param object $entity * @param object $entity
*/ */
public function addInsert($entity) public function addInsert($entity)
{ {
$this->_queuedInserts[spl_object_hash($entity)] = $entity; $this->_queuedInserts[spl_object_hash($entity)] = $entity;
} }
/** /**
* Executes all queued inserts. * Executes all queued inserts.
* *
* @return array An array of any generated post-insert IDs. * @return array An array of any generated post-insert IDs.
*/ */
public function executeInserts() public function executeInserts()
{ {
if ( ! $this->_queuedInserts) { if ( ! $this->_queuedInserts) {
return; return;
} }
$postInsertIds = array(); $postInsertIds = array();
$idGen = $this->_class->idGenerator; $idGen = $this->_class->idGenerator;
$isPostInsertId = $idGen->isPostInsertGenerator(); $isPostInsertId = $idGen->isPostInsertGenerator();
$stmt = $this->_conn->prepare($this->_class->insertSql); $stmt = $this->_conn->prepare($this->_class->insertSql);
$primaryTableName = $this->_class->primaryTable['name']; $primaryTableName = $this->_class->primaryTable['name'];
$sqlLogger = $this->_conn->getConfiguration()->getSqlLogger(); $sqlLogger = $this->_conn->getConfiguration()->getSqlLogger();
foreach ($this->_queuedInserts as $entity) { foreach ($this->_queuedInserts as $entity) {
$insertData = array(); $insertData = array();
$this->_prepareData($entity, $insertData, true); $this->_prepareData($entity, $insertData, true);
$paramIndex = 1; $paramIndex = 1;
if ($sqlLogger) { if ($sqlLogger) {
//TODO: Log type //TODO: Log type
$params = array(); $params = array();
foreach ($insertData[$primaryTableName] as $value) { foreach ($insertData[$primaryTableName] as $value) {
$params[$paramIndex] = $value; $params[$paramIndex] = $value;
$stmt->bindValue($paramIndex++, $value/*, Type::getType()*/); $stmt->bindValue($paramIndex++, $value/*, Type::getType()*/);
} }
$sqlLogger->logSql($this->_class->insertSql, $params); $sqlLogger->logSql($this->_class->insertSql, $params);
} else { } else {
foreach ($insertData[$primaryTableName] as $value) { foreach ($insertData[$primaryTableName] as $value) {
$stmt->bindValue($paramIndex++, $value/*, Type::getType()*/); $stmt->bindValue($paramIndex++, $value/*, Type::getType()*/);
} }
} }
$stmt->execute(); $stmt->execute();
if ($isPostInsertId) { if ($isPostInsertId) {
$postInsertIds[$idGen->generate($this->_em, $entity)] = $entity; $postInsertIds[$idGen->generate($this->_em, $entity)] = $entity;
} }
} }
$stmt->closeCursor(); $stmt->closeCursor();
$this->_queuedInserts = array(); $this->_queuedInserts = array();
return $postInsertIds; return $postInsertIds;
} }
/** /**
* Updates an entity. * Updates an entity.
* *
* @param object $entity The entity to update. * @param object $entity The entity to update.
*/ */
public function update($entity) public function update($entity)
{ {
$updateData = array(); $updateData = array();
$this->_prepareData($entity, $updateData); $this->_prepareData($entity, $updateData);
$id = array_combine($this->_class->getIdentifierFieldNames(), $id = array_combine($this->_class->getIdentifierFieldNames(),
$this->_em->getUnitOfWork()->getEntityIdentifier($entity)); $this->_em->getUnitOfWork()->getEntityIdentifier($entity));
$tableName = $this->_class->primaryTable['name']; $tableName = $this->_class->primaryTable['name'];
$this->_conn->update($tableName, $updateData[$tableName], $id); $this->_conn->update($tableName, $updateData[$tableName], $id);
} }
/** /**
* Deletes an entity. * Deletes an entity.
* *
* @param object $entity The entity to delete. * @param object $entity The entity to delete.
*/ */
public function delete($entity) public function delete($entity)
{ {
$id = array_combine( $id = array_combine(
$this->_class->getIdentifierFieldNames(), $this->_class->getIdentifierFieldNames(),
$this->_em->getUnitOfWork()->getEntityIdentifier($entity) $this->_em->getUnitOfWork()->getEntityIdentifier($entity)
); );
$this->_conn->delete($this->_class->primaryTable['name'], $id); $this->_conn->delete($this->_class->primaryTable['name'], $id);
} }
/** /**
* Adds an entity to delete. * Adds an entity to delete.
* *
* @param object $entity * @param object $entity
*/ */
public function addDelete($entity) public function addDelete($entity)
{ {
} }
/** /**
* Executes all pending entity deletions. * Executes all pending entity deletions.
* *
* @see addDelete() * @see addDelete()
*/ */
public function executeDeletions() public function executeDeletions()
{ {
} }
/** /**
* Gets the ClassMetadata instance of the entity class this persister is used for. * Gets the ClassMetadata instance of the entity class this persister is used for.
* *
* @return Doctrine\ORM\Mapping\ClassMetadata * @return Doctrine\ORM\Mapping\ClassMetadata
*/ */
public function getClassMetadata() public function getClassMetadata()
{ {
return $this->_class; return $this->_class;
} }
/** /**
* Gets the table name to use for temporary identifier tables. * Gets the table name to use for temporary identifier tables.
*/ */
public function getTemporaryIdTableName() public function getTemporaryIdTableName()
{ {
//... //...
} }
/** /**
* Prepares the data changeset of an entity for database insertion. * Prepares the data changeset of an entity for database insertion.
* The array that is passed as the second parameter is filled with * The array that is passed as the second parameter is filled with
* <columnName> => <value> pairs, grouped by table name, during this preparation. * <columnName> => <value> pairs, grouped by table name, during this preparation.
* *
* Example: * Example:
* <code> * <code>
* array( * array(
* 'foo_table' => array('column1' => 'value1', 'column2' => 'value2', ...), * 'foo_table' => array('column1' => 'value1', 'column2' => 'value2', ...),
* 'bar_table' => array('columnX' => 'valueX', 'columnY' => 'valueY', ...), * 'bar_table' => array('columnX' => 'valueX', 'columnY' => 'valueY', ...),
* ... * ...
* ) * )
* </code> * </code>
* *
* Notes to inheritors: Be sure to call <code>parent::_prepareData($entity, $result, $isInsert);</code> * Notes to inheritors: Be sure to call <code>parent::_prepareData($entity, $result, $isInsert);</code>
* *
* @param object $entity * @param object $entity
* @param array $result The reference to the data array. * @param array $result The reference to the data array.
* @param boolean $isInsert * @param boolean $isInsert
*/ */
protected function _prepareData($entity, array &$result, $isInsert = false) protected function _prepareData($entity, array &$result, $isInsert = false)
{ {
$platform = $this->_conn->getDatabasePlatform(); $platform = $this->_conn->getDatabasePlatform();
$uow = $this->_em->getUnitOfWork(); $uow = $this->_em->getUnitOfWork();
foreach ($uow->getEntityChangeSet($entity) as $field => $change) { foreach ($uow->getEntityChangeSet($entity) as $field => $change) {
$oldVal = $change[0]; $oldVal = $change[0];
$newVal = $change[1]; $newVal = $change[1];
$columnName = $this->_class->getColumnName($field); $columnName = $this->_class->getColumnName($field);
if (isset($this->_class->associationMappings[$field])) { if (isset($this->_class->associationMappings[$field])) {
$assocMapping = $this->_class->associationMappings[$field]; $assocMapping = $this->_class->associationMappings[$field];
// Only owning side of x-1 associations can have a FK column. // Only owning side of x-1 associations can have a FK column.
if ( ! $assocMapping->isOneToOne() || $assocMapping->isInverseSide()) { if ( ! $assocMapping->isOneToOne() || $assocMapping->isInverseSide()) {
continue; continue;
} }
// Special case: One-one self-referencing of the same class. // Special case: One-one self-referencing of the same class.
if ($newVal !== null && $assocMapping->sourceEntityName == $assocMapping->targetEntityName) { if ($newVal !== null && $assocMapping->sourceEntityName == $assocMapping->targetEntityName) {
$oid = spl_object_hash($newVal); $oid = spl_object_hash($newVal);
$isScheduledForInsert = $uow->isRegisteredNew($newVal); $isScheduledForInsert = $uow->isRegisteredNew($newVal);
if (isset($this->_queuedInserts[$oid]) || $isScheduledForInsert) { if (isset($this->_queuedInserts[$oid]) || $isScheduledForInsert) {
// The associated entity $newVal is not yet persisted, so we must // The associated entity $newVal is not yet persisted, so we must
// set $newVal = null, in order to insert a null value and update later. // set $newVal = null, in order to insert a null value and update later.
$newVal = null; $newVal = null;
} else if ($isInsert && ! $isScheduledForInsert && $uow->getEntityState($newVal) == UnitOfWork::STATE_MANAGED) { } else if ($isInsert && ! $isScheduledForInsert && $uow->getEntityState($newVal) == UnitOfWork::STATE_MANAGED) {
// $newVal is already fully persisted // $newVal is already fully persisted
// Clear changeset of $newVal, so that only the identifier is updated. // Clear changeset of $newVal, so that only the identifier is updated.
// Not sure this is really rock-solid here but it seems to work. // Not sure this is really rock-solid here but it seems to work.
$uow->clearEntityChangeSet($oid); $uow->clearEntityChangeSet($oid);
$uow->propertyChanged($newVal, $field, $entity, $entity); $uow->propertyChanged($newVal, $field, $entity, $entity);
} }
} }
foreach ($assocMapping->sourceToTargetKeyColumns as $sourceColumn => $targetColumn) { foreach ($assocMapping->sourceToTargetKeyColumns as $sourceColumn => $targetColumn) {
$otherClass = $this->_em->getClassMetadata($assocMapping->targetEntityName); $otherClass = $this->_em->getClassMetadata($assocMapping->targetEntityName);
if ($newVal === null) { if ($newVal === null) {
$result[$this->getOwningTable($field)][$sourceColumn] = null; $result[$this->getOwningTable($field)][$sourceColumn] = null;
} else { } else {
$result[$this->getOwningTable($field)][$sourceColumn] = $result[$this->getOwningTable($field)][$sourceColumn] =
$otherClass->reflFields[$otherClass->fieldNames[$targetColumn]]->getValue($newVal); $otherClass->reflFields[$otherClass->fieldNames[$targetColumn]]->getValue($newVal);
} }
} }
} else if ($newVal === null) { } else if ($newVal === null) {
$result[$this->getOwningTable($field)][$columnName] = null; $result[$this->getOwningTable($field)][$columnName] = null;
} else { } else {
$result[$this->getOwningTable($field)][$columnName] = Type::getType( $result[$this->getOwningTable($field)][$columnName] = Type::getType(
$this->_class->fieldMappings[$field]['type']) $this->_class->fieldMappings[$field]['type'])
->convertToDatabaseValue($newVal, $platform); ->convertToDatabaseValue($newVal, $platform);
} }
} }
} }
/** /**
* Gets the name of the table that owns the column the given field is mapped to. * Gets the name of the table that owns the column the given field is mapped to.
* *
* @param string $fieldName * @param string $fieldName
* @return string * @return string
*/ */
public function getOwningTable($fieldName) public function getOwningTable($fieldName)
{ {
return $this->_class->primaryTable['name']; return $this->_class->primaryTable['name'];
} }
/** /**
* Loads an entity by a list of field criteria. * Loads an entity by a list of field criteria.
* *
* @param array $criteria The criteria by which to load the entity. * @param array $criteria The criteria by which to load the entity.
* @param object $entity The entity to load the data into. If not specified, * @param object $entity The entity to load the data into. If not specified,
* a new entity is created. * a new entity is created.
*/ */
public function load(array $criteria, $entity = null) public function load(array $criteria, $entity = null)
{ {
$stmt = $this->_conn->prepare($this->_getSelectSingleEntitySql($criteria)); $stmt = $this->_conn->prepare($this->_getSelectSingleEntitySql($criteria));
$stmt->execute(array_values($criteria)); $stmt->execute(array_values($criteria));
$data = array(); $data = array();
foreach ($stmt->fetch(\PDO::FETCH_ASSOC) as $column => $value) { foreach ($stmt->fetch(\PDO::FETCH_ASSOC) as $column => $value) {
$fieldName = $this->_class->fieldNames[$column]; $fieldName = $this->_class->fieldNames[$column];
$data[$fieldName] = Type::getType($this->_class->getTypeOfField($fieldName)) $data[$fieldName] = Type::getType($this->_class->getTypeOfField($fieldName))
->convertToPHPValue($value); ->convertToPHPValue($value);
} }
$stmt->closeCursor(); $stmt->closeCursor();
if ($entity === null) { if ($entity === null) {
$entity = $this->_em->getUnitOfWork()->createEntity($this->_entityName, $data); $entity = $this->_em->getUnitOfWork()->createEntity($this->_entityName, $data);
} else { } else {
foreach ($data as $field => $value) { foreach ($data as $field => $value) {
$this->_class->reflFields[$field]->setValue($entity, $value); $this->_class->reflFields[$field]->setValue($entity, $value);
} }
$id = array(); $id = array();
if ($this->_class->isIdentifierComposite) { if ($this->_class->isIdentifierComposite) {
foreach ($this->_class->identifier as $fieldName) { foreach ($this->_class->identifier as $fieldName) {
$id[] = $data[$fieldName]; $id[] = $data[$fieldName];
} }
} else { } else {
$id = array($data[$this->_class->getSingleIdentifierFieldName()]); $id = array($data[$this->_class->getSingleIdentifierFieldName()]);
} }
$this->_em->getUnitOfWork()->registerManaged($entity, $id, $data); $this->_em->getUnitOfWork()->registerManaged($entity, $id, $data);
} }
if ( ! $this->_em->getConfiguration()->getAllowPartialObjects()) { if ( ! $this->_em->getConfiguration()->getAllowPartialObjects()) {
foreach ($this->_class->associationMappings as $field => $assoc) { foreach ($this->_class->associationMappings as $field => $assoc) {
if ($assoc->isOneToOne()) { if ($assoc->isOneToOne()) {
if ($assoc->isLazilyFetched) { if ($assoc->isLazilyFetched) {
// Inject proxy // Inject proxy
$proxy = $this->_em->getProxyGenerator()->getAssociationProxy($entity, $assoc); $proxy = $this->_em->getProxyGenerator()->getAssociationProxy($entity, $assoc);
$this->_class->reflFields[$field]->setValue($entity, $proxy); $this->_class->reflFields[$field]->setValue($entity, $proxy);
} else { } else {
//TODO: Eager fetch? //TODO: Eager fetch?
} }
} else { } else {
// Inject collection // Inject collection
$this->_class->reflFields[$field]->setValue( $this->_class->reflFields[$field]->setValue(
$entity, new PersistentCollection($this->_em, $entity, new PersistentCollection($this->_em,
$this->_em->getClassMetadata($assoc->targetEntityName) $this->_em->getClassMetadata($assoc->targetEntityName)
)); ));
} }
} }
} }
return $entity; return $entity;
} }
/** /**
* Gets the SELECT SQL to select a single entity by a set of field criteria. * Gets the SELECT SQL to select a single entity by a set of field criteria.
* *
* @param array $criteria * @param array $criteria
* @return string The SQL. * @return string The SQL.
* @todo Quote identifier. * @todo Quote identifier.
*/ */
protected function _getSelectSingleEntitySql(array $criteria) protected function _getSelectSingleEntitySql(array $criteria)
{ {
$columnList = ''; $columnList = '';
foreach ($this->_class->columnNames as $column) { foreach ($this->_class->columnNames as $column) {
if ($columnList != '') $columnList .= ', '; if ($columnList != '') $columnList .= ', ';
$columnList .= $column; $columnList .= $column;
} }
$conditionSql = ''; $conditionSql = '';
foreach ($criteria as $field => $value) { foreach ($criteria as $field => $value) {
if ($conditionSql != '') $conditionSql .= ' AND '; if ($conditionSql != '') $conditionSql .= ' AND ';
$conditionSql .= $this->_class->columnNames[$field] . ' = ?'; $conditionSql .= $this->_class->columnNames[$field] . ' = ?';
} }
return 'SELECT ' . $columnList . ' FROM ' . $this->_class->getTableName() return 'SELECT ' . $columnList . ' FROM ' . $this->_class->getTableName()
. ' WHERE ' . $conditionSql; . ' WHERE ' . $conditionSql;
} }
} }
\ No newline at end of file
...@@ -28,12 +28,12 @@ namespace Doctrine\ORM\Query\AST; ...@@ -28,12 +28,12 @@ namespace Doctrine\ORM\Query\AST;
*/ */
class StateFieldPathExpression extends Node class StateFieldPathExpression extends Node
{ {
//const TYPE_COLLECTION_VALUED_ASSOCIATION = 1; //const TYPE_COLLECTION_VALUED_ASSOCIATION = 1;
//const TYPE_SINGLE_VALUED_ASSOCIATION = 2; //const TYPE_SINGLE_VALUED_ASSOCIATION = 2;
//const TYPE_STATE_FIELD = 3; //const TYPE_STATE_FIELD = 3;
//private $_type; //private $_type;
private $_parts; private $_parts;
// Information that is attached during semantical analysis. // Information that is attached during semantical analysis.
private $_isSimpleStateFieldPathExpression = false; private $_isSimpleStateFieldPathExpression = false;
......
...@@ -884,7 +884,7 @@ class Parser ...@@ -884,7 +884,7 @@ class Parser
while ($this->_lexer->isNextToken('.')) { while ($this->_lexer->isNextToken('.')) {
if ($stateFieldSeen) { if ($stateFieldSeen) {
$this->syntaxError(); $this->syntaxError();
} }
$this->match('.'); $this->match('.');
$part = $this->_IdentificationVariable(); $part = $this->_IdentificationVariable();
...@@ -1098,7 +1098,7 @@ class Parser ...@@ -1098,7 +1098,7 @@ class Parser
{ {
$condPrimary = new AST\ConditionalPrimary; $condPrimary = new AST\ConditionalPrimary;
if ($this->_lexer->isNextToken('(')) { if ($this->_lexer->isNextToken('(')) {
// Peek beyond the matching closing paranthesis ')' // Peek beyond the matching closing paranthesis ')'
$numUnmatched = 1; $numUnmatched = 1;
$peek = $this->_lexer->peek(); $peek = $this->_lexer->peek();
while ($numUnmatched > 0) { while ($numUnmatched > 0) {
...@@ -1276,8 +1276,8 @@ class Parser ...@@ -1276,8 +1276,8 @@ class Parser
*/ */
private function _isNextAllAnySome() private function _isNextAllAnySome()
{ {
return $this->_lexer->lookahead['type'] === Lexer::T_ALL || return $this->_lexer->lookahead['type'] === Lexer::T_ALL ||
$this->_lexer->lookahead['type'] === Lexer::T_ANY || $this->_lexer->lookahead['type'] === Lexer::T_ANY ||
$this->_lexer->lookahead['type'] === Lexer::T_SOME; $this->_lexer->lookahead['type'] === Lexer::T_SOME;
} }
...@@ -1499,7 +1499,7 @@ class Parser ...@@ -1499,7 +1499,7 @@ class Parser
} }
/** /**
* Subselect ::= SimpleSelectClause SubselectFromClause [WhereClause] [GroupByClause] [HavingClause] [OrderByClause] * Subselect ::= SimpleSelectClause SubselectFromClause [WhereClause] [GroupByClause] [HavingClause] [OrderByClause]
*/ */
public function _Subselect() public function _Subselect()
{ {
......
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