From b998a522b030c154eac161ba7effd99133a09c73 Mon Sep 17 00:00:00 2001 From: "Fabio B. Silva" Date: Sun, 22 Jul 2012 13:42:43 -0300 Subject: [PATCH 01/16] remove '_' prefix at BasicEntityPersister --- .../AbstractEntityInheritancePersister.php | 22 +- .../ORM/Persisters/BasicEntityPersister.php | 560 +++++++++--------- .../Persisters/JoinedSubclassPersister.php | 236 ++++---- .../ORM/Persisters/SingleTablePersister.php | 74 +-- .../Tests/Mocks/EntityPersisterMock.php | 54 +- .../BasicEntityPersisterTypeValueSqlTest.php | 6 +- 6 files changed, 477 insertions(+), 475 deletions(-) diff --git a/lib/Doctrine/ORM/Persisters/AbstractEntityInheritancePersister.php b/lib/Doctrine/ORM/Persisters/AbstractEntityInheritancePersister.php index 119160843..e12cb3d4b 100644 --- a/lib/Doctrine/ORM/Persisters/AbstractEntityInheritancePersister.php +++ b/lib/Doctrine/ORM/Persisters/AbstractEntityInheritancePersister.php @@ -36,14 +36,14 @@ abstract class AbstractEntityInheritancePersister extends BasicEntityPersister /** * {@inheritdoc} */ - protected function _prepareInsertData($entity) + protected function prepareInsertData($entity) { - $data = parent::_prepareInsertData($entity); + $data = parent::prepareInsertData($entity); // Populate the discriminator column - $discColumn = $this->_class->discriminatorColumn; - $this->_columnTypes[$discColumn['name']] = $discColumn['type']; - $data[$this->_getDiscriminatorColumnTableName()][$discColumn['name']] = $this->_class->discriminatorValue; + $discColumn = $this->class->discriminatorColumn; + $this->columnTypes[$discColumn['name']] = $discColumn['type']; + $data[$this->getDiscriminatorColumnTableName()][$discColumn['name']] = $this->class->discriminatorValue; return $data; } @@ -53,21 +53,21 @@ abstract class AbstractEntityInheritancePersister extends BasicEntityPersister * * @return string The table name. */ - abstract protected function _getDiscriminatorColumnTableName(); + abstract protected function getDiscriminatorColumnTableName(); /** * {@inheritdoc} */ - protected function _getSelectColumnSQL($field, ClassMetadata $class, $alias = 'r') + protected function getSelectColumnSQL($field, ClassMetadata $class, $alias = 'r') { $columnName = $class->columnNames[$field]; - $sql = $this->_getSQLTableAlias($class->name, $alias == 'r' ? '' : $alias) . '.' . $this->quoteStrategy->getColumnName($field, $class, $this->_platform); + $sql = $this->getSQLTableAlias($class->name, $alias == 'r' ? '' : $alias) . '.' . $this->quoteStrategy->getColumnName($field, $class, $this->platform); $columnAlias = $this->getSQLColumnAlias($columnName); - $this->_rsm->addFieldResult($alias, $columnAlias, $field, $class->name); + $this->rsm->addFieldResult($alias, $columnAlias, $field, $class->name); if (isset($class->fieldMappings[$field]['requireSQLConversion'])) { $type = Type::getType($class->getTypeOfField($field)); - $sql = $type->convertToPHPValueSQL($sql, $this->_platform); + $sql = $type->convertToPHPValueSQL($sql, $this->platform); } return $sql . ' AS ' . $columnAlias; @@ -76,7 +76,7 @@ abstract class AbstractEntityInheritancePersister extends BasicEntityPersister protected function getSelectJoinColumnSQL($tableAlias, $joinColumnName, $className) { $columnAlias = $this->getSQLColumnAlias($joinColumnName); - $this->_rsm->addMetaResult('r', $columnAlias, $joinColumnName); + $this->rsm->addMetaResult('r', $columnAlias, $joinColumnName); return $tableAlias . '.' . $joinColumnName . ' AS ' . $columnAlias; } diff --git a/lib/Doctrine/ORM/Persisters/BasicEntityPersister.php b/lib/Doctrine/ORM/Persisters/BasicEntityPersister.php index 28cac813e..bcefb18cc 100644 --- a/lib/Doctrine/ORM/Persisters/BasicEntityPersister.php +++ b/lib/Doctrine/ORM/Persisters/BasicEntityPersister.php @@ -75,6 +75,7 @@ use Doctrine\Common\Collections\Expr\Comparison; * @author Giorgio Sironi * @author Benjamin Eberlei * @author Alexander + * @author Fabio B. Silva * @since 2.0 */ class BasicEntityPersister @@ -99,35 +100,35 @@ class BasicEntityPersister * * @var \Doctrine\ORM\Mapping\ClassMetadata */ - protected $_class; + protected $class; /** * The underlying DBAL Connection of the used EntityManager. * * @var \Doctrine\DBAL\Connection $conn */ - protected $_conn; + protected $conn; /** * The database platform. * * @var \Doctrine\DBAL\Platforms\AbstractPlatform */ - protected $_platform; + protected $platform; /** * The EntityManager instance. * * @var \Doctrine\ORM\EntityManager */ - protected $_em; + protected $em; /** * Queued inserts. * * @var array */ - protected $_queuedInserts = array(); + protected $queuedInserts = array(); /** * ResultSetMapping that is used for all queries. Is generated lazily once per request. @@ -136,24 +137,24 @@ class BasicEntityPersister * * @var Query\ResultSetMapping */ - protected $_rsm; + protected $rsm; /** * The map of column names to DBAL mapping types of all prepared columns used * when INSERTing or UPDATEing an entity. * * @var array - * @see _prepareInsertData($entity) - * @see _prepareUpdateData($entity) + * @see prepareInsertData($entity) + * @see prepareUpdateData($entity) */ - protected $_columnTypes = array(); + protected $columnTypes = array(); /** * The map of quoted column names. * * @var array - * @see _prepareInsertData($entity) - * @see _prepareUpdateData($entity) + * @see prepareInsertData($entity) + * @see prepareUpdateData($entity) */ protected $quotedColumns = array(); @@ -163,7 +164,7 @@ class BasicEntityPersister * * @var string */ - private $_insertSql; + private $insertSql; /** * The SELECT column list SQL fragment used for querying entities by this persister. @@ -171,7 +172,7 @@ class BasicEntityPersister * * @var string */ - protected $_selectColumnListSql; + protected $selectColumnListSql; /** * The JOIN SQL fragement used to eagerly load all many-to-one and one-to-one @@ -179,21 +180,21 @@ class BasicEntityPersister * * @var string */ - protected $_selectJoinSql; + protected $selectJoinSql; /** * Counter for creating unique SQL table and column aliases. * * @var integer */ - protected $_sqlAliasCounter = 0; + protected $sqlAliasCounter = 0; /** * Map from class names (FQCN) to the corresponding generated SQL table aliases. * * @var array */ - protected $_sqlTableAliases = array(); + protected $sqlTableAliases = array(); /** * The quote strategy. @@ -211,10 +212,10 @@ class BasicEntityPersister */ public function __construct(EntityManager $em, ClassMetadata $class) { - $this->_em = $em; - $this->_class = $class; - $this->_conn = $em->getConnection(); - $this->_platform = $this->_conn->getDatabasePlatform(); + $this->em = $em; + $this->class = $class; + $this->conn = $em->getConnection(); + $this->platform = $this->conn->getDatabasePlatform(); $this->quoteStrategy = $em->getConfiguration()->getQuoteStrategy(); } @@ -223,7 +224,7 @@ class BasicEntityPersister */ public function getClassMetadata() { - return $this->_class; + return $this->class; } /** @@ -234,7 +235,7 @@ class BasicEntityPersister */ public function addInsert($entity) { - $this->_queuedInserts[spl_object_hash($entity)] = $entity; + $this->queuedInserts[spl_object_hash($entity)] = $entity; } /** @@ -248,44 +249,44 @@ class BasicEntityPersister */ public function executeInserts() { - if ( ! $this->_queuedInserts) { + if ( ! $this->queuedInserts) { return; } $postInsertIds = array(); - $idGen = $this->_class->idGenerator; + $idGen = $this->class->idGenerator; $isPostInsertId = $idGen->isPostInsertGenerator(); - $stmt = $this->_conn->prepare($this->_getInsertSQL()); - $tableName = $this->_class->getTableName(); + $stmt = $this->conn->prepare($this->getInsertSQL()); + $tableName = $this->class->getTableName(); - foreach ($this->_queuedInserts as $entity) { - $insertData = $this->_prepareInsertData($entity); + foreach ($this->queuedInserts as $entity) { + $insertData = $this->prepareInsertData($entity); if (isset($insertData[$tableName])) { $paramIndex = 1; foreach ($insertData[$tableName] as $column => $value) { - $stmt->bindValue($paramIndex++, $value, $this->_columnTypes[$column]); + $stmt->bindValue($paramIndex++, $value, $this->columnTypes[$column]); } } $stmt->execute(); if ($isPostInsertId) { - $id = $idGen->generate($this->_em, $entity); + $id = $idGen->generate($this->em, $entity); $postInsertIds[$id] = $entity; } else { - $id = $this->_class->getIdentifierValues($entity); + $id = $this->class->getIdentifierValues($entity); } - if ($this->_class->isVersioned) { + if ($this->class->isVersioned) { $this->assignDefaultVersionValue($entity, $id); } } $stmt->closeCursor(); - $this->_queuedInserts = array(); + $this->queuedInserts = array(); return $postInsertIds; } @@ -300,8 +301,8 @@ class BasicEntityPersister */ protected function assignDefaultVersionValue($entity, $id) { - $value = $this->fetchVersionValue($this->_class, $id); - $this->_class->setFieldValue($entity, $this->_class->versionField, $value); + $value = $this->fetchVersionValue($this->class, $id); + $this->class->setFieldValue($entity, $this->class->versionField, $value); } /** @@ -314,46 +315,46 @@ class BasicEntityPersister protected function fetchVersionValue($versionedClass, $id) { $versionField = $versionedClass->versionField; - $identifier = $this->quoteStrategy->getIdentifierColumnNames($versionedClass, $this->_platform); + $identifier = $this->quoteStrategy->getIdentifierColumnNames($versionedClass, $this->platform); - $versionFieldColumnName = $this->quoteStrategy->getColumnName($versionField, $versionedClass, $this->_platform); + $versionFieldColumnName = $this->quoteStrategy->getColumnName($versionField, $versionedClass, $this->platform); //FIXME: Order with composite keys might not be correct $sql = 'SELECT ' . $versionFieldColumnName - . ' FROM ' . $this->quoteStrategy->getTableName($versionedClass, $this->_platform) + . ' FROM ' . $this->quoteStrategy->getTableName($versionedClass, $this->platform) . ' WHERE ' . implode(' = ? AND ', $identifier) . ' = ?'; - $value = $this->_conn->fetchColumn($sql, array_values((array)$id)); + $value = $this->conn->fetchColumn($sql, array_values((array)$id)); - return Type::getType($versionedClass->fieldMappings[$versionField]['type'])->convertToPHPValue($value, $this->_platform); + return Type::getType($versionedClass->fieldMappings[$versionField]['type'])->convertToPHPValue($value, $this->platform); } /** * Updates a managed entity. The entity is updated according to its current changeset * in the running UnitOfWork. If there is no changeset, nothing is updated. * - * The data to update is retrieved through {@link _prepareUpdateData}. + * The data to update is retrieved through {@link prepareUpdateData}. * Subclasses that override this method are supposed to obtain the update data - * in the same way, through {@link _prepareUpdateData}. + * in the same way, through {@link prepareUpdateData}. * * Subclasses are also supposed to take care of versioning when overriding this method, - * if necessary. The {@link _updateTable} method can be used to apply the data retrieved + * if necessary. The {@link updateTable} method can be used to apply the data retrieved * from {@_prepareUpdateData} on the target tables, thereby optionally applying versioning. * * @param object $entity The entity to update. */ public function update($entity) { - $updateData = $this->_prepareUpdateData($entity); - $tableName = $this->_class->getTableName(); + $updateData = $this->prepareUpdateData($entity); + $tableName = $this->class->getTableName(); if (isset($updateData[$tableName]) && $updateData[$tableName]) { - $this->_updateTable( - $entity, $this->quoteStrategy->getTableName($this->_class, $this->_platform), - $updateData[$tableName], $this->_class->isVersioned + $this->updateTable( + $entity, $this->quoteStrategy->getTableName($this->class, $this->platform), + $updateData[$tableName], $this->class->isVersioned ); - if ($this->_class->isVersioned) { - $id = $this->_em->getUnitOfWork()->getEntityIdentifier($entity); + if ($this->class->isVersioned) { + $id = $this->em->getUnitOfWork()->getEntityIdentifier($entity); $this->assignDefaultVersionValue($entity, $id); } } @@ -368,7 +369,7 @@ class BasicEntityPersister * @param array $updateData The map of columns to update (column => value). * @param boolean $versioned Whether the UPDATE should be versioned. */ - protected final function _updateTable($entity, $quotedTableName, array $updateData, $versioned = false) + protected final function updateTable($entity, $quotedTableName, array $updateData, $versioned = false) { $set = $params = $types = array(); @@ -376,12 +377,12 @@ class BasicEntityPersister $column = $columnName; $placeholder = '?'; - if (isset($this->_class->fieldNames[$columnName])) { - $column = $this->quoteStrategy->getColumnName($this->_class->fieldNames[$columnName], $this->_class, $this->_platform); + if (isset($this->class->fieldNames[$columnName])) { + $column = $this->quoteStrategy->getColumnName($this->class->fieldNames[$columnName], $this->class, $this->platform); - if (isset($this->_class->fieldMappings[$this->_class->fieldNames[$columnName]]['requireSQLConversion'])) { - $type = Type::getType($this->_columnTypes[$columnName]); - $placeholder = $type->convertToDatabaseValueSQL('?', $this->_platform); + if (isset($this->class->fieldMappings[$this->class->fieldNames[$columnName]]['requireSQLConversion'])) { + $type = Type::getType($this->columnTypes[$columnName]); + $placeholder = $type->convertToDatabaseValueSQL('?', $this->platform); } } else if (isset($this->quotedColumns[$columnName])) { $column = $this->quotedColumns[$columnName]; @@ -389,16 +390,16 @@ class BasicEntityPersister $set[] = $column . ' = ' . $placeholder; $params[] = $value; - $types[] = $this->_columnTypes[$columnName]; + $types[] = $this->columnTypes[$columnName]; } $where = array(); - $id = $this->_em->getUnitOfWork()->getEntityIdentifier($entity); + $id = $this->em->getUnitOfWork()->getEntityIdentifier($entity); - foreach ($this->_class->identifier as $idField) { - if (isset($this->_class->associationMappings[$idField])) { - $targetMapping = $this->_em->getClassMetadata($this->_class->associationMappings[$idField]['targetEntity']); - $where[] = $this->_class->associationMappings[$idField]['joinColumns'][0]['name']; + foreach ($this->class->identifier as $idField) { + if (isset($this->class->associationMappings[$idField])) { + $targetMapping = $this->em->getClassMetadata($this->class->associationMappings[$idField]['targetEntity']); + $where[] = $this->class->associationMappings[$idField]['joinColumns'][0]['name']; $params[] = $id[$idField]; switch (true) { @@ -414,16 +415,16 @@ class BasicEntityPersister throw ORMException::unrecognizedField($targetMapping->identifier[0]); } } else { - $where[] = $this->quoteStrategy->getColumnName($idField, $this->_class, $this->_platform); + $where[] = $this->quoteStrategy->getColumnName($idField, $this->class, $this->platform); $params[] = $id[$idField]; - $types[] = $this->_class->fieldMappings[$idField]['type']; + $types[] = $this->class->fieldMappings[$idField]['type']; } } if ($versioned) { - $versionField = $this->_class->versionField; - $versionFieldType = $this->_class->fieldMappings[$versionField]['type']; - $versionColumn = $this->quoteStrategy->getColumnName($versionField, $this->_class, $this->_platform); + $versionField = $this->class->versionField; + $versionFieldType = $this->class->fieldMappings[$versionField]['type']; + $versionColumn = $this->quoteStrategy->getColumnName($versionField, $this->class, $this->platform); if ($versionFieldType == Type::INTEGER) { $set[] = $versionColumn . ' = ' . $versionColumn . ' + 1'; @@ -432,15 +433,15 @@ class BasicEntityPersister } $where[] = $versionColumn; - $params[] = $this->_class->reflFields[$versionField]->getValue($entity); - $types[] = $this->_class->fieldMappings[$versionField]['type']; + $params[] = $this->class->reflFields[$versionField]->getValue($entity); + $types[] = $this->class->fieldMappings[$versionField]['type']; } $sql = 'UPDATE ' . $quotedTableName . ' SET ' . implode(', ', $set) . ' WHERE ' . implode(' = ? AND ', $where) . ' = ?'; - $result = $this->_conn->executeUpdate($sql, $params, $types); + $result = $this->conn->executeUpdate($sql, $params, $types); if ($versioned && ! $result) { throw OptimisticLockException::lockFailed($entity); @@ -454,7 +455,7 @@ class BasicEntityPersister */ protected function deleteJoinTableRecords($identifier) { - foreach ($this->_class->associationMappings as $mapping) { + foreach ($this->class->associationMappings as $mapping) { if ($mapping['type'] == ClassMetadata::MANY_TO_MANY) { // @Todo this only covers scenarios with no inheritance or of the same level. Is there something // like self-referential relationship between different levels of an inheritance hierachy? I hope not! @@ -463,39 +464,39 @@ class BasicEntityPersister $keys = array(); if ( ! $mapping['isOwningSide']) { - $relatedClass = $this->_em->getClassMetadata($mapping['targetEntity']); + $relatedClass = $this->em->getClassMetadata($mapping['targetEntity']); $mapping = $relatedClass->associationMappings[$mapping['mappedBy']]; foreach ($mapping['joinTable']['inverseJoinColumns'] as $joinColumn) { - $keys[] = $this->quoteStrategy->getJoinColumnName($joinColumn, $relatedClass, $this->_platform); + $keys[] = $this->quoteStrategy->getJoinColumnName($joinColumn, $relatedClass, $this->platform); } if ($selfReferential) { foreach ($mapping['joinTable']['joinColumns'] as $joinColumn) { - $otherKeys[] = $this->quoteStrategy->getJoinColumnName($joinColumn, $relatedClass, $this->_platform); + $otherKeys[] = $this->quoteStrategy->getJoinColumnName($joinColumn, $relatedClass, $this->platform); } } } else { foreach ($mapping['joinTable']['joinColumns'] as $joinColumn) { - $keys[] = $this->quoteStrategy->getJoinColumnName($joinColumn, $this->_class, $this->_platform); + $keys[] = $this->quoteStrategy->getJoinColumnName($joinColumn, $this->class, $this->platform); } if ($selfReferential) { foreach ($mapping['joinTable']['inverseJoinColumns'] as $joinColumn) { - $otherKeys[] = $this->quoteStrategy->getJoinColumnName($joinColumn, $this->_class, $this->_platform); + $otherKeys[] = $this->quoteStrategy->getJoinColumnName($joinColumn, $this->class, $this->platform); } } } if ( ! isset($mapping['isOnDeleteCascade'])) { - $joinTableName = $this->quoteStrategy->getJoinTableName($mapping, $this->_class, $this->_platform); + $joinTableName = $this->quoteStrategy->getJoinTableName($mapping, $this->class, $this->platform); - $this->_conn->delete($joinTableName, array_combine($keys, $identifier)); + $this->conn->delete($joinTableName, array_combine($keys, $identifier)); if ($selfReferential) { - $this->_conn->delete($joinTableName, array_combine($otherKeys, $identifier)); + $this->conn->delete($joinTableName, array_combine($otherKeys, $identifier)); } } } @@ -514,13 +515,13 @@ class BasicEntityPersister */ public function delete($entity) { - $identifier = $this->_em->getUnitOfWork()->getEntityIdentifier($entity); + $identifier = $this->em->getUnitOfWork()->getEntityIdentifier($entity); $this->deleteJoinTableRecords($identifier); - $id = array_combine($this->quoteStrategy->getIdentifierColumnNames($this->_class, $this->_platform), $identifier); + $id = array_combine($this->quoteStrategy->getIdentifierColumnNames($this->class, $this->platform), $identifier); - $this->_conn->delete($this->quoteStrategy->getTableName($this->_class, $this->_platform), $id); + $this->conn->delete($this->quoteStrategy->getTableName($this->class, $this->platform), $id); } /** @@ -543,13 +544,13 @@ class BasicEntityPersister * @param object $entity The entity for which to prepare the data. * @return array The prepared data. */ - protected function _prepareUpdateData($entity) + protected function prepareUpdateData($entity) { $result = array(); - $uow = $this->_em->getUnitOfWork(); + $uow = $this->em->getUnitOfWork(); - if (($versioned = $this->_class->isVersioned) != false) { - $versionField = $this->_class->versionField; + if (($versioned = $this->class->isVersioned) != false) { + $versionField = $this->class->versionField; } foreach ($uow->getEntityChangeSet($entity) as $field => $change) { @@ -559,8 +560,8 @@ class BasicEntityPersister $newVal = $change[1]; - if (isset($this->_class->associationMappings[$field])) { - $assoc = $this->_class->associationMappings[$field]; + if (isset($this->class->associationMappings[$field])) { + $assoc = $this->class->associationMappings[$field]; // Only owning side of x-1 associations can have a FK column. if ( ! $assoc['isOwningSide'] || ! ($assoc['type'] & ClassMetadata::TO_ONE)) { @@ -570,7 +571,7 @@ class BasicEntityPersister if ($newVal !== null) { $oid = spl_object_hash($newVal); - if (isset($this->_queuedInserts[$oid]) || $uow->isScheduledForInsert($newVal)) { + if (isset($this->queuedInserts[$oid]) || $uow->isScheduledForInsert($newVal)) { // The associated entity $newVal is not yet persisted, so we must // set $newVal = null, in order to insert a null value and schedule an // extra update on the UnitOfWork. @@ -585,13 +586,13 @@ class BasicEntityPersister $newValId = $uow->getEntityIdentifier($newVal); } - $targetClass = $this->_em->getClassMetadata($assoc['targetEntity']); + $targetClass = $this->em->getClassMetadata($assoc['targetEntity']); $owningTable = $this->getOwningTable($field); foreach ($assoc['joinColumns'] as $joinColumn) { $sourceColumn = $joinColumn['name']; $targetColumn = $joinColumn['referencedColumnName']; - $quotedColumn = $this->quoteStrategy->getJoinColumnName($joinColumn, $this->_class, $this->_platform); + $quotedColumn = $this->quoteStrategy->getJoinColumnName($joinColumn, $this->class, $this->platform); $this->quotedColumns[$sourceColumn] = $quotedColumn; @@ -603,11 +604,11 @@ class BasicEntityPersister $result[$owningTable][$sourceColumn] = $newValId[$targetClass->fieldNames[$targetColumn]]; } - $this->_columnTypes[$sourceColumn] = $targetClass->getTypeOfColumn($targetColumn); + $this->columnTypes[$sourceColumn] = $targetClass->getTypeOfColumn($targetColumn); } } else { - $columnName = $this->_class->columnNames[$field]; - $this->_columnTypes[$columnName] = $this->_class->fieldMappings[$field]['type']; + $columnName = $this->class->columnNames[$field]; + $this->columnTypes[$columnName] = $this->class->fieldMappings[$field]['type']; $result[$this->getOwningTable($field)][$columnName] = $newVal; } } @@ -623,11 +624,11 @@ class BasicEntityPersister * * @param object $entity The entity for which to prepare the data. * @return array The prepared data for the tables to update. - * @see _prepareUpdateData + * @see prepareUpdateData */ - protected function _prepareInsertData($entity) + protected function prepareInsertData($entity) { - return $this->_prepareUpdateData($entity); + return $this->prepareUpdateData($entity); } /** @@ -642,7 +643,7 @@ class BasicEntityPersister */ public function getOwningTable($fieldName) { - return $this->_class->getTableName(); + return $this->class->getTableName(); } /** @@ -661,17 +662,17 @@ class BasicEntityPersister */ public function load(array $criteria, $entity = null, $assoc = null, array $hints = array(), $lockMode = 0, $limit = null, array $orderBy = null) { - $sql = $this->_getSelectEntitiesSQL($criteria, $assoc, $lockMode, $limit, null, $orderBy); + $sql = $this->getSelectEntitiesSQL($criteria, $assoc, $lockMode, $limit, null, $orderBy); list($params, $types) = $this->expandParameters($criteria); - $stmt = $this->_conn->executeQuery($sql, $params, $types); + $stmt = $this->conn->executeQuery($sql, $params, $types); if ($entity !== null) { $hints[Query::HINT_REFRESH] = true; $hints[Query::HINT_REFRESH_ENTITY] = $entity; } - $hydrator = $this->_em->newHydrator($this->_selectJoinSql ? Query::HYDRATE_OBJECT : Query::HYDRATE_SIMPLEOBJECT); - $entities = $hydrator->hydrateAll($stmt, $this->_rsm, $hints); + $hydrator = $this->em->newHydrator($this->selectJoinSql ? Query::HYDRATE_OBJECT : Query::HYDRATE_SIMPLEOBJECT); + $entities = $hydrator->hydrateAll($stmt, $this->rsm, $hints); return $entities ? $entities[0] : null; } @@ -689,11 +690,11 @@ class BasicEntityPersister */ public function loadOneToOneEntity(array $assoc, $sourceEntity, array $identifier = array()) { - if (($foundEntity = $this->_em->getUnitOfWork()->tryGetById($identifier, $assoc['targetEntity'])) != false) { + if (($foundEntity = $this->em->getUnitOfWork()->tryGetById($identifier, $assoc['targetEntity'])) != false) { return $foundEntity; } - $targetClass = $this->_em->getClassMetadata($assoc['targetEntity']); + $targetClass = $this->em->getClassMetadata($assoc['targetEntity']); if ($assoc['isOwningSide']) { $isInverseSingleValued = $assoc['inversedBy'] && ! $targetClass->isCollectionValuedAssociation($assoc['inversedBy']); @@ -707,7 +708,7 @@ class BasicEntityPersister } /* cascade read-only status - if ($this->_em->getUnitOfWork()->isReadOnly($sourceEntity)) { + if ($this->em->getUnitOfWork()->isReadOnly($sourceEntity)) { $hints[Query::HINT_READ_ONLY] = true; } */ @@ -719,7 +720,7 @@ class BasicEntityPersister $targetClass->reflFields[$assoc['inversedBy']]->setValue($targetEntity, $sourceEntity); } } else { - $sourceClass = $this->_em->getClassMetadata($assoc['sourceEntity']); + $sourceClass = $this->em->getClassMetadata($assoc['sourceEntity']); $owningAssoc = $targetClass->getAssociationMapping($assoc['mappedBy']); // TRICKY: since the association is specular source and target are flipped @@ -732,7 +733,7 @@ class BasicEntityPersister // unset the old value and set the new sql aliased value here. By definition // unset($identifier[$targetKeyColumn] works here with how UnitOfWork::createEntity() calls this method. - $identifier[$this->_getSQLTableAlias($targetClass->name) . "." . $targetKeyColumn] = + $identifier[$this->getSQLTableAlias($targetClass->name) . "." . $targetKeyColumn] = $sourceClass->reflFields[$sourceClass->fieldNames[$sourceKeyColumn]]->getValue($sourceEntity); unset($identifier[$targetKeyColumn]); @@ -757,12 +758,12 @@ class BasicEntityPersister */ public function refresh(array $id, $entity, $lockMode = 0) { - $sql = $this->_getSelectEntitiesSQL($id, null, $lockMode); + $sql = $this->getSelectEntitiesSQL($id, null, $lockMode); list($params, $types) = $this->expandParameters($id); - $stmt = $this->_conn->executeQuery($sql, $params, $types); + $stmt = $this->conn->executeQuery($sql, $params, $types); - $hydrator = $this->_em->newHydrator(Query::HYDRATE_OBJECT); - $hydrator->hydrateAll($stmt, $this->_rsm, array(Query::HINT_REFRESH => true)); + $hydrator = $this->em->newHydrator(Query::HYDRATE_OBJECT); + $hydrator->hydrateAll($stmt, $this->rsm, array(Query::HINT_REFRESH => true)); } /** @@ -778,15 +779,15 @@ class BasicEntityPersister $limit = $criteria->getMaxResults(); $offset = $criteria->getFirstResult(); - $sql = $this->_getSelectEntitiesSQL($criteria, null, 0, $limit, $offset, $orderBy); + $sql = $this->getSelectEntitiesSQL($criteria, null, 0, $limit, $offset, $orderBy); list($params, $types) = $this->expandCriteriaParameters($criteria); - $stmt = $this->_conn->executeQuery($sql, $params, $types); + $stmt = $this->conn->executeQuery($sql, $params, $types); - $hydrator = $this->_em->newHydrator(($this->_selectJoinSql) ? Query::HYDRATE_OBJECT : Query::HYDRATE_SIMPLEOBJECT); + $hydrator = $this->em->newHydrator(($this->selectJoinSql) ? Query::HYDRATE_OBJECT : Query::HYDRATE_SIMPLEOBJECT); - return $hydrator->hydrateAll($stmt, $this->_rsm, array('deferEagerLoads' => true)); + return $hydrator->hydrateAll($stmt, $this->rsm, array('deferEagerLoads' => true)); } /** @@ -806,6 +807,7 @@ class BasicEntityPersister } $valueVisitor = new SqlValueVisitor(); + $valueVisitor->dispatch($expression); list($values, $types) = $valueVisitor->getParamsAndTypes(); @@ -835,13 +837,13 @@ class BasicEntityPersister */ public function loadAll(array $criteria = array(), array $orderBy = null, $limit = null, $offset = null) { - $sql = $this->_getSelectEntitiesSQL($criteria, null, 0, $limit, $offset, $orderBy); + $sql = $this->getSelectEntitiesSQL($criteria, null, 0, $limit, $offset, $orderBy); list($params, $types) = $this->expandParameters($criteria); - $stmt = $this->_conn->executeQuery($sql, $params, $types); + $stmt = $this->conn->executeQuery($sql, $params, $types); - $hydrator = $this->_em->newHydrator(($this->_selectJoinSql) ? Query::HYDRATE_OBJECT : Query::HYDRATE_SIMPLEOBJECT); + $hydrator = $this->em->newHydrator(($this->selectJoinSql) ? Query::HYDRATE_OBJECT : Query::HYDRATE_SIMPLEOBJECT); - return $hydrator->hydrateAll($stmt, $this->_rsm, array('deferEagerLoads' => true)); + return $hydrator->hydrateAll($stmt, $this->rsm, array('deferEagerLoads' => true)); } /** @@ -873,13 +875,13 @@ class BasicEntityPersister $hints = array('deferEagerLoads' => true); if (isset($assoc['indexBy'])) { - $rsm = clone ($this->_rsm); // this is necessary because the "default rsm" should be changed. + $rsm = clone ($this->rsm); // this is necessary because the "default rsm" should be changed. $rsm->addIndexBy('r', $assoc['indexBy']); } else { - $rsm = $this->_rsm; + $rsm = $this->rsm; } - $hydrator = $this->_em->newHydrator(Query::HYDRATE_OBJECT); + $hydrator = $this->em->newHydrator(Query::HYDRATE_OBJECT); return $hydrator->hydrateAll($stmt, $rsm, $hints); } @@ -898,13 +900,13 @@ class BasicEntityPersister $hints = array('deferEagerLoads' => true, 'collection' => $coll); if (isset($assoc['indexBy'])) { - $rsm = clone ($this->_rsm); // this is necessary because the "default rsm" should be changed. + $rsm = clone ($this->rsm); // this is necessary because the "default rsm" should be changed. $rsm->addIndexBy('r', $assoc['indexBy']); } else { - $rsm = $this->_rsm; + $rsm = $this->rsm; } - $hydrator = $this->_em->newHydrator(Query::HYDRATE_OBJECT); + $hydrator = $this->em->newHydrator(Query::HYDRATE_OBJECT); return $hydrator->hydrateAll($stmt, $rsm, $hints); } @@ -929,23 +931,23 @@ class BasicEntityPersister private function getManyToManyStatement(array $assoc, $sourceEntity, $offset = null, $limit = null) { $criteria = array(); - $sourceClass = $this->_em->getClassMetadata($assoc['sourceEntity']); + $sourceClass = $this->em->getClassMetadata($assoc['sourceEntity']); if ($assoc['isOwningSide']) { - $quotedJoinTable = $this->quoteStrategy->getJoinTableName($assoc, $sourceClass, $this->_platform); + $quotedJoinTable = $this->quoteStrategy->getJoinTableName($assoc, $sourceClass, $this->platform); foreach ($assoc['joinTable']['joinColumns'] as $joinColumn) { $relationKeyColumn = $joinColumn['name']; $sourceKeyColumn = $joinColumn['referencedColumnName']; - $quotedKeyColumn = $this->quoteStrategy->getJoinColumnName($joinColumn, $sourceClass, $this->_platform); + $quotedKeyColumn = $this->quoteStrategy->getJoinColumnName($joinColumn, $sourceClass, $this->platform); if ($sourceClass->containsForeignIdentifier) { $field = $sourceClass->getFieldForColumn($sourceKeyColumn); $value = $sourceClass->reflFields[$field]->getValue($sourceEntity); if (isset($sourceClass->associationMappings[$field])) { - $value = $this->_em->getUnitOfWork()->getEntityIdentifier($value); - $value = $value[$this->_em->getClassMetadata($sourceClass->associationMappings[$field]['targetEntity'])->identifier[0]]; + $value = $this->em->getUnitOfWork()->getEntityIdentifier($value); + $value = $value[$this->em->getClassMetadata($sourceClass->associationMappings[$field]['targetEntity'])->identifier[0]]; } $criteria[$quotedJoinTable . "." . $quotedKeyColumn] = $value; @@ -958,22 +960,22 @@ class BasicEntityPersister } } } else { - $owningAssoc = $this->_em->getClassMetadata($assoc['targetEntity'])->associationMappings[$assoc['mappedBy']]; - $quotedJoinTable = $this->quoteStrategy->getJoinTableName($owningAssoc, $sourceClass, $this->_platform); + $owningAssoc = $this->em->getClassMetadata($assoc['targetEntity'])->associationMappings[$assoc['mappedBy']]; + $quotedJoinTable = $this->quoteStrategy->getJoinTableName($owningAssoc, $sourceClass, $this->platform); // TRICKY: since the association is inverted source and target are flipped foreach ($owningAssoc['joinTable']['inverseJoinColumns'] as $joinColumn) { $relationKeyColumn = $joinColumn['name']; $sourceKeyColumn = $joinColumn['referencedColumnName']; - $quotedKeyColumn = $this->quoteStrategy->getJoinColumnName($joinColumn, $sourceClass, $this->_platform); + $quotedKeyColumn = $this->quoteStrategy->getJoinColumnName($joinColumn, $sourceClass, $this->platform); if ($sourceClass->containsForeignIdentifier) { $field = $sourceClass->getFieldForColumn($sourceKeyColumn); $value = $sourceClass->reflFields[$field]->getValue($sourceEntity); if (isset($sourceClass->associationMappings[$field])) { - $value = $this->_em->getUnitOfWork()->getEntityIdentifier($value); - $value = $value[$this->_em->getClassMetadata($sourceClass->associationMappings[$field]['targetEntity'])->identifier[0]]; + $value = $this->em->getUnitOfWork()->getEntityIdentifier($value); + $value = $value[$this->em->getClassMetadata($sourceClass->associationMappings[$field]['targetEntity'])->identifier[0]]; } $criteria[$quotedJoinTable . "." . $quotedKeyColumn] = $value; @@ -987,10 +989,10 @@ class BasicEntityPersister } } - $sql = $this->_getSelectEntitiesSQL($criteria, $assoc, 0, $limit, $offset); + $sql = $this->getSelectEntitiesSQL($criteria, $assoc, 0, $limit, $offset); list($params, $types) = $this->expandParameters($criteria); - return $this->_conn->executeQuery($sql, $params, $types); + return $this->conn->executeQuery($sql, $params, $types); } /** @@ -1004,29 +1006,29 @@ class BasicEntityPersister * @param int $offset * @param array $orderBy * @return string - * @todo Refactor: _getSelectSQL(...) + * @todo Refactor: getSelectSQL(...) */ - protected function _getSelectEntitiesSQL($criteria, $assoc = null, $lockMode = 0, $limit = null, $offset = null, array $orderBy = null) + protected function getSelectEntitiesSQL($criteria, $assoc = null, $lockMode = 0, $limit = null, $offset = null, array $orderBy = null) { - $joinSql = ($assoc != null && $assoc['type'] == ClassMetadata::MANY_TO_MANY) ? $this->_getSelectManyToManyJoinSQL($assoc) : ''; + $joinSql = ($assoc != null && $assoc['type'] == ClassMetadata::MANY_TO_MANY) ? $this->getSelectManyToManyJoinSQL($assoc) : ''; $conditionSql = ($criteria instanceof Criteria) - ? $this->_getSelectConditionCriteriaSQL($criteria) - : $this->_getSelectConditionSQL($criteria, $assoc); + ? $this->getSelectConditionCriteriaSQL($criteria) + : $this->getSelectConditionSQL($criteria, $assoc); $orderBy = ($assoc !== null && isset($assoc['orderBy'])) ? $assoc['orderBy'] : $orderBy; - $orderBySql = $orderBy ? $this->_getOrderBySQL($orderBy, $this->_getSQLTableAlias($this->_class->name)) : ''; + $orderBySql = $orderBy ? $this->getOrderBySQL($orderBy, $this->getSQLTableAlias($this->class->name)) : ''; $lockSql = ''; if ($lockMode == LockMode::PESSIMISTIC_READ) { - $lockSql = ' ' . $this->_platform->getReadLockSql(); + $lockSql = ' ' . $this->platform->getReadLockSql(); } else if ($lockMode == LockMode::PESSIMISTIC_WRITE) { - $lockSql = ' ' . $this->_platform->getWriteLockSql(); + $lockSql = ' ' . $this->platform->getWriteLockSql(); } - $alias = $this->_getSQLTableAlias($this->_class->name); + $alias = $this->getSQLTableAlias($this->class->name); - if ($filterSql = $this->generateFilterConditionSQL($this->_class, $alias)) { + if ($filterSql = $this->generateFilterConditionSQL($this->class, $alias)) { if ($conditionSql) { $conditionSql .= ' AND '; } @@ -1034,10 +1036,10 @@ class BasicEntityPersister $conditionSql .= $filterSql; } - return $this->_platform->modifyLimitQuery('SELECT ' . $this->_getSelectColumnListSQL() - . $this->_platform->appendLockHint(' FROM ' . $this->quoteStrategy->getTableName($this->_class, $this->_platform) . ' ' + return $this->platform->modifyLimitQuery('SELECT ' . $this->getSelectColumnListSQL() + . $this->platform->appendLockHint(' FROM ' . $this->quoteStrategy->getTableName($this->class, $this->platform) . ' ' . $alias, $lockMode) - . $this->_selectJoinSql . $joinSql + . $this->selectJoinSql . $joinSql . ($conditionSql ? ' WHERE ' . $conditionSql : '') . $orderBySql, $limit, $offset) . $lockSql; @@ -1050,25 +1052,25 @@ class BasicEntityPersister * @param string $baseTableAlias * @return string */ - protected final function _getOrderBySQL(array $orderBy, $baseTableAlias) + protected final function getOrderBySQL(array $orderBy, $baseTableAlias) { $orderBySql = ''; foreach ($orderBy as $fieldName => $orientation) { - if ( ! isset($this->_class->fieldMappings[$fieldName])) { + if ( ! isset($this->class->fieldMappings[$fieldName])) { throw ORMException::unrecognizedField($fieldName); } $orientation = strtoupper(trim($orientation)); if ($orientation != 'ASC' && $orientation != 'DESC') { - throw ORMException::invalidOrientation($this->_class->name, $fieldName); + throw ORMException::invalidOrientation($this->class->name, $fieldName); } - $tableAlias = isset($this->_class->fieldMappings[$fieldName]['inherited']) ? - $this->_getSQLTableAlias($this->_class->fieldMappings[$fieldName]['inherited']) + $tableAlias = isset($this->class->fieldMappings[$fieldName]['inherited']) ? + $this->getSQLTableAlias($this->class->fieldMappings[$fieldName]['inherited']) : $baseTableAlias; - $columnName = $this->quoteStrategy->getColumnName($fieldName, $this->_class, $this->_platform); + $columnName = $this->quoteStrategy->getColumnName($fieldName, $this->class, $this->platform); $orderBySql .= $orderBySql ? ', ' : ' ORDER BY '; $orderBySql .= $tableAlias . '.' . $columnName . ' ' . $orientation; @@ -1083,34 +1085,34 @@ class BasicEntityPersister * * Subclasses should override this method to alter or change the select column * list SQL fragment. Note that in the implementation of BasicEntityPersister - * the resulting SQL fragment is generated only once and cached in {@link _selectColumnListSql}. + * the resulting SQL fragment is generated only once and cached in {@link selectColumnListSql}. * Subclasses may or may not do the same. * * @return string The SQL fragment. - * @todo Rename: _getSelectColumnsSQL() + * @todo Rename: getSelectColumnsSQL() */ - protected function _getSelectColumnListSQL() + protected function getSelectColumnListSQL() { - if ($this->_selectColumnListSql !== null) { - return $this->_selectColumnListSql; + if ($this->selectColumnListSql !== null) { + return $this->selectColumnListSql; } $columnList = ''; - $this->_rsm = new Query\ResultSetMapping(); - $this->_rsm->addEntityResult($this->_class->name, 'r'); // r for root + $this->rsm = new Query\ResultSetMapping(); + $this->rsm->addEntityResult($this->class->name, 'r'); // r for root // Add regular columns to select list - foreach ($this->_class->fieldNames as $field) { + foreach ($this->class->fieldNames as $field) { if ($columnList) $columnList .= ', '; - $columnList .= $this->_getSelectColumnSQL($field, $this->_class); + $columnList .= $this->getSelectColumnSQL($field, $this->class); } - $this->_selectJoinSql = ''; + $this->selectJoinSql = ''; $eagerAliasCounter = 0; - foreach ($this->_class->associationMappings as $assocField => $assoc) { - $assocColumnSQL = $this->_getSelectColumnAssociationSQL($assocField, $assoc, $this->_class); + foreach ($this->class->associationMappings as $assocField => $assoc) { + $assocColumnSQL = $this->getSelectColumnAssociationSQL($assocField, $assoc, $this->class); if ($assocColumnSQL) { if ($columnList) $columnList .= ', '; @@ -1119,23 +1121,23 @@ class BasicEntityPersister } if ($assoc['type'] & ClassMetadata::TO_ONE && ($assoc['fetch'] == ClassMetadata::FETCH_EAGER || !$assoc['isOwningSide'])) { - $eagerEntity = $this->_em->getClassMetadata($assoc['targetEntity']); + $eagerEntity = $this->em->getClassMetadata($assoc['targetEntity']); if ($eagerEntity->inheritanceType != ClassMetadata::INHERITANCE_TYPE_NONE) { continue; // now this is why you shouldn't use inheritance } $assocAlias = 'e' . ($eagerAliasCounter++); - $this->_rsm->addJoinedEntityResult($assoc['targetEntity'], $assocAlias, 'r', $assocField); + $this->rsm->addJoinedEntityResult($assoc['targetEntity'], $assocAlias, 'r', $assocField); foreach ($eagerEntity->fieldNames as $field) { if ($columnList) $columnList .= ', '; - $columnList .= $this->_getSelectColumnSQL($field, $eagerEntity, $assocAlias); + $columnList .= $this->getSelectColumnSQL($field, $eagerEntity, $assocAlias); } foreach ($eagerEntity->associationMappings as $assoc2Field => $assoc2) { - $assoc2ColumnSQL = $this->_getSelectColumnAssociationSQL($assoc2Field, $assoc2, $eagerEntity, $assocAlias); + $assoc2ColumnSQL = $this->getSelectColumnAssociationSQL($assoc2Field, $assoc2, $eagerEntity, $assocAlias); if ($assoc2ColumnSQL) { if ($columnList) $columnList .= ', '; @@ -1145,50 +1147,50 @@ class BasicEntityPersister $first = true; if ($assoc['isOwningSide']) { - $this->_selectJoinSql .= ' ' . $this->getJoinSQLForJoinColumns($assoc['joinColumns']); - $this->_selectJoinSql .= ' ' . $this->quoteStrategy->getTableName($eagerEntity, $this->_platform) . ' ' . $this->_getSQLTableAlias($eagerEntity->name, $assocAlias) .' ON '; + $this->selectJoinSql .= ' ' . $this->getJoinSQLForJoinColumns($assoc['joinColumns']); + $this->selectJoinSql .= ' ' . $this->quoteStrategy->getTableName($eagerEntity, $this->platform) . ' ' . $this->getSQLTableAlias($eagerEntity->name, $assocAlias) .' ON '; - $tableAlias = $this->_getSQLTableAlias($assoc['targetEntity'], $assocAlias); + $tableAlias = $this->getSQLTableAlias($assoc['targetEntity'], $assocAlias); foreach ($assoc['joinColumns'] as $joinColumn) { - $sourceCol = $this->quoteStrategy->getJoinColumnName($joinColumn, $this->_class, $this->_platform); - $targetCol = $this->quoteStrategy->getReferencedJoinColumnName($joinColumn, $this->_class, $this->_platform); + $sourceCol = $this->quoteStrategy->getJoinColumnName($joinColumn, $this->class, $this->platform); + $targetCol = $this->quoteStrategy->getReferencedJoinColumnName($joinColumn, $this->class, $this->platform); if ( ! $first) { - $this->_selectJoinSql .= ' AND '; + $this->selectJoinSql .= ' AND '; } - $this->_selectJoinSql .= $this->_getSQLTableAlias($assoc['sourceEntity']) . '.' . $sourceCol . ' = ' + $this->selectJoinSql .= $this->getSQLTableAlias($assoc['sourceEntity']) . '.' . $sourceCol . ' = ' . $tableAlias . '.' . $targetCol; $first = false; } // Add filter SQL if ($filterSql = $this->generateFilterConditionSQL($eagerEntity, $tableAlias)) { - $this->_selectJoinSql .= ' AND ' . $filterSql; + $this->selectJoinSql .= ' AND ' . $filterSql; } } else { - $eagerEntity = $this->_em->getClassMetadata($assoc['targetEntity']); + $eagerEntity = $this->em->getClassMetadata($assoc['targetEntity']); $owningAssoc = $eagerEntity->getAssociationMapping($assoc['mappedBy']); - $this->_selectJoinSql .= ' LEFT JOIN'; - $this->_selectJoinSql .= ' ' . $this->quoteStrategy->getTableName($eagerEntity, $this->_platform) . ' ' - . $this->_getSQLTableAlias($eagerEntity->name, $assocAlias) . ' ON '; + $this->selectJoinSql .= ' LEFT JOIN'; + $this->selectJoinSql .= ' ' . $this->quoteStrategy->getTableName($eagerEntity, $this->platform) . ' ' + . $this->getSQLTableAlias($eagerEntity->name, $assocAlias) . ' ON '; foreach ($owningAssoc['sourceToTargetKeyColumns'] as $sourceCol => $targetCol) { if ( ! $first) { - $this->_selectJoinSql .= ' AND '; + $this->selectJoinSql .= ' AND '; } - $this->_selectJoinSql .= $this->_getSQLTableAlias($owningAssoc['sourceEntity'], $assocAlias) . '.' . $sourceCol . ' = ' - . $this->_getSQLTableAlias($owningAssoc['targetEntity']) . '.' . $targetCol; + $this->selectJoinSql .= $this->getSQLTableAlias($owningAssoc['sourceEntity'], $assocAlias) . '.' . $sourceCol . ' = ' + . $this->getSQLTableAlias($owningAssoc['targetEntity']) . '.' . $targetCol; $first = false; } } } } - $this->_selectColumnListSql = $columnList; + $this->selectColumnListSql = $columnList; - return $this->_selectColumnListSql; + return $this->selectColumnListSql; } /** @@ -1201,7 +1203,7 @@ class BasicEntityPersister * * @return string */ - protected function _getSelectColumnAssociationSQL($field, $assoc, ClassMetadata $class, $alias = 'r') + protected function getSelectColumnAssociationSQL($field, $assoc, ClassMetadata $class, $alias = 'r') { $columnList = array(); @@ -1209,12 +1211,12 @@ class BasicEntityPersister foreach ($assoc['joinColumns'] as $joinColumn) { - $quotedColumn = $this->quoteStrategy->getJoinColumnName($joinColumn, $this->_class, $this->_platform); + $quotedColumn = $this->quoteStrategy->getJoinColumnName($joinColumn, $this->class, $this->platform); $resultColumnName = $this->getSQLColumnAlias($joinColumn['name']); - $columnList[] = $this->_getSQLTableAlias($class->name, ($alias == 'r' ? '' : $alias) ) + $columnList[] = $this->getSQLTableAlias($class->name, ($alias == 'r' ? '' : $alias) ) . '.' . $quotedColumn . ' AS ' . $resultColumnName; - $this->_rsm->addMetaResult($alias, $resultColumnName, $quotedColumn, isset($assoc['id']) && $assoc['id'] === true); + $this->rsm->addMetaResult($alias, $resultColumnName, $quotedColumn, isset($assoc['id']) && $assoc['id'] === true); } } @@ -1228,25 +1230,25 @@ class BasicEntityPersister * @param ManyToManyMapping $manyToMany * @return string */ - protected function _getSelectManyToManyJoinSQL(array $manyToMany) + protected function getSelectManyToManyJoinSQL(array $manyToMany) { $conditions = array(); $association = $manyToMany; - $sourceTableAlias = $this->_getSQLTableAlias($this->_class->name); + $sourceTableAlias = $this->getSQLTableAlias($this->class->name); if ( ! $manyToMany['isOwningSide']) { - $targetEntity = $this->_em->getClassMetadata($manyToMany['targetEntity']); + $targetEntity = $this->em->getClassMetadata($manyToMany['targetEntity']); $association = $targetEntity->associationMappings[$manyToMany['mappedBy']]; } - $joinTableName = $this->quoteStrategy->getJoinTableName($association, $this->_class, $this->_platform); + $joinTableName = $this->quoteStrategy->getJoinTableName($association, $this->class, $this->platform); $joinColumns = ($manyToMany['isOwningSide']) ? $association['joinTable']['inverseJoinColumns'] : $association['joinTable']['joinColumns']; foreach ($joinColumns as $joinColumn) { - $quotedSourceColumn = $this->quoteStrategy->getJoinColumnName($joinColumn, $this->_class, $this->_platform); - $quotedTargetColumn = $this->quoteStrategy->getReferencedJoinColumnName($joinColumn, $this->_class, $this->_platform); + $quotedSourceColumn = $this->quoteStrategy->getJoinColumnName($joinColumn, $this->class, $this->platform); + $quotedTargetColumn = $this->quoteStrategy->getReferencedJoinColumnName($joinColumn, $this->class, $this->platform); $conditions[] = $sourceTableAlias . '.' . $quotedTargetColumn . ' = ' . $joinTableName . '.' . $quotedSourceColumn; } @@ -1258,16 +1260,16 @@ class BasicEntityPersister * * @return string */ - protected function _getInsertSQL() + protected function getInsertSQL() { - if ($this->_insertSql === null) { + if ($this->insertSql === null) { $insertSql = ''; - $columns = $this->_getInsertColumnList(); + $columns = $this->getInsertColumnList(); if (empty($columns)) { - $insertSql = $this->_platform->getEmptyIdentityInsertSQL( - $this->quoteStrategy->getTableName($this->_class, $this->_platform), - $this->quoteStrategy->getColumnName($this->_class->identifier[0], $this->_class, $this->_platform) + $insertSql = $this->platform->getEmptyIdentityInsertSQL( + $this->quoteStrategy->getTableName($this->class, $this->platform), + $this->quoteStrategy->getColumnName($this->class->identifier[0], $this->class, $this->platform) ); } else { $columns = array_unique($columns); @@ -1276,24 +1278,24 @@ class BasicEntityPersister foreach ($columns as $column) { $placeholder = '?'; - if (isset($this->_class->fieldNames[$column]) && - isset($this->_columnTypes[$this->_class->fieldNames[$column]]) && - isset($this->_class->fieldMappings[$this->_class->fieldNames[$column]]['requireSQLConversion'])) { - $type = Type::getType($this->_columnTypes[$this->_class->fieldNames[$column]]); - $placeholder = $type->convertToDatabaseValueSQL('?', $this->_platform); + if (isset($this->class->fieldNames[$column]) && + isset($this->columnTypes[$this->class->fieldNames[$column]]) && + isset($this->class->fieldMappings[$this->class->fieldNames[$column]]['requireSQLConversion'])) { + $type = Type::getType($this->columnTypes[$this->class->fieldNames[$column]]); + $placeholder = $type->convertToDatabaseValueSQL('?', $this->platform); } $values[] = $placeholder; } - $insertSql = 'INSERT INTO ' . $this->quoteStrategy->getTableName($this->_class, $this->_platform) + $insertSql = 'INSERT INTO ' . $this->quoteStrategy->getTableName($this->class, $this->platform) . ' (' . implode(', ', $columns) . ') VALUES (' . implode(', ', $values) . ')'; } - $this->_insertSql = $insertSql; + $this->insertSql = $insertSql; } - return $this->_insertSql; + return $this->insertSql; } /** @@ -1304,25 +1306,25 @@ class BasicEntityPersister * * @return array The list of columns. */ - protected function _getInsertColumnList() + protected function getInsertColumnList() { $columns = array(); - foreach ($this->_class->reflFields as $name => $field) { - if ($this->_class->isVersioned && $this->_class->versionField == $name) { + foreach ($this->class->reflFields as $name => $field) { + if ($this->class->isVersioned && $this->class->versionField == $name) { continue; } - if (isset($this->_class->associationMappings[$name])) { - $assoc = $this->_class->associationMappings[$name]; + if (isset($this->class->associationMappings[$name])) { + $assoc = $this->class->associationMappings[$name]; if ($assoc['isOwningSide'] && $assoc['type'] & ClassMetadata::TO_ONE) { foreach ($assoc['joinColumns'] as $joinColumn) { - $columns[] = $this->quoteStrategy->getJoinColumnName($joinColumn, $this->_class, $this->_platform); + $columns[] = $this->quoteStrategy->getJoinColumnName($joinColumn, $this->class, $this->platform); } } - } else if ($this->_class->generatorType != ClassMetadata::GENERATOR_TYPE_IDENTITY || $this->_class->identifier[0] != $name) { - $columns[] = $this->quoteStrategy->getColumnName($name, $this->_class, $this->_platform); - $this->_columnTypes[$name] = $this->_class->fieldMappings[$name]['type']; + } else if ($this->class->generatorType != ClassMetadata::GENERATOR_TYPE_IDENTITY || $this->class->identifier[0] != $name) { + $columns[] = $this->quoteStrategy->getColumnName($name, $this->class, $this->platform); + $this->columnTypes[$name] = $this->class->fieldMappings[$name]['type']; } } @@ -1337,17 +1339,17 @@ class BasicEntityPersister * mapped to must own the column for the given field. * @param string $alias */ - protected function _getSelectColumnSQL($field, ClassMetadata $class, $alias = 'r') + protected function getSelectColumnSQL($field, ClassMetadata $class, $alias = 'r') { - $sql = $this->_getSQLTableAlias($class->name, $alias == 'r' ? '' : $alias) - . '.' . $this->quoteStrategy->getColumnName($field, $class, $this->_platform); + $sql = $this->getSQLTableAlias($class->name, $alias == 'r' ? '' : $alias) + . '.' . $this->quoteStrategy->getColumnName($field, $class, $this->platform); $columnAlias = $this->getSQLColumnAlias($class->columnNames[$field]); - $this->_rsm->addFieldResult($alias, $columnAlias, $field); + $this->rsm->addFieldResult($alias, $columnAlias, $field); if (isset($class->fieldMappings[$field]['requireSQLConversion'])) { $type = Type::getType($class->getTypeOfField($field)); - $sql = $type->convertToPHPValueSQL($sql, $this->_platform); + $sql = $type->convertToPHPValueSQL($sql, $this->platform); } return $sql . ' AS ' . $columnAlias; @@ -1360,19 +1362,19 @@ class BasicEntityPersister * @return string The SQL table alias. * @todo Reconsider. Binding table aliases to class names is not such a good idea. */ - protected function _getSQLTableAlias($className, $assocName = '') + protected function getSQLTableAlias($className, $assocName = '') { if ($assocName) { $className .= '#' . $assocName; } - if (isset($this->_sqlTableAliases[$className])) { - return $this->_sqlTableAliases[$className]; + if (isset($this->sqlTableAliases[$className])) { + return $this->sqlTableAliases[$className]; } - $tableAlias = 't' . $this->_sqlAliasCounter++; + $tableAlias = 't' . $this->sqlAliasCounter++; - $this->_sqlTableAliases[$className] = $tableAlias; + $this->sqlTableAliases[$className] = $tableAlias; return $tableAlias; } @@ -1386,21 +1388,21 @@ class BasicEntityPersister */ public function lock(array $criteria, $lockMode) { - $conditionSql = $this->_getSelectConditionSQL($criteria); + $conditionSql = $this->getSelectConditionSQL($criteria); if ($lockMode == LockMode::PESSIMISTIC_READ) { - $lockSql = $this->_platform->getReadLockSql(); + $lockSql = $this->platform->getReadLockSql(); } else if ($lockMode == LockMode::PESSIMISTIC_WRITE) { - $lockSql = $this->_platform->getWriteLockSql(); + $lockSql = $this->platform->getWriteLockSql(); } $sql = 'SELECT 1 ' - . $this->_platform->appendLockHint($this->getLockTablesSql(), $lockMode) + . $this->platform->appendLockHint($this->getLockTablesSql(), $lockMode) . ($conditionSql ? ' WHERE ' . $conditionSql : '') . ' ' . $lockSql; list($params, $types) = $this->expandParameters($criteria); - $this->_conn->executeQuery($sql, $params, $types); + $this->conn->executeQuery($sql, $params, $types); } /** @@ -1410,8 +1412,8 @@ class BasicEntityPersister */ protected function getLockTablesSql() { - return 'FROM ' . $this->quoteStrategy->getTableName($this->_class, $this->_platform) . ' ' - . $this->_getSQLTableAlias($this->_class->name); + return 'FROM ' . $this->quoteStrategy->getTableName($this->class, $this->platform) . ' ' + . $this->getSQLTableAlias($this->class->name); } /** @@ -1420,7 +1422,7 @@ class BasicEntityPersister * @param \Doctrine\Common\Collections\Criteria $criteria * @return string */ - protected function _getSelectConditionCriteriaSQL(Criteria $criteria) + protected function getSelectConditionCriteriaSQL(Criteria $criteria) { $expression = $criteria->getWhereExpression(); @@ -1448,9 +1450,9 @@ class BasicEntityPersister $conditionSql = $this->getSelectConditionStatementColumnSQL($field, $assoc); $placeholder = '?'; - if (isset($this->_class->fieldMappings[$field]['requireSQLConversion'])) { - $type = Type::getType($this->_class->getTypeOfField($field)); - $placeholder = $type->convertToDatabaseValueSQL($placeholder, $this->_platform); + if (isset($this->class->fieldMappings[$field]['requireSQLConversion'])) { + $type = Type::getType($this->class->getTypeOfField($field)); + $placeholder = $type->convertToDatabaseValueSQL($placeholder, $this->platform); } $conditionSql .= ($comparison === null) @@ -1472,23 +1474,23 @@ class BasicEntityPersister protected function getSelectConditionStatementColumnSQL($field, $assoc = null) { switch (true) { - case (isset($this->_class->columnNames[$field])): - $className = (isset($this->_class->fieldMappings[$field]['inherited'])) - ? $this->_class->fieldMappings[$field]['inherited'] - : $this->_class->name; + case (isset($this->class->columnNames[$field])): + $className = (isset($this->class->fieldMappings[$field]['inherited'])) + ? $this->class->fieldMappings[$field]['inherited'] + : $this->class->name; - return $this->_getSQLTableAlias($className) . '.' . $this->quoteStrategy->getColumnName($field, $this->_class, $this->_platform); + return $this->getSQLTableAlias($className) . '.' . $this->quoteStrategy->getColumnName($field, $this->class, $this->platform); - case (isset($this->_class->associationMappings[$field])): - if ( ! $this->_class->associationMappings[$field]['isOwningSide']) { - throw ORMException::invalidFindByInverseAssociation($this->_class->name, $field); + case (isset($this->class->associationMappings[$field])): + if ( ! $this->class->associationMappings[$field]['isOwningSide']) { + throw ORMException::invalidFindByInverseAssociation($this->class->name, $field); } - $className = (isset($this->_class->associationMappings[$field]['inherited'])) - ? $this->_class->associationMappings[$field]['inherited'] - : $this->_class->name; + $className = (isset($this->class->associationMappings[$field]['inherited'])) + ? $this->class->associationMappings[$field]['inherited'] + : $this->class->name; - return $this->_getSQLTableAlias($className) . '.' . $this->_class->associationMappings[$field]['joinColumns'][0]['name']; + return $this->getSQLTableAlias($className) . '.' . $this->class->associationMappings[$field]['joinColumns'][0]['name']; case ($assoc !== null && strpos($field, " ") === false && strpos($field, "(") === false): // very careless developers could potentially open up this normally hidden api for userland attacks, @@ -1512,7 +1514,7 @@ class BasicEntityPersister * @param AssociationMapping $assoc * @return string */ - protected function _getSelectConditionSQL(array $criteria, $assoc = null) + protected function getSelectConditionSQL(array $criteria, $assoc = null) { $conditionSql = ''; @@ -1568,10 +1570,10 @@ class BasicEntityPersister private function getOneToManyStatement(array $assoc, $sourceEntity, $offset = null, $limit = null) { $criteria = array(); - $owningAssoc = $this->_class->associationMappings[$assoc['mappedBy']]; - $sourceClass = $this->_em->getClassMetadata($assoc['sourceEntity']); + $owningAssoc = $this->class->associationMappings[$assoc['mappedBy']]; + $sourceClass = $this->em->getClassMetadata($assoc['sourceEntity']); - $tableAlias = $this->_getSQLTableAlias(isset($owningAssoc['inherited']) ? $owningAssoc['inherited'] : $this->_class->name); + $tableAlias = $this->getSQLTableAlias(isset($owningAssoc['inherited']) ? $owningAssoc['inherited'] : $this->class->name); foreach ($owningAssoc['targetToSourceKeyColumns'] as $sourceKeyColumn => $targetKeyColumn) { if ($sourceClass->containsForeignIdentifier) { @@ -1579,8 +1581,8 @@ class BasicEntityPersister $value = $sourceClass->reflFields[$field]->getValue($sourceEntity); if (isset($sourceClass->associationMappings[$field])) { - $value = $this->_em->getUnitOfWork()->getEntityIdentifier($value); - $value = $value[$this->_em->getClassMetadata($sourceClass->associationMappings[$field]['targetEntity'])->identifier[0]]; + $value = $this->em->getUnitOfWork()->getEntityIdentifier($value); + $value = $value[$this->em->getClassMetadata($sourceClass->associationMappings[$field]['targetEntity'])->identifier[0]]; } $criteria[$tableAlias . "." . $targetKeyColumn] = $value; @@ -1589,10 +1591,10 @@ class BasicEntityPersister } } - $sql = $this->_getSelectEntitiesSQL($criteria, $assoc, 0, $limit, $offset); + $sql = $this->getSelectEntitiesSQL($criteria, $assoc, 0, $limit, $offset); list($params, $types) = $this->expandParameters($criteria); - return $this->_conn->executeQuery($sql, $params, $types); + return $this->conn->executeQuery($sql, $params, $types); } /** @@ -1627,18 +1629,18 @@ class BasicEntityPersister private function getType($field, $value) { switch (true) { - case (isset($this->_class->fieldMappings[$field])): - $type = $this->_class->fieldMappings[$field]['type']; + case (isset($this->class->fieldMappings[$field])): + $type = $this->class->fieldMappings[$field]['type']; break; - case (isset($this->_class->associationMappings[$field])): - $assoc = $this->_class->associationMappings[$field]; + case (isset($this->class->associationMappings[$field])): + $assoc = $this->class->associationMappings[$field]; if (count($assoc['sourceToTargetKeyColumns']) > 1) { throw Query\QueryException::associationPathCompositeKeyNotSupported(); } - $targetClass = $this->_em->getClassMetadata($assoc['targetEntity']); + $targetClass = $this->em->getClassMetadata($assoc['targetEntity']); $targetColumn = $assoc['joinColumns'][0]['referencedColumnName']; $type = null; @@ -1688,11 +1690,11 @@ class BasicEntityPersister */ private function getIndividualValue($value) { - if (is_object($value) && $this->_em->getMetadataFactory()->hasMetadataFor(ClassUtils::getClass($value))) { - if ($this->_em->getUnitOfWork()->getEntityState($value) === UnitOfWork::STATE_MANAGED) { - $idValues = $this->_em->getUnitOfWork()->getEntityIdentifier($value); + if (is_object($value) && $this->em->getMetadataFactory()->hasMetadataFor(ClassUtils::getClass($value))) { + if ($this->em->getUnitOfWork()->getEntityState($value) === UnitOfWork::STATE_MANAGED) { + $idValues = $this->em->getUnitOfWork()->getEntityIdentifier($value); } else { - $class = $this->_em->getClassMetadata(get_class($value)); + $class = $this->em->getClassMetadata(get_class($value)); $idValues = $class->getIdentifierValues($value); } @@ -1714,7 +1716,7 @@ class BasicEntityPersister */ public function exists($entity, array $extraConditions = array()) { - $criteria = $this->_class->getIdentifierValues($entity); + $criteria = $this->class->getIdentifierValues($entity); if ( ! $criteria) { return false; @@ -1724,19 +1726,19 @@ class BasicEntityPersister $criteria = array_merge($criteria, $extraConditions); } - $alias = $this->_getSQLTableAlias($this->_class->name); + $alias = $this->getSQLTableAlias($this->class->name); $sql = 'SELECT 1 ' . $this->getLockTablesSql() - . ' WHERE ' . $this->_getSelectConditionSQL($criteria); + . ' WHERE ' . $this->getSelectConditionSQL($criteria); - if ($filterSql = $this->generateFilterConditionSQL($this->_class, $alias)) { + if ($filterSql = $this->generateFilterConditionSQL($this->class, $alias)) { $sql .= ' AND ' . $filterSql; } list($params) = $this->expandParameters($criteria); - return (bool) $this->_conn->fetchColumn($sql, $params); + return (bool) $this->conn->fetchColumn($sql, $params); } /** @@ -1765,7 +1767,7 @@ class BasicEntityPersister */ public function getSQLColumnAlias($columnName) { - return $this->quoteStrategy->getColumnAlias($columnName, $this->_sqlAliasCounter++, $this->_platform); + return $this->quoteStrategy->getColumnAlias($columnName, $this->sqlAliasCounter++, $this->platform); } /** @@ -1780,7 +1782,7 @@ class BasicEntityPersister { $filterClauses = array(); - foreach ($this->_em->getFilters()->getEnabledFilters() as $filter) { + foreach ($this->em->getFilters()->getEnabledFilters() as $filter) { if ('' !== $filterExpr = $filter->addFilterConstraint($targetEntity, $targetTableAlias)) { $filterClauses[] = '(' . $filterExpr . ')'; } diff --git a/lib/Doctrine/ORM/Persisters/JoinedSubclassPersister.php b/lib/Doctrine/ORM/Persisters/JoinedSubclassPersister.php index 345a9a030..129adbc04 100644 --- a/lib/Doctrine/ORM/Persisters/JoinedSubclassPersister.php +++ b/lib/Doctrine/ORM/Persisters/JoinedSubclassPersister.php @@ -45,23 +45,23 @@ class JoinedSubclassPersister extends AbstractEntityInheritancePersister * * @var array */ - private $_owningTableMap = array(); + private $owningTableMap = array(); /** * Map of table to quoted table names. * * @var array */ - private $_quotedTableMap = array(); + private $quotedTableMap = array(); /** * {@inheritdoc} */ - protected function _getDiscriminatorColumnTableName() + protected function getDiscriminatorColumnTableName() { - $class = ($this->_class->name !== $this->_class->rootEntityName) - ? $this->_em->getClassMetadata($this->_class->rootEntityName) - : $this->_class; + $class = ($this->class->name !== $this->class->rootEntityName) + ? $this->em->getClassMetadata($this->class->rootEntityName) + : $this->class; return $class->getTableName(); } @@ -72,15 +72,15 @@ class JoinedSubclassPersister extends AbstractEntityInheritancePersister * * @return \Doctrine\ORM\Mapping\ClassMetadata */ - private function _getVersionedClassMetadata() + private function getVersionedClassMetadata() { - if (isset($this->_class->fieldMappings[$this->_class->versionField]['inherited'])) { - $definingClassName = $this->_class->fieldMappings[$this->_class->versionField]['inherited']; + if (isset($this->class->fieldMappings[$this->class->versionField]['inherited'])) { + $definingClassName = $this->class->fieldMappings[$this->class->versionField]['inherited']; - return $this->_em->getClassMetadata($definingClassName); + return $this->em->getClassMetadata($definingClassName); } - return $this->_class; + return $this->class; } /** @@ -92,22 +92,22 @@ class JoinedSubclassPersister extends AbstractEntityInheritancePersister */ public function getOwningTable($fieldName) { - if (isset($this->_owningTableMap[$fieldName])) { - return $this->_owningTableMap[$fieldName]; + if (isset($this->owningTableMap[$fieldName])) { + return $this->owningTableMap[$fieldName]; } - if (isset($this->_class->associationMappings[$fieldName]['inherited'])) { - $cm = $this->_em->getClassMetadata($this->_class->associationMappings[$fieldName]['inherited']); - } else if (isset($this->_class->fieldMappings[$fieldName]['inherited'])) { - $cm = $this->_em->getClassMetadata($this->_class->fieldMappings[$fieldName]['inherited']); + if (isset($this->class->associationMappings[$fieldName]['inherited'])) { + $cm = $this->em->getClassMetadata($this->class->associationMappings[$fieldName]['inherited']); + } else if (isset($this->class->fieldMappings[$fieldName]['inherited'])) { + $cm = $this->em->getClassMetadata($this->class->fieldMappings[$fieldName]['inherited']); } else { - $cm = $this->_class; + $cm = $this->class; } $tableName = $cm->getTableName(); - $this->_owningTableMap[$fieldName] = $tableName; - $this->_quotedTableMap[$tableName] = $this->quoteStrategy->getTableName($cm, $this->_platform); + $this->owningTableMap[$fieldName] = $tableName; + $this->quotedTableMap[$tableName] = $this->quoteStrategy->getTableName($cm, $this->platform); return $tableName; } @@ -117,57 +117,57 @@ class JoinedSubclassPersister extends AbstractEntityInheritancePersister */ public function executeInserts() { - if ( ! $this->_queuedInserts) { + if ( ! $this->queuedInserts) { return; } $postInsertIds = array(); - $idGen = $this->_class->idGenerator; + $idGen = $this->class->idGenerator; $isPostInsertId = $idGen->isPostInsertGenerator(); // Prepare statement for the root table - $rootClass = ($this->_class->name !== $this->_class->rootEntityName) ? $this->_em->getClassMetadata($this->_class->rootEntityName) : $this->_class; - $rootPersister = $this->_em->getUnitOfWork()->getEntityPersister($rootClass->name); + $rootClass = ($this->class->name !== $this->class->rootEntityName) ? $this->em->getClassMetadata($this->class->rootEntityName) : $this->class; + $rootPersister = $this->em->getUnitOfWork()->getEntityPersister($rootClass->name); $rootTableName = $rootClass->getTableName(); - $rootTableStmt = $this->_conn->prepare($rootPersister->_getInsertSQL()); + $rootTableStmt = $this->conn->prepare($rootPersister->getInsertSQL()); // Prepare statements for sub tables. $subTableStmts = array(); - if ($rootClass !== $this->_class) { - $subTableStmts[$this->_class->getTableName()] = $this->_conn->prepare($this->_getInsertSQL()); + if ($rootClass !== $this->class) { + $subTableStmts[$this->class->getTableName()] = $this->conn->prepare($this->getInsertSQL()); } - foreach ($this->_class->parentClasses as $parentClassName) { - $parentClass = $this->_em->getClassMetadata($parentClassName); + foreach ($this->class->parentClasses as $parentClassName) { + $parentClass = $this->em->getClassMetadata($parentClassName); $parentTableName = $parentClass->getTableName(); if ($parentClass !== $rootClass) { - $parentPersister = $this->_em->getUnitOfWork()->getEntityPersister($parentClassName); - $subTableStmts[$parentTableName] = $this->_conn->prepare($parentPersister->_getInsertSQL()); + $parentPersister = $this->em->getUnitOfWork()->getEntityPersister($parentClassName); + $subTableStmts[$parentTableName] = $this->conn->prepare($parentPersister->getInsertSQL()); } } // Execute all inserts. For each entity: // 1) Insert on root table // 2) Insert on sub tables - foreach ($this->_queuedInserts as $entity) { - $insertData = $this->_prepareInsertData($entity); + foreach ($this->queuedInserts as $entity) { + $insertData = $this->prepareInsertData($entity); // Execute insert on root table $paramIndex = 1; foreach ($insertData[$rootTableName] as $columnName => $value) { - $rootTableStmt->bindValue($paramIndex++, $value, $this->_columnTypes[$columnName]); + $rootTableStmt->bindValue($paramIndex++, $value, $this->columnTypes[$columnName]); } $rootTableStmt->execute(); if ($isPostInsertId) { - $id = $idGen->generate($this->_em, $entity); + $id = $idGen->generate($this->em, $entity); $postInsertIds[$id] = $entity; } else { - $id = $this->_em->getUnitOfWork()->getEntityIdentifier($entity); + $id = $this->em->getUnitOfWork()->getEntityIdentifier($entity); } // Execute inserts on subtables. @@ -177,13 +177,13 @@ class JoinedSubclassPersister extends AbstractEntityInheritancePersister $paramIndex = 1; foreach ((array) $id as $idName => $idVal) { - $type = isset($this->_columnTypes[$idName]) ? $this->_columnTypes[$idName] : Type::STRING; + $type = isset($this->columnTypes[$idName]) ? $this->columnTypes[$idName] : Type::STRING; $stmt->bindValue($paramIndex++, $idVal, $type); } foreach ($data as $columnName => $value) { - $stmt->bindValue($paramIndex++, $value, $this->_columnTypes[$columnName]); + $stmt->bindValue($paramIndex++, $value, $this->columnTypes[$columnName]); } $stmt->execute(); @@ -196,11 +196,11 @@ class JoinedSubclassPersister extends AbstractEntityInheritancePersister $stmt->closeCursor(); } - if ($this->_class->isVersioned) { + if ($this->class->isVersioned) { $this->assignDefaultVersionValue($entity, $id); } - $this->_queuedInserts = array(); + $this->queuedInserts = array(); return $postInsertIds; } @@ -210,26 +210,26 @@ class JoinedSubclassPersister extends AbstractEntityInheritancePersister */ public function update($entity) { - $updateData = $this->_prepareUpdateData($entity); + $updateData = $this->prepareUpdateData($entity); - if (($isVersioned = $this->_class->isVersioned) != false) { - $versionedClass = $this->_getVersionedClassMetadata(); + if (($isVersioned = $this->class->isVersioned) != false) { + $versionedClass = $this->getVersionedClassMetadata(); $versionedTable = $versionedClass->getTableName(); } if ($updateData) { foreach ($updateData as $tableName => $data) { - $this->_updateTable( - $entity, $this->_quotedTableMap[$tableName], $data, $isVersioned && $versionedTable == $tableName + $this->updateTable( + $entity, $this->quotedTableMap[$tableName], $data, $isVersioned && $versionedTable == $tableName ); } // Make sure the table with the version column is updated even if no columns on that // table were affected. if ($isVersioned && ! isset($updateData[$versionedTable])) { - $this->_updateTable($entity, $this->quoteStrategy->getTableName($versionedClass, $this->_platform), array(), true); + $this->updateTable($entity, $this->quoteStrategy->getTableName($versionedClass, $this->platform), array(), true); - $id = $this->_em->getUnitOfWork()->getEntityIdentifier($entity); + $id = $this->em->getUnitOfWork()->getEntityIdentifier($entity); $this->assignDefaultVersionValue($entity, $id); } } @@ -240,24 +240,24 @@ class JoinedSubclassPersister extends AbstractEntityInheritancePersister */ public function delete($entity) { - $identifier = $this->_em->getUnitOfWork()->getEntityIdentifier($entity); + $identifier = $this->em->getUnitOfWork()->getEntityIdentifier($entity); $this->deleteJoinTableRecords($identifier); - $id = array_combine($this->_class->getIdentifierColumnNames(), $identifier); + $id = array_combine($this->class->getIdentifierColumnNames(), $identifier); // If the database platform supports FKs, just // delete the row from the root table. Cascades do the rest. - if ($this->_platform->supportsForeignKeyConstraints()) { - $this->_conn->delete( - $this->quoteStrategy->getTableName($this->_em->getClassMetadata($this->_class->rootEntityName), $this->_platform), $id + if ($this->platform->supportsForeignKeyConstraints()) { + $this->conn->delete( + $this->quoteStrategy->getTableName($this->em->getClassMetadata($this->class->rootEntityName), $this->platform), $id ); } else { // Delete from all tables individually, starting from this class' table up to the root table. - $this->_conn->delete($this->quoteStrategy->getTableName($this->_class, $this->_platform), $id); + $this->conn->delete($this->quoteStrategy->getTableName($this->class, $this->platform), $id); - foreach ($this->_class->parentClasses as $parentClass) { - $this->_conn->delete( - $this->quoteStrategy->getTableName($this->_em->getClassMetadata($parentClass), $this->_platform), $id + foreach ($this->class->parentClasses as $parentClass) { + $this->conn->delete( + $this->quoteStrategy->getTableName($this->em->getClassMetadata($parentClass), $this->platform), $id ); } } @@ -266,33 +266,33 @@ class JoinedSubclassPersister extends AbstractEntityInheritancePersister /** * {@inheritdoc} */ - protected function _getSelectEntitiesSQL($criteria, $assoc = null, $lockMode = 0, $limit = null, $offset = null, array $orderBy = null) + protected function getSelectEntitiesSQL($criteria, $assoc = null, $lockMode = 0, $limit = null, $offset = null, array $orderBy = null) { - $idColumns = $this->_class->getIdentifierColumnNames(); - $baseTableAlias = $this->_getSQLTableAlias($this->_class->name); + $idColumns = $this->class->getIdentifierColumnNames(); + $baseTableAlias = $this->getSQLTableAlias($this->class->name); // Create the column list fragment only once - if ($this->_selectColumnListSql === null) { + if ($this->selectColumnListSql === null) { - $this->_rsm = new ResultSetMapping(); - $this->_rsm->addEntityResult($this->_class->name, 'r'); + $this->rsm = new ResultSetMapping(); + $this->rsm->addEntityResult($this->class->name, 'r'); // Add regular columns $columnList = ''; - foreach ($this->_class->fieldMappings as $fieldName => $mapping) { + foreach ($this->class->fieldMappings as $fieldName => $mapping) { if ($columnList != '') $columnList .= ', '; - $columnList .= $this->_getSelectColumnSQL( + $columnList .= $this->getSelectColumnSQL( $fieldName, - isset($mapping['inherited']) ? $this->_em->getClassMetadata($mapping['inherited']) : $this->_class + isset($mapping['inherited']) ? $this->em->getClassMetadata($mapping['inherited']) : $this->class ); } // Add foreign key columns - foreach ($this->_class->associationMappings as $assoc2) { + foreach ($this->class->associationMappings as $assoc2) { if ($assoc2['isOwningSide'] && $assoc2['type'] & ClassMetadata::TO_ONE) { - $tableAlias = isset($assoc2['inherited']) ? $this->_getSQLTableAlias($assoc2['inherited']) : $baseTableAlias; + $tableAlias = isset($assoc2['inherited']) ? $this->getSQLTableAlias($assoc2['inherited']) : $baseTableAlias; foreach ($assoc2['targetToSourceKeyColumns'] as $srcColumn) { if ($columnList != '') $columnList .= ', '; @@ -300,30 +300,30 @@ class JoinedSubclassPersister extends AbstractEntityInheritancePersister $columnList .= $this->getSelectJoinColumnSQL( $tableAlias, $srcColumn, - isset($assoc2['inherited']) ? $assoc2['inherited'] : $this->_class->name + isset($assoc2['inherited']) ? $assoc2['inherited'] : $this->class->name ); } } } // Add discriminator column (DO NOT ALIAS, see AbstractEntityInheritancePersister#_processSQLResult). - $discrColumn = $this->_class->discriminatorColumn['name']; - $tableAlias = ($this->_class->rootEntityName == $this->_class->name) ? $baseTableAlias : $this->_getSQLTableAlias($this->_class->rootEntityName); + $discrColumn = $this->class->discriminatorColumn['name']; + $tableAlias = ($this->class->rootEntityName == $this->class->name) ? $baseTableAlias : $this->getSQLTableAlias($this->class->rootEntityName); $columnList .= ', ' . $tableAlias . '.' . $discrColumn; - $resultColumnName = $this->_platform->getSQLResultCasing($discrColumn); + $resultColumnName = $this->platform->getSQLResultCasing($discrColumn); - $this->_rsm->setDiscriminatorColumn('r', $resultColumnName); - $this->_rsm->addMetaResult('r', $resultColumnName, $discrColumn); + $this->rsm->setDiscriminatorColumn('r', $resultColumnName); + $this->rsm->addMetaResult('r', $resultColumnName, $discrColumn); } // INNER JOIN parent tables $joinSql = ''; - foreach ($this->_class->parentClasses as $parentClassName) { - $parentClass = $this->_em->getClassMetadata($parentClassName); - $tableAlias = $this->_getSQLTableAlias($parentClassName); - $joinSql .= ' INNER JOIN ' . $this->quoteStrategy->getTableName($parentClass, $this->_platform) . ' ' . $tableAlias . ' ON '; + foreach ($this->class->parentClasses as $parentClassName) { + $parentClass = $this->em->getClassMetadata($parentClassName); + $tableAlias = $this->getSQLTableAlias($parentClassName); + $joinSql .= ' INNER JOIN ' . $this->quoteStrategy->getTableName($parentClass, $this->platform) . ' ' . $tableAlias . ' ON '; $first = true; foreach ($idColumns as $idColumn) { @@ -334,16 +334,16 @@ class JoinedSubclassPersister extends AbstractEntityInheritancePersister } // OUTER JOIN sub tables - foreach ($this->_class->subClasses as $subClassName) { - $subClass = $this->_em->getClassMetadata($subClassName); - $tableAlias = $this->_getSQLTableAlias($subClassName); + foreach ($this->class->subClasses as $subClassName) { + $subClass = $this->em->getClassMetadata($subClassName); + $tableAlias = $this->getSQLTableAlias($subClassName); - if ($this->_selectColumnListSql === null) { + if ($this->selectColumnListSql === null) { // Add subclass columns foreach ($subClass->fieldMappings as $fieldName => $mapping) { if (isset($mapping['inherited'])) continue; - $columnList .= ', ' . $this->_getSelectColumnSQL($fieldName, $subClass); + $columnList .= ', ' . $this->getSelectColumnSQL($fieldName, $subClass); } // Add join columns (foreign keys) @@ -363,7 +363,7 @@ class JoinedSubclassPersister extends AbstractEntityInheritancePersister } // Add LEFT JOIN - $joinSql .= ' LEFT JOIN ' . $this->quoteStrategy->getTableName($subClass, $this->_platform) . ' ' . $tableAlias . ' ON '; + $joinSql .= ' LEFT JOIN ' . $this->quoteStrategy->getTableName($subClass, $this->platform) . ' ' . $tableAlias . ' ON '; $first = true; foreach ($idColumns as $idColumn) { @@ -373,14 +373,14 @@ class JoinedSubclassPersister extends AbstractEntityInheritancePersister } } - $joinSql .= ($assoc != null && $assoc['type'] == ClassMetadata::MANY_TO_MANY) ? $this->_getSelectManyToManyJoinSQL($assoc) : ''; + $joinSql .= ($assoc != null && $assoc['type'] == ClassMetadata::MANY_TO_MANY) ? $this->getSelectManyToManyJoinSQL($assoc) : ''; $conditionSql = ($criteria instanceof Criteria) - ? $this->_getSelectConditionCriteriaSQL($criteria) - : $this->_getSelectConditionSQL($criteria, $assoc); + ? $this->getSelectConditionCriteriaSQL($criteria) + : $this->getSelectConditionSQL($criteria, $assoc); // If the current class in the root entity, add the filters - if ($filterSql = $this->generateFilterConditionSQL($this->_em->getClassMetadata($this->_class->rootEntityName), $this->_getSQLTableAlias($this->_class->rootEntityName))) { + if ($filterSql = $this->generateFilterConditionSQL($this->em->getClassMetadata($this->class->rootEntityName), $this->getSQLTableAlias($this->class->rootEntityName))) { if ($conditionSql) { $conditionSql .= ' AND '; } @@ -389,22 +389,22 @@ class JoinedSubclassPersister extends AbstractEntityInheritancePersister } $orderBy = ($assoc !== null && isset($assoc['orderBy'])) ? $assoc['orderBy'] : $orderBy; - $orderBySql = $orderBy ? $this->_getOrderBySQL($orderBy, $baseTableAlias) : ''; + $orderBySql = $orderBy ? $this->getOrderBySQL($orderBy, $baseTableAlias) : ''; - if ($this->_selectColumnListSql === null) { - $this->_selectColumnListSql = $columnList; + if ($this->selectColumnListSql === null) { + $this->selectColumnListSql = $columnList; } $lockSql = ''; if ($lockMode == LockMode::PESSIMISTIC_READ) { - $lockSql = ' ' . $this->_platform->getReadLockSql(); + $lockSql = ' ' . $this->platform->getReadLockSql(); } else if ($lockMode == LockMode::PESSIMISTIC_WRITE) { - $lockSql = ' ' . $this->_platform->getWriteLockSql(); + $lockSql = ' ' . $this->platform->getWriteLockSql(); } - return $this->_platform->modifyLimitQuery('SELECT ' . $this->_selectColumnListSql - . ' FROM ' . $this->quoteStrategy->getTableName($this->_class, $this->_platform) . ' ' . $baseTableAlias + return $this->platform->modifyLimitQuery('SELECT ' . $this->selectColumnListSql + . ' FROM ' . $this->quoteStrategy->getTableName($this->class, $this->platform) . ' ' . $baseTableAlias . $joinSql . ($conditionSql != '' ? ' WHERE ' . $conditionSql : '') . $orderBySql, $limit, $offset) . $lockSql; @@ -417,16 +417,16 @@ class JoinedSubclassPersister extends AbstractEntityInheritancePersister */ public function getLockTablesSql() { - $idColumns = $this->_class->getIdentifierColumnNames(); - $baseTableAlias = $this->_getSQLTableAlias($this->_class->name); + $idColumns = $this->class->getIdentifierColumnNames(); + $baseTableAlias = $this->getSQLTableAlias($this->class->name); // INNER JOIN parent tables $joinSql = ''; - foreach ($this->_class->parentClasses as $parentClassName) { - $parentClass = $this->_em->getClassMetadata($parentClassName); - $tableAlias = $this->_getSQLTableAlias($parentClassName); - $joinSql .= ' INNER JOIN ' . $this->quoteStrategy->getTableName($parentClass, $this->_platform) . ' ' . $tableAlias . ' ON '; + foreach ($this->class->parentClasses as $parentClassName) { + $parentClass = $this->em->getClassMetadata($parentClassName); + $tableAlias = $this->getSQLTableAlias($parentClassName); + $joinSql .= ' INNER JOIN ' . $this->quoteStrategy->getTableName($parentClass, $this->platform) . ' ' . $tableAlias . ' ON '; $first = true; foreach ($idColumns as $idColumn) { @@ -436,45 +436,45 @@ class JoinedSubclassPersister extends AbstractEntityInheritancePersister } } - return 'FROM ' .$this->quoteStrategy->getTableName($this->_class, $this->_platform) . ' ' . $baseTableAlias . $joinSql; + return 'FROM ' .$this->quoteStrategy->getTableName($this->class, $this->platform) . ' ' . $baseTableAlias . $joinSql; } /* Ensure this method is never called. This persister overrides _getSelectEntitiesSQL directly. */ - protected function _getSelectColumnListSQL() + protected function getSelectColumnListSQL() { throw new \BadMethodCallException("Illegal invocation of ".__METHOD__."."); } /** {@inheritdoc} */ - protected function _getInsertColumnList() + protected function getInsertColumnList() { // Identifier columns must always come first in the column list of subclasses. - $columns = $this->_class->parentClasses ? $this->_class->getIdentifierColumnNames() : array(); + $columns = $this->class->parentClasses ? $this->class->getIdentifierColumnNames() : array(); - foreach ($this->_class->reflFields as $name => $field) { - if (isset($this->_class->fieldMappings[$name]['inherited']) && ! isset($this->_class->fieldMappings[$name]['id']) - || isset($this->_class->associationMappings[$name]['inherited']) - || ($this->_class->isVersioned && $this->_class->versionField == $name)) { + foreach ($this->class->reflFields as $name => $field) { + if (isset($this->class->fieldMappings[$name]['inherited']) && ! isset($this->class->fieldMappings[$name]['id']) + || isset($this->class->associationMappings[$name]['inherited']) + || ($this->class->isVersioned && $this->class->versionField == $name)) { continue; } - if (isset($this->_class->associationMappings[$name])) { - $assoc = $this->_class->associationMappings[$name]; + if (isset($this->class->associationMappings[$name])) { + $assoc = $this->class->associationMappings[$name]; if ($assoc['type'] & ClassMetadata::TO_ONE && $assoc['isOwningSide']) { foreach ($assoc['targetToSourceKeyColumns'] as $sourceCol) { $columns[] = $sourceCol; } } - } else if ($this->_class->name != $this->_class->rootEntityName || - ! $this->_class->isIdGeneratorIdentity() || $this->_class->identifier[0] != $name) { - $columns[] = $this->quoteStrategy->getColumnName($name, $this->_class, $this->_platform); - $this->_columnTypes[$name] = $this->_class->fieldMappings[$name]['type']; + } else if ($this->class->name != $this->class->rootEntityName || + ! $this->class->isIdGeneratorIdentity() || $this->class->identifier[0] != $name) { + $columns[] = $this->quoteStrategy->getColumnName($name, $this->class, $this->platform); + $this->columnTypes[$name] = $this->class->fieldMappings[$name]['type']; } } // Add discriminator column if it is the topmost class. - if ($this->_class->name == $this->_class->rootEntityName) { - $columns[] = $this->_class->discriminatorColumn['name']; + if ($this->class->name == $this->class->rootEntityName) { + $columns[] = $this->class->discriminatorColumn['name']; } return $columns; @@ -485,8 +485,8 @@ class JoinedSubclassPersister extends AbstractEntityInheritancePersister */ protected function assignDefaultVersionValue($entity, $id) { - $value = $this->fetchVersionValue($this->_getVersionedClassMetadata(), $id); - $this->_class->setFieldValue($entity, $this->_class->versionField, $value); + $value = $this->fetchVersionValue($this->getVersionedClassMetadata(), $id); + $this->class->setFieldValue($entity, $this->class->versionField, $value); } } diff --git a/lib/Doctrine/ORM/Persisters/SingleTablePersister.php b/lib/Doctrine/ORM/Persisters/SingleTablePersister.php index 9fedcaa00..61cee2432 100644 --- a/lib/Doctrine/ORM/Persisters/SingleTablePersister.php +++ b/lib/Doctrine/ORM/Persisters/SingleTablePersister.php @@ -35,40 +35,40 @@ use Doctrine\Common\Collections\Criteria; class SingleTablePersister extends AbstractEntityInheritancePersister { /** {@inheritdoc} */ - protected function _getDiscriminatorColumnTableName() + protected function getDiscriminatorColumnTableName() { - return $this->_class->getTableName(); + return $this->class->getTableName(); } /** {@inheritdoc} */ - protected function _getSelectColumnListSQL() + protected function getSelectColumnListSQL() { - if ($this->_selectColumnListSql !== null) { - return $this->_selectColumnListSql; + if ($this->selectColumnListSql !== null) { + return $this->selectColumnListSql; } - $columnList = parent::_getSelectColumnListSQL(); + $columnList = parent::getSelectColumnListSQL(); - $rootClass = $this->_em->getClassMetadata($this->_class->rootEntityName); - $tableAlias = $this->_getSQLTableAlias($rootClass->name); + $rootClass = $this->em->getClassMetadata($this->class->rootEntityName); + $tableAlias = $this->getSQLTableAlias($rootClass->name); // Append discriminator column - $discrColumn = $this->_class->discriminatorColumn['name']; + $discrColumn = $this->class->discriminatorColumn['name']; $columnList .= ', ' . $tableAlias . '.' . $discrColumn; - $resultColumnName = $this->_platform->getSQLResultCasing($discrColumn); + $resultColumnName = $this->platform->getSQLResultCasing($discrColumn); - $this->_rsm->setDiscriminatorColumn('r', $resultColumnName); - $this->_rsm->addMetaResult('r', $resultColumnName, $discrColumn); + $this->rsm->setDiscriminatorColumn('r', $resultColumnName); + $this->rsm->addMetaResult('r', $resultColumnName, $discrColumn); // Append subclass columns - foreach ($this->_class->subClasses as $subClassName) { - $subClass = $this->_em->getClassMetadata($subClassName); + foreach ($this->class->subClasses as $subClassName) { + $subClass = $this->em->getClassMetadata($subClassName); // Regular columns foreach ($subClass->fieldMappings as $fieldName => $mapping) { if ( ! isset($mapping['inherited'])) { - $columnList .= ', ' . $this->_getSelectColumnSQL($fieldName, $subClass); + $columnList .= ', ' . $this->getSelectColumnSQL($fieldName, $subClass); } } @@ -81,73 +81,73 @@ class SingleTablePersister extends AbstractEntityInheritancePersister $columnList .= $this->getSelectJoinColumnSQL( $tableAlias, $srcColumn, - isset($assoc['inherited']) ? $assoc['inherited'] : $this->_class->name + isset($assoc['inherited']) ? $assoc['inherited'] : $this->class->name ); } } } } - $this->_selectColumnListSql = $columnList; - return $this->_selectColumnListSql; + $this->selectColumnListSql = $columnList; + return $this->selectColumnListSql; } /** {@inheritdoc} */ - protected function _getInsertColumnList() + protected function getInsertColumnList() { - $columns = parent::_getInsertColumnList(); + $columns = parent::getInsertColumnList(); // Add discriminator column to the INSERT SQL - $columns[] = $this->_class->discriminatorColumn['name']; + $columns[] = $this->class->discriminatorColumn['name']; return $columns; } /** {@inheritdoc} */ - protected function _getSQLTableAlias($className, $assocName = '') + protected function getSQLTableAlias($className, $assocName = '') { - return parent::_getSQLTableAlias($this->_class->rootEntityName, $assocName); + return parent::getSQLTableAlias($this->class->rootEntityName, $assocName); } /** {@inheritdoc} */ - protected function _getSelectConditionSQL(array $criteria, $assoc = null) + protected function getSelectConditionSQL(array $criteria, $assoc = null) { - $conditionSql = parent::_getSelectConditionSQL($criteria, $assoc); + $conditionSql = parent::getSelectConditionSQL($criteria, $assoc); if ($conditionSql) { $conditionSql .= ' AND '; } - return $conditionSql . $this->_getSelectConditionDiscriminatorValueSQL(); + return $conditionSql . $this->getSelectConditionDiscriminatorValueSQL(); } /** {@inheritdoc} */ - protected function _getSelectConditionCriteriaSQL(Criteria $criteria) + protected function getSelectConditionCriteriaSQL(Criteria $criteria) { - $conditionSql = parent::_getSelectConditionCriteriaSQL($criteria); + $conditionSql = parent::getSelectConditionCriteriaSQL($criteria); if ($conditionSql) { $conditionSql .= ' AND '; } - return $conditionSql . $this->_getSelectConditionDiscriminatorValueSQL(); + return $conditionSql . $this->getSelectConditionDiscriminatorValueSQL(); } - protected function _getSelectConditionDiscriminatorValueSQL() + protected function getSelectConditionDiscriminatorValueSQL() { $values = array(); - if ($this->_class->discriminatorValue !== null) { // discriminators can be 0 - $values[] = $this->_conn->quote($this->_class->discriminatorValue); + if ($this->class->discriminatorValue !== null) { // discriminators can be 0 + $values[] = $this->conn->quote($this->class->discriminatorValue); } - $discrValues = array_flip($this->_class->discriminatorMap); + $discrValues = array_flip($this->class->discriminatorMap); - foreach ($this->_class->subClasses as $subclassName) { - $values[] = $this->_conn->quote($discrValues[$subclassName]); + foreach ($this->class->subClasses as $subclassName) { + $values[] = $this->conn->quote($discrValues[$subclassName]); } - return $this->_getSQLTableAlias($this->_class->name) . '.' . $this->_class->discriminatorColumn['name'] + return $this->getSQLTableAlias($this->class->name) . '.' . $this->class->discriminatorColumn['name'] . ' IN (' . implode(', ', $values) . ')'; } @@ -155,7 +155,7 @@ class SingleTablePersister extends AbstractEntityInheritancePersister protected function generateFilterConditionSQL(ClassMetadata $targetEntity, $targetTableAlias) { // Ensure that the filters are applied to the root entity of the inheritance tree - $targetEntity = $this->_em->getClassMetadata($targetEntity->rootEntityName); + $targetEntity = $this->em->getClassMetadata($targetEntity->rootEntityName); // we dont care about the $targetTableAlias, in a STI there is only one table. return parent::generateFilterConditionSQL($targetEntity, $targetTableAlias); diff --git a/tests/Doctrine/Tests/Mocks/EntityPersisterMock.php b/tests/Doctrine/Tests/Mocks/EntityPersisterMock.php index 768e357b1..4c071c751 100644 --- a/tests/Doctrine/Tests/Mocks/EntityPersisterMock.php +++ b/tests/Doctrine/Tests/Mocks/EntityPersisterMock.php @@ -7,12 +7,12 @@ namespace Doctrine\Tests\Mocks; */ class EntityPersisterMock extends \Doctrine\ORM\Persisters\BasicEntityPersister { - private $_inserts = array(); - private $_updates = array(); - private $_deletes = array(); - private $_identityColumnValueCounter = 0; - private $_mockIdGeneratorType; - private $_postInsertIds = array(); + private $inserts = array(); + private $updates = array(); + private $deletes = array(); + private $identityColumnValueCounter = 0; + private $mockIdGeneratorType; + private $postInsertIds = array(); private $existsCalled = false; /** @@ -22,11 +22,11 @@ class EntityPersisterMock extends \Doctrine\ORM\Persisters\BasicEntityPersister */ public function insert($entity) { - $this->_inserts[] = $entity; - if ( ! is_null($this->_mockIdGeneratorType) && $this->_mockIdGeneratorType == \Doctrine\ORM\Mapping\ClassMetadata::GENERATOR_TYPE_IDENTITY - || $this->_class->isIdGeneratorIdentity()) { - $id = $this->_identityColumnValueCounter++; - $this->_postInsertIds[$id] = $entity; + $this->inserts[] = $entity; + if ( ! is_null($this->mockIdGeneratorType) && $this->mockIdGeneratorType == \Doctrine\ORM\Mapping\ClassMetadata::GENERATOR_TYPE_IDENTITY + || $this->class->isIdGeneratorIdentity()) { + $id = $this->identityColumnValueCounter++; + $this->postInsertIds[$id] = $entity; return $id; } return null; @@ -34,11 +34,11 @@ class EntityPersisterMock extends \Doctrine\ORM\Persisters\BasicEntityPersister public function addInsert($entity) { - $this->_inserts[] = $entity; - if ( ! is_null($this->_mockIdGeneratorType) && $this->_mockIdGeneratorType == \Doctrine\ORM\Mapping\ClassMetadata::GENERATOR_TYPE_IDENTITY - || $this->_class->isIdGeneratorIdentity()) { - $id = $this->_identityColumnValueCounter++; - $this->_postInsertIds[$id] = $entity; + $this->inserts[] = $entity; + if ( ! is_null($this->mockIdGeneratorType) && $this->mockIdGeneratorType == \Doctrine\ORM\Mapping\ClassMetadata::GENERATOR_TYPE_IDENTITY + || $this->class->isIdGeneratorIdentity()) { + $id = $this->identityColumnValueCounter++; + $this->postInsertIds[$id] = $entity; return $id; } return null; @@ -46,17 +46,17 @@ class EntityPersisterMock extends \Doctrine\ORM\Persisters\BasicEntityPersister public function executeInserts() { - return $this->_postInsertIds; + return $this->postInsertIds; } public function setMockIdGeneratorType($genType) { - $this->_mockIdGeneratorType = $genType; + $this->mockIdGeneratorType = $genType; } public function update($entity) { - $this->_updates[] = $entity; + $this->updates[] = $entity; } public function exists($entity, array $extraConditions = array()) @@ -66,31 +66,31 @@ class EntityPersisterMock extends \Doctrine\ORM\Persisters\BasicEntityPersister public function delete($entity) { - $this->_deletes[] = $entity; + $this->deletes[] = $entity; } public function getInserts() { - return $this->_inserts; + return $this->inserts; } public function getUpdates() { - return $this->_updates; + return $this->updates; } public function getDeletes() { - return $this->_deletes; + return $this->deletes; } public function reset() { $this->existsCalled = false; - $this->_identityColumnValueCounter = 0; - $this->_inserts = array(); - $this->_updates = array(); - $this->_deletes = array(); + $this->identityColumnValueCounter = 0; + $this->inserts = array(); + $this->updates = array(); + $this->deletes = array(); } public function isExistsCalled() diff --git a/tests/Doctrine/Tests/ORM/Persisters/BasicEntityPersisterTypeValueSqlTest.php b/tests/Doctrine/Tests/ORM/Persisters/BasicEntityPersisterTypeValueSqlTest.php index fa1d7530a..6ba86f228 100644 --- a/tests/Doctrine/Tests/ORM/Persisters/BasicEntityPersisterTypeValueSqlTest.php +++ b/tests/Doctrine/Tests/ORM/Persisters/BasicEntityPersisterTypeValueSqlTest.php @@ -38,7 +38,7 @@ class BasicEntityPersisterTypeValueSqlTest extends \Doctrine\Tests\OrmTestCase public function testGetInsertSQLUsesTypeValuesSQL() { - $method = new \ReflectionMethod($this->_persister, '_getInsertSQL'); + $method = new \ReflectionMethod($this->_persister, 'getInsertSQL'); $method->setAccessible(true); $sql = $method->invoke($this->_persister); @@ -69,7 +69,7 @@ class BasicEntityPersisterTypeValueSqlTest extends \Doctrine\Tests\OrmTestCase public function testGetSelectConditionSQLUsesTypeValuesSQL() { - $method = new \ReflectionMethod($this->_persister, '_getSelectConditionSQL'); + $method = new \ReflectionMethod($this->_persister, 'getSelectConditionSQL'); $method->setAccessible(true); $sql = $method->invoke($this->_persister, array('customInteger' => 1, 'child' => 1)); @@ -83,7 +83,7 @@ class BasicEntityPersisterTypeValueSqlTest extends \Doctrine\Tests\OrmTestCase public function testStripNonAlphanumericCharactersFromSelectColumnListSQL() { $persister = new BasicEntityPersister($this->_em, $this->_em->getClassMetadata('Doctrine\Tests\Models\Quote\SimpleEntity')); - $method = new \ReflectionMethod($persister, '_getSelectColumnListSQL'); + $method = new \ReflectionMethod($persister, 'getSelectColumnListSQL'); $method->setAccessible(true); $this->assertEquals('t0."simple-entity-id" AS simpleentityid1, t0."simple-entity-value" AS simpleentityvalue2', $method->invoke($persister)); From e6f08f0b9219bfb948c07b24f7fef5257954edf7 Mon Sep 17 00:00:00 2001 From: "Fabio B. Silva" Date: Sun, 22 Jul 2012 15:26:00 -0300 Subject: [PATCH 02/16] remove '_' prefix at AbstractCollectionPersister --- .../AbstractCollectionPersister.php | 128 +++++++++++++----- .../ORM/Persisters/BasicEntityPersister.php | 2 +- .../Persisters/JoinedSubclassPersister.php | 8 +- .../ORM/Persisters/ManyToManyPersister.php | 108 ++++++++------- .../ORM/Persisters/OneToManyPersister.php | 100 +++++++------- 5 files changed, 205 insertions(+), 141 deletions(-) diff --git a/lib/Doctrine/ORM/Persisters/AbstractCollectionPersister.php b/lib/Doctrine/ORM/Persisters/AbstractCollectionPersister.php index a83c7d892..bd32fcc05 100644 --- a/lib/Doctrine/ORM/Persisters/AbstractCollectionPersister.php +++ b/lib/Doctrine/ORM/Persisters/AbstractCollectionPersister.php @@ -33,17 +33,17 @@ abstract class AbstractCollectionPersister /** * @var EntityManager */ - protected $_em; + protected $em; /** * @var \Doctrine\DBAL\Connection */ - protected $_conn; + protected $conn; /** * @var \Doctrine\ORM\UnitOfWork */ - protected $_uow; + protected $uow; /** * The database platform. @@ -66,17 +66,17 @@ abstract class AbstractCollectionPersister */ public function __construct(EntityManager $em) { - $this->_em = $em; - $this->_uow = $em->getUnitOfWork(); - $this->_conn = $em->getConnection(); - $this->platform = $this->_conn->getDatabasePlatform(); + $this->em = $em; + $this->uow = $em->getUnitOfWork(); + $this->conn = $em->getConnection(); + $this->platform = $this->conn->getDatabasePlatform(); $this->quoteStrategy = $em->getConfiguration()->getQuoteStrategy(); } /** * Deletes the persistent state represented by the given collection. * - * @param PersistentCollection $coll + * @param \Doctrine\ORM\PersistentCollection $coll */ public function delete(PersistentCollection $coll) { @@ -86,30 +86,30 @@ abstract class AbstractCollectionPersister return; // ignore inverse side } - $sql = $this->_getDeleteSQL($coll); - $this->_conn->executeUpdate($sql, $this->_getDeleteSQLParameters($coll)); + $sql = $this->getDeleteSQL($coll); + $this->conn->executeUpdate($sql, $this->getDeleteSQLParameters($coll)); } /** * Gets the SQL statement for deleting the given collection. * - * @param PersistentCollection $coll + * @param \Doctrine\ORM\PersistentCollection $coll */ - abstract protected function _getDeleteSQL(PersistentCollection $coll); + abstract protected function getDeleteSQL(PersistentCollection $coll); /** * Gets the SQL parameters for the corresponding SQL statement to delete * the given collection. * - * @param PersistentCollection $coll + * @param \Doctrine\ORM\PersistentCollection $coll */ - abstract protected function _getDeleteSQLParameters(PersistentCollection $coll); + abstract protected function getDeleteSQLParameters(PersistentCollection $coll); /** * Updates the given collection, synchronizing it's state with the database * by inserting, updating and deleting individual elements. * - * @param PersistentCollection $coll + * @param \Doctrine\ORM\PersistentCollection $coll */ public function update(PersistentCollection $coll) { @@ -120,63 +120,117 @@ abstract class AbstractCollectionPersister } $this->deleteRows($coll); - //$this->updateRows($coll); $this->insertRows($coll); } + /** + * Delete rows + * + * @param \Doctrine\ORM\PersistentCollection $coll + */ public function deleteRows(PersistentCollection $coll) { - $deleteDiff = $coll->getDeleteDiff(); - $sql = $this->_getDeleteRowSQL($coll); + $diff = $coll->getDeleteDiff(); + $sql = $this->getDeleteRowSQL($coll); - foreach ($deleteDiff as $element) { - $this->_conn->executeUpdate($sql, $this->_getDeleteRowSQLParameters($coll, $element)); + foreach ($diff as $element) { + $this->conn->executeUpdate($sql, $this->getDeleteRowSQLParameters($coll, $element)); } } - //public function updateRows(PersistentCollection $coll) - //{} - + /** + * Insert rows + * + * @param \Doctrine\ORM\PersistentCollection $coll + */ public function insertRows(PersistentCollection $coll) { - $insertDiff = $coll->getInsertDiff(); - $sql = $this->_getInsertRowSQL($coll); + $diff = $coll->getInsertDiff(); + $sql = $this->getInsertRowSQL($coll); - foreach ($insertDiff as $element) { - $this->_conn->executeUpdate($sql, $this->_getInsertRowSQLParameters($coll, $element)); + foreach ($diff as $element) { + $this->conn->executeUpdate($sql, $this->getInsertRowSQLParameters($coll, $element)); } } + /** + * Count the size of this persistent collection + * + * @param \Doctrine\ORM\PersistentCollection $coll + * @return integer + */ public function count(PersistentCollection $coll) { throw new \BadMethodCallException("Counting the size of this persistent collection is not supported by this CollectionPersister."); } + /** + * Slice elements + * + * @param \Doctrine\ORM\PersistentCollection $coll + * @param integer $offset + * @param integer $length + * @return array + */ public function slice(PersistentCollection $coll, $offset, $length = null) { throw new \BadMethodCallException("Slicing elements is not supported by this CollectionPersister."); } + /** + * Check for existance of an element + * + * @param \Doctrine\ORM\PersistentCollection $coll + * @param mixed \Doctrine\ORM\PersistentCollection + * @return boolean + */ public function contains(PersistentCollection $coll, $element) { throw new \BadMethodCallException("Checking for existance of an element is not supported by this CollectionPersister."); } + /** + * Check for existance of a key + * + * @param \Doctrine\ORM\PersistentCollection $coll + * @param mixed $key + * @return boolean + */ public function containsKey(PersistentCollection $coll, $key) { throw new \BadMethodCallException("Checking for existance of a key is not supported by this CollectionPersister."); } + /** + * Remove an element + * + * @param \Doctrine\ORM\PersistentCollection $coll + * @param object $element + * @return mixed + */ public function removeElement(PersistentCollection $coll, $element) { throw new \BadMethodCallException("Removing an element is not supported by this CollectionPersister."); } + /** + * Remove an element by key + * + * @param \Doctrine\ORM\PersistentCollection $coll + * @param mixed $key + */ public function removeKey(PersistentCollection $coll, $key) { throw new \BadMethodCallException("Removing a key is not supported by this CollectionPersister."); } + /** + * Get an element by key + * + * @param \Doctrine\ORM\PersistentCollection $coll + * @param mixed $index + * @return object + */ public function get(PersistentCollection $coll, $index) { throw new \BadMethodCallException("Selecting a collection by index is not supported by this CollectionPersister."); @@ -185,39 +239,39 @@ abstract class AbstractCollectionPersister /** * Gets the SQL statement used for deleting a row from the collection. * - * @param PersistentCollection $coll + * @param \Doctrine\ORM\PersistentCollection $coll */ - abstract protected function _getDeleteRowSQL(PersistentCollection $coll); + abstract protected function getDeleteRowSQL(PersistentCollection $coll); /** * Gets the SQL parameters for the corresponding SQL statement to delete the given * element from the given collection. * - * @param PersistentCollection $coll + * @param \Doctrine\ORM\PersistentCollection $coll * @param mixed $element */ - abstract protected function _getDeleteRowSQLParameters(PersistentCollection $coll, $element); + abstract protected function getDeleteRowSQLParameters(PersistentCollection $coll, $element); /** * Gets the SQL statement used for updating a row in the collection. * - * @param PersistentCollection $coll + * @param \Doctrine\ORM\PersistentCollection $coll */ - abstract protected function _getUpdateRowSQL(PersistentCollection $coll); + abstract protected function getUpdateRowSQL(PersistentCollection $coll); /** * Gets the SQL statement used for inserting a row in the collection. * - * @param PersistentCollection $coll + * @param \Doctrine\ORM\PersistentCollection $coll */ - abstract protected function _getInsertRowSQL(PersistentCollection $coll); + abstract protected function getInsertRowSQL(PersistentCollection $coll); /** * Gets the SQL parameters for the corresponding SQL statement to insert the given * element of the given collection into the database. * - * @param PersistentCollection $coll + * @param \Doctrine\ORM\PersistentCollection $coll * @param mixed $element */ - abstract protected function _getInsertRowSQLParameters(PersistentCollection $coll, $element); + abstract protected function getInsertRowSQLParameters(PersistentCollection $coll, $element); } diff --git a/lib/Doctrine/ORM/Persisters/BasicEntityPersister.php b/lib/Doctrine/ORM/Persisters/BasicEntityPersister.php index bcefb18cc..52f0411cc 100644 --- a/lib/Doctrine/ORM/Persisters/BasicEntityPersister.php +++ b/lib/Doctrine/ORM/Persisters/BasicEntityPersister.php @@ -338,7 +338,7 @@ class BasicEntityPersister * * Subclasses are also supposed to take care of versioning when overriding this method, * if necessary. The {@link updateTable} method can be used to apply the data retrieved - * from {@_prepareUpdateData} on the target tables, thereby optionally applying versioning. + * from {@prepareUpdateData} on the target tables, thereby optionally applying versioning. * * @param object $entity The entity to update. */ diff --git a/lib/Doctrine/ORM/Persisters/JoinedSubclassPersister.php b/lib/Doctrine/ORM/Persisters/JoinedSubclassPersister.php index 129adbc04..0843cbb0d 100644 --- a/lib/Doctrine/ORM/Persisters/JoinedSubclassPersister.php +++ b/lib/Doctrine/ORM/Persisters/JoinedSubclassPersister.php @@ -439,13 +439,17 @@ class JoinedSubclassPersister extends AbstractEntityInheritancePersister return 'FROM ' .$this->quoteStrategy->getTableName($this->class, $this->platform) . ' ' . $baseTableAlias . $joinSql; } - /* Ensure this method is never called. This persister overrides _getSelectEntitiesSQL directly. */ + /* + * Ensure this method is never called. This persister overrides getSelectEntitiesSQL directly. + */ protected function getSelectColumnListSQL() { throw new \BadMethodCallException("Illegal invocation of ".__METHOD__."."); } - /** {@inheritdoc} */ + /** + * {@inheritdoc} + */ protected function getInsertColumnList() { // Identifier columns must always come first in the column list of subclasses. diff --git a/lib/Doctrine/ORM/Persisters/ManyToManyPersister.php b/lib/Doctrine/ORM/Persisters/ManyToManyPersister.php index 1d77643d7..d8d9b7bf4 100644 --- a/lib/Doctrine/ORM/Persisters/ManyToManyPersister.php +++ b/lib/Doctrine/ORM/Persisters/ManyToManyPersister.php @@ -38,11 +38,11 @@ class ManyToManyPersister extends AbstractCollectionPersister * * @override */ - protected function _getDeleteRowSQL(PersistentCollection $coll) + protected function getDeleteRowSQL(PersistentCollection $coll) { $columns = array(); $mapping = $coll->getMapping(); - $class = $this->_em->getClassMetadata(get_class($coll->getOwner())); + $class = $this->em->getClassMetadata(get_class($coll->getOwner())); foreach ($mapping['joinTable']['joinColumns'] as $joinColumn) { $columns[] = $this->quoteStrategy->getJoinColumnName($joinColumn, $class, $this->platform); @@ -60,34 +60,34 @@ class ManyToManyPersister extends AbstractCollectionPersister * {@inheritdoc} * * @override - * @internal Order of the parameters must be the same as the order of the columns in - * _getDeleteRowSql. + * @internal Order of the parameters must be the same as the order of the columns in getDeleteRowSql. */ - protected function _getDeleteRowSQLParameters(PersistentCollection $coll, $element) + protected function getDeleteRowSQLParameters(PersistentCollection $coll, $element) { - return $this->_collectJoinTableColumnParameters($coll, $element); + return $this->collectJoinTableColumnParameters($coll, $element); + } + + /** + * {@inheritdoc} + * + * @throws \BadMethodCallException Not used for OneToManyPersister + */ + protected function getUpdateRowSQL(PersistentCollection $coll) + { + throw new \BadMethodCallException("Insert Row SQL is not used for ManyToManyPersister"); } /** * {@inheritdoc} * * @override + * @internal Order of the parameters must be the same as the order of the columns in getInsertRowSql. */ - protected function _getUpdateRowSQL(PersistentCollection $coll) - {} - - /** - * {@inheritdoc} - * - * @override - * @internal Order of the parameters must be the same as the order of the columns in - * _getInsertRowSql. - */ - protected function _getInsertRowSQL(PersistentCollection $coll) + protected function getInsertRowSQL(PersistentCollection $coll) { $columns = array(); $mapping = $coll->getMapping(); - $class = $this->_em->getClassMetadata(get_class($coll->getOwner())); + $class = $this->em->getClassMetadata(get_class($coll->getOwner())); $joinTable = $this->quoteStrategy->getJoinTableName($mapping, $class, $this->platform); foreach ($mapping['joinTable']['joinColumns'] as $joinColumn) { @@ -106,33 +106,32 @@ class ManyToManyPersister extends AbstractCollectionPersister * {@inheritdoc} * * @override - * @internal Order of the parameters must be the same as the order of the columns in - * _getInsertRowSql. + * @internal Order of the parameters must be the same as the order of the columns in getInsertRowSql. */ - protected function _getInsertRowSQLParameters(PersistentCollection $coll, $element) + protected function getInsertRowSQLParameters(PersistentCollection $coll, $element) { - return $this->_collectJoinTableColumnParameters($coll, $element); + return $this->collectJoinTableColumnParameters($coll, $element); } /** * Collects the parameters for inserting/deleting on the join table in the order * of the join table columns as specified in ManyToManyMapping#joinTableColumns. * - * @param $coll - * @param $element + * @param \Doctrine\ORM\PersistentCollection $coll + * @param object $element * @return array */ - private function _collectJoinTableColumnParameters(PersistentCollection $coll, $element) + private function collectJoinTableColumnParameters(PersistentCollection $coll, $element) { $params = array(); $mapping = $coll->getMapping(); $isComposite = count($mapping['joinTableColumns']) > 2; - $identifier1 = $this->_uow->getEntityIdentifier($coll->getOwner()); - $identifier2 = $this->_uow->getEntityIdentifier($element); + $identifier1 = $this->uow->getEntityIdentifier($coll->getOwner()); + $identifier2 = $this->uow->getEntityIdentifier($element); if ($isComposite) { - $class1 = $this->_em->getClassMetadata(get_class($coll->getOwner())); + $class1 = $this->em->getClassMetadata(get_class($coll->getOwner())); $class2 = $coll->getTypeClass(); } @@ -162,11 +161,11 @@ class ManyToManyPersister extends AbstractCollectionPersister * * @override */ - protected function _getDeleteSQL(PersistentCollection $coll) + protected function getDeleteSQL(PersistentCollection $coll) { $columns = array(); $mapping = $coll->getMapping(); - $class = $this->_em->getClassMetadata(get_class($coll->getOwner())); + $class = $this->em->getClassMetadata(get_class($coll->getOwner())); $joinTable = $this->quoteStrategy->getJoinTableName($mapping, $class, $this->platform); foreach ($mapping['joinTable']['joinColumns'] as $joinColumn) { @@ -181,12 +180,11 @@ class ManyToManyPersister extends AbstractCollectionPersister * {@inheritdoc} * * @override - * @internal Order of the parameters must be the same as the order of the columns in - * _getDeleteSql. + * @internal Order of the parameters must be the same as the order of the columns in getDeleteSql. */ - protected function _getDeleteSQLParameters(PersistentCollection $coll) + protected function getDeleteSQLParameters(PersistentCollection $coll) { - $identifier = $this->_uow->getEntityIdentifier($coll->getOwner()); + $identifier = $this->uow->getEntityIdentifier($coll->getOwner()); $mapping = $coll->getMapping(); $params = array(); @@ -198,7 +196,7 @@ class ManyToManyPersister extends AbstractCollectionPersister } // Composite identifier - $sourceClass = $this->_em->getClassMetadata(get_class($coll->getOwner())); + $sourceClass = $this->em->getClassMetadata(get_class($coll->getOwner())); foreach ($mapping['relationToSourceKeyColumns'] as $srcColumn) { $params[] = $identifier[$sourceClass->fieldNames[$srcColumn]]; @@ -216,11 +214,11 @@ class ManyToManyPersister extends AbstractCollectionPersister $params = array(); $mapping = $coll->getMapping(); $association = $mapping; - $class = $this->_em->getClassMetadata($mapping['sourceEntity']); - $id = $this->_em->getUnitOfWork()->getEntityIdentifier($coll->getOwner()); + $class = $this->em->getClassMetadata($mapping['sourceEntity']); + $id = $this->em->getUnitOfWork()->getEntityIdentifier($coll->getOwner()); if ( ! $mapping['isOwningSide']) { - $targetEntity = $this->_em->getClassMetadata($mapping['targetEntity']); + $targetEntity = $this->em->getClassMetadata($mapping['targetEntity']); $association = $targetEntity->associationMappings[$mapping['mappedBy']]; } @@ -249,11 +247,11 @@ class ManyToManyPersister extends AbstractCollectionPersister . $joinTargetEntitySQL . ' WHERE ' . implode(' AND ', $conditions); - return $this->_conn->fetchColumn($sql, $params); + return $this->conn->fetchColumn($sql, $params); } /** - * @param PersistentCollection $coll + * @param \Doctrine\ORM\PersistentCollection $coll * @param int $offset * @param int $length * @return array @@ -262,17 +260,17 @@ class ManyToManyPersister extends AbstractCollectionPersister { $mapping = $coll->getMapping(); - return $this->_em->getUnitOfWork()->getEntityPersister($mapping['targetEntity'])->getManyToManyCollection($mapping, $coll->getOwner(), $offset, $length); + return $this->em->getUnitOfWork()->getEntityPersister($mapping['targetEntity'])->getManyToManyCollection($mapping, $coll->getOwner(), $offset, $length); } /** - * @param PersistentCollection $coll + * @param \Doctrine\ORM\PersistentCollection $coll * @param object $element * @return boolean */ public function contains(PersistentCollection $coll, $element) { - $uow = $this->_em->getUnitOfWork(); + $uow = $this->em->getUnitOfWork(); // Shortcut for new entities $entityState = $uow->getEntityState($element, UnitOfWork::STATE_NEW); @@ -290,17 +288,17 @@ class ManyToManyPersister extends AbstractCollectionPersister $sql = 'SELECT 1 FROM ' . $quotedJoinTable . ' WHERE ' . implode(' AND ', $whereClauses); - return (bool) $this->_conn->fetchColumn($sql, $params); + return (bool) $this->conn->fetchColumn($sql, $params); } /** - * @param PersistentCollection $coll + * @param \Doctrine\ORM\PersistentCollection $coll * @param object $element * @return boolean */ public function removeElement(PersistentCollection $coll, $element) { - $uow = $this->_em->getUnitOfWork(); + $uow = $this->em->getUnitOfWork(); // shortcut for new entities $entityState = $uow->getEntityState($element, UnitOfWork::STATE_NEW); @@ -319,7 +317,7 @@ class ManyToManyPersister extends AbstractCollectionPersister $sql = 'DELETE FROM ' . $quotedJoinTable . ' WHERE ' . implode(' AND ', $whereClauses); - return (bool) $this->_conn->executeUpdate($sql, $params); + return (bool) $this->conn->executeUpdate($sql, $params); } /** @@ -330,19 +328,19 @@ class ManyToManyPersister extends AbstractCollectionPersister */ private function getJoinTableRestrictions(PersistentCollection $coll, $element, $addFilters) { - $uow = $this->_em->getUnitOfWork(); + $uow = $this->em->getUnitOfWork(); $mapping = $filterMapping = $coll->getMapping(); if ( ! $mapping['isOwningSide']) { - $sourceClass = $this->_em->getClassMetadata($mapping['targetEntity']); - $targetClass = $this->_em->getClassMetadata($mapping['sourceEntity']); + $sourceClass = $this->em->getClassMetadata($mapping['targetEntity']); + $targetClass = $this->em->getClassMetadata($mapping['sourceEntity']); $sourceId = $uow->getEntityIdentifier($element); $targetId = $uow->getEntityIdentifier($coll->getOwner()); $mapping = $sourceClass->associationMappings[$mapping['mappedBy']]; } else { - $sourceClass = $this->_em->getClassMetadata($mapping['sourceEntity']); - $targetClass = $this->_em->getClassMetadata($mapping['targetEntity']); + $sourceClass = $this->em->getClassMetadata($mapping['sourceEntity']); + $targetClass = $this->em->getClassMetadata($mapping['targetEntity']); $sourceId = $uow->getEntityIdentifier($coll->getOwner()); $targetId = $uow->getEntityIdentifier($element); } @@ -393,7 +391,7 @@ class ManyToManyPersister extends AbstractCollectionPersister */ public function getFilterSql($mapping) { - $targetClass = $this->_em->getClassMetadata($mapping['targetEntity']); + $targetClass = $this->em->getClassMetadata($mapping['targetEntity']); if ($mapping['isOwningSide']) { $joinColumns = $mapping['relationToTargetKeyColumns']; @@ -402,7 +400,7 @@ class ManyToManyPersister extends AbstractCollectionPersister $joinColumns = $mapping['relationToSourceKeyColumns']; } - $targetClass = $this->_em->getClassMetadata($targetClass->rootEntityName); + $targetClass = $this->em->getClassMetadata($targetClass->rootEntityName); // A join is needed if there is filtering on the target entity $joinTargetEntitySQL = ''; @@ -434,7 +432,7 @@ class ManyToManyPersister extends AbstractCollectionPersister { $filterClauses = array(); - foreach ($this->_em->getFilters()->getEnabledFilters() as $filter) { + foreach ($this->em->getFilters()->getEnabledFilters() as $filter) { if ($filterExpr = $filter->addFilterConstraint($targetEntity, $targetTableAlias)) { $filterClauses[] = '(' . $filterExpr . ')'; } diff --git a/lib/Doctrine/ORM/Persisters/OneToManyPersister.php b/lib/Doctrine/ORM/Persisters/OneToManyPersister.php index 2e587f270..c88d755b3 100644 --- a/lib/Doctrine/ORM/Persisters/OneToManyPersister.php +++ b/lib/Doctrine/ORM/Persisters/OneToManyPersister.php @@ -36,14 +36,14 @@ class OneToManyPersister extends AbstractCollectionPersister * Generates the SQL UPDATE that updates a particular row's foreign * key to null. * - * @param PersistentCollection $coll + * @param \Doctrine\ORM\PersistentCollection $coll * @return string * @override */ - protected function _getDeleteRowSQL(PersistentCollection $coll) + protected function getDeleteRowSQL(PersistentCollection $coll) { $mapping = $coll->getMapping(); - $class = $this->_em->getClassMetadata($mapping['targetEntity']); + $class = $this->em->getClassMetadata($mapping['targetEntity']); return 'DELETE FROM ' . $this->quoteStrategy->getTableName($class, $this->platform) . ' WHERE ' . implode('= ? AND ', $class->getIdentifierColumnNames()) . ' = ?'; @@ -51,52 +51,60 @@ class OneToManyPersister extends AbstractCollectionPersister /** * {@inheritdoc} - * */ - protected function _getDeleteRowSQLParameters(PersistentCollection $coll, $element) + protected function getDeleteRowSQLParameters(PersistentCollection $coll, $element) { - return array_values($this->_uow->getEntityIdentifier($element)); - } - - protected function _getInsertRowSQL(PersistentCollection $coll) - { - return "UPDATE xxx SET foreign_key = yyy WHERE foreign_key = zzz"; + return array_values($this->uow->getEntityIdentifier($element)); } /** - * Gets the SQL parameters for the corresponding SQL statement to insert the given - * element of the given collection into the database. - * - * @param PersistentCollection $coll - * @param mixed $element + * {@inheritdoc} + * @throws \BadMethodCallException Not used for OneToManyPersister */ - protected function _getInsertRowSQLParameters(PersistentCollection $coll, $element) - {} - - /* Not used for OneToManyPersister */ - protected function _getUpdateRowSQL(PersistentCollection $coll) + protected function getInsertRowSQL(PersistentCollection $coll) { - return; + throw new \BadMethodCallException("Insert Row SQL is not used for OneToManyPersister"); } /** - * Generates the SQL UPDATE that updates all the foreign keys to null. - * - * @param PersistentCollection $coll + * {@inheritdoc} + * + * @throws \BadMethodCallException Not used for OneToManyPersister */ - protected function _getDeleteSQL(PersistentCollection $coll) + protected function getInsertRowSQLParameters(PersistentCollection $coll, $element) { - + throw new \BadMethodCallException("Insert Row SQL is not used for OneToManyPersister"); } /** - * Gets the SQL parameters for the corresponding SQL statement to delete - * the given collection. + * {@inheritdoc} * - * @param PersistentCollection $coll + * @throws \BadMethodCallException Not used for OneToManyPersister */ - protected function _getDeleteSQLParameters(PersistentCollection $coll) - {} + protected function getUpdateRowSQL(PersistentCollection $coll) + { + throw new \BadMethodCallException("Update Row SQL is not used for OneToManyPersister"); + } + + /** + * {@inheritdoc} + * + * @throws \BadMethodCallException Not used for OneToManyPersister + */ + protected function getDeleteSQL(PersistentCollection $coll) + { + throw new \BadMethodCallException("Update Row SQL is not used for OneToManyPersister"); + } + + /** + * {@inheritdoc} + * + * @throws \BadMethodCallException Not used for OneToManyPersister + */ + protected function getDeleteSQLParameters(PersistentCollection $coll) + { + throw new \BadMethodCallException("Update Row SQL is not used for OneToManyPersister"); + } /** * {@inheritdoc} @@ -104,9 +112,9 @@ class OneToManyPersister extends AbstractCollectionPersister public function count(PersistentCollection $coll) { $mapping = $coll->getMapping(); - $targetClass = $this->_em->getClassMetadata($mapping['targetEntity']); - $sourceClass = $this->_em->getClassMetadata($mapping['sourceEntity']); - $id = $this->_em->getUnitOfWork()->getEntityIdentifier($coll->getOwner()); + $targetClass = $this->em->getClassMetadata($mapping['targetEntity']); + $sourceClass = $this->em->getClassMetadata($mapping['sourceEntity']); + $id = $this->em->getUnitOfWork()->getEntityIdentifier($coll->getOwner()); $whereClauses = array(); $params = array(); @@ -119,8 +127,8 @@ class OneToManyPersister extends AbstractCollectionPersister : $id[$sourceClass->fieldNames[$joinColumn['referencedColumnName']]]; } - $filterTargetClass = $this->_em->getClassMetadata($targetClass->rootEntityName); - foreach ($this->_em->getFilters()->getEnabledFilters() as $filter) { + $filterTargetClass = $this->em->getClassMetadata($targetClass->rootEntityName); + foreach ($this->em->getFilters()->getEnabledFilters() as $filter) { if ($filterExpr = $filter->addFilterConstraint($filterTargetClass, 't')) { $whereClauses[] = '(' . $filterExpr . ')'; } @@ -130,11 +138,11 @@ class OneToManyPersister extends AbstractCollectionPersister . ' FROM ' . $this->quoteStrategy->getTableName($targetClass, $this->platform) . ' t' . ' WHERE ' . implode(' AND ', $whereClauses); - return $this->_conn->fetchColumn($sql, $params); + return $this->conn->fetchColumn($sql, $params); } /** - * @param PersistentCollection $coll + * @param \Doctrine\ORM\PersistentCollection $coll * @param int $offset * @param int $length * @return \Doctrine\Common\Collections\ArrayCollection @@ -142,21 +150,21 @@ class OneToManyPersister extends AbstractCollectionPersister public function slice(PersistentCollection $coll, $offset, $length = null) { $mapping = $coll->getMapping(); - $uow = $this->_em->getUnitOfWork(); + $uow = $this->em->getUnitOfWork(); $persister = $uow->getEntityPersister($mapping['targetEntity']); return $persister->getOneToManyCollection($mapping, $coll->getOwner(), $offset, $length); } /** - * @param PersistentCollection $coll + * @param \Doctrine\ORM\PersistentCollection $coll * @param object $element * @return boolean */ public function contains(PersistentCollection $coll, $element) { $mapping = $coll->getMapping(); - $uow = $this->_em->getUnitOfWork(); + $uow = $this->em->getUnitOfWork(); // shortcut for new entities $entityState = $uow->getEntityState($element, UnitOfWork::STATE_NEW); @@ -181,13 +189,13 @@ class OneToManyPersister extends AbstractCollectionPersister } /** - * @param PersistentCollection $coll + * @param \Doctrine\ORM\PersistentCollection $coll * @param object $element * @return boolean */ public function removeElement(PersistentCollection $coll, $element) { - $uow = $this->_em->getUnitOfWork(); + $uow = $this->em->getUnitOfWork(); // shortcut for new entities $entityState = $uow->getEntityState($element, UnitOfWork::STATE_NEW); @@ -203,10 +211,10 @@ class OneToManyPersister extends AbstractCollectionPersister } $mapping = $coll->getMapping(); - $class = $this->_em->getClassMetadata($mapping['targetEntity']); + $class = $this->em->getClassMetadata($mapping['targetEntity']); $sql = 'DELETE FROM ' . $this->quoteStrategy->getTableName($class, $this->platform) . ' WHERE ' . implode('= ? AND ', $class->getIdentifierColumnNames()) . ' = ?'; - return (bool) $this->_conn->executeUpdate($sql, $this->_getDeleteRowSQLParameters($coll, $element)); + return (bool) $this->conn->executeUpdate($sql, $this->getDeleteRowSQLParameters($coll, $element)); } } From 7e348b7815d8d0d161a53dcb617c084b92be212e Mon Sep 17 00:00:00 2001 From: "Fabio B. Silva" Date: Sun, 22 Jul 2012 15:31:26 -0300 Subject: [PATCH 03/16] small refacory on AbstractEntityInheritancePersister --- .../Persisters/AbstractEntityInheritancePersister.php | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/lib/Doctrine/ORM/Persisters/AbstractEntityInheritancePersister.php b/lib/Doctrine/ORM/Persisters/AbstractEntityInheritancePersister.php index e12cb3d4b..d0f7151f3 100644 --- a/lib/Doctrine/ORM/Persisters/AbstractEntityInheritancePersister.php +++ b/lib/Doctrine/ORM/Persisters/AbstractEntityInheritancePersister.php @@ -60,14 +60,17 @@ abstract class AbstractEntityInheritancePersister extends BasicEntityPersister */ protected function getSelectColumnSQL($field, ClassMetadata $class, $alias = 'r') { - $columnName = $class->columnNames[$field]; - $sql = $this->getSQLTableAlias($class->name, $alias == 'r' ? '' : $alias) . '.' . $this->quoteStrategy->getColumnName($field, $class, $this->platform); + $tableAlias = $alias == 'r' ? '' : $alias; + $columnName = $class->columnNames[$field]; $columnAlias = $this->getSQLColumnAlias($columnName); + $sql = $this->getSQLTableAlias($class->name, $tableAlias) . '.' + . $this->quoteStrategy->getColumnName($field, $class, $this->platform); + $this->rsm->addFieldResult($alias, $columnAlias, $field, $class->name); if (isset($class->fieldMappings[$field]['requireSQLConversion'])) { - $type = Type::getType($class->getTypeOfField($field)); - $sql = $type->convertToPHPValueSQL($sql, $this->platform); + $type = Type::getType($class->getTypeOfField($field)); + $sql = $type->convertToPHPValueSQL($sql, $this->platform); } return $sql . ' AS ' . $columnAlias; From 308b54a8f3904139a2d9ad590093b3c562d999c0 Mon Sep 17 00:00:00 2001 From: "Fabio B. Silva" Date: Tue, 24 Jul 2012 22:33:23 -0300 Subject: [PATCH 04/16] code refactoring on BasicEntityPersister --- .../ORM/Persisters/BasicEntityPersister.php | 753 ++++++++++-------- .../Persisters/JoinedSubclassPersister.php | 4 +- .../ORM/Persisters/SingleTablePersister.php | 24 +- .../BasicEntityPersisterTypeValueSqlTest.php | 2 +- 4 files changed, 424 insertions(+), 359 deletions(-) diff --git a/lib/Doctrine/ORM/Persisters/BasicEntityPersister.php b/lib/Doctrine/ORM/Persisters/BasicEntityPersister.php index 52f0411cc..1fadb8036 100644 --- a/lib/Doctrine/ORM/Persisters/BasicEntityPersister.php +++ b/lib/Doctrine/ORM/Persisters/BasicEntityPersister.php @@ -253,12 +253,12 @@ class BasicEntityPersister return; } - $postInsertIds = array(); - $idGen = $this->class->idGenerator; - $isPostInsertId = $idGen->isPostInsertGenerator(); + $postInsertIds = array(); + $idGenerator = $this->class->idGenerator; + $isPostInsertId = $idGenerator->isPostInsertGenerator(); - $stmt = $this->conn->prepare($this->getInsertSQL()); - $tableName = $this->class->getTableName(); + $stmt = $this->conn->prepare($this->getInsertSQL()); + $tableName = $this->class->getTableName(); foreach ($this->queuedInserts as $entity) { $insertData = $this->prepareInsertData($entity); @@ -274,7 +274,7 @@ class BasicEntityPersister $stmt->execute(); if ($isPostInsertId) { - $id = $idGen->generate($this->em, $entity); + $id = $idGenerator->generate($this->em, $entity); $postInsertIds[$id] = $entity; } else { $id = $this->class->getIdentifierValues($entity); @@ -314,18 +314,21 @@ class BasicEntityPersister */ protected function fetchVersionValue($versionedClass, $id) { - $versionField = $versionedClass->versionField; - $identifier = $this->quoteStrategy->getIdentifierColumnNames($versionedClass, $this->platform); - - $versionFieldColumnName = $this->quoteStrategy->getColumnName($versionField, $versionedClass, $this->platform); + $versionField = $versionedClass->versionField; + $tableName = $this->quoteStrategy->getTableName($versionedClass, $this->platform); + $identifier = $this->quoteStrategy->getIdentifierColumnNames($versionedClass, $this->platform); + $columnName = $this->quoteStrategy->getColumnName($versionField, $versionedClass, $this->platform); //FIXME: Order with composite keys might not be correct - $sql = 'SELECT ' . $versionFieldColumnName - . ' FROM ' . $this->quoteStrategy->getTableName($versionedClass, $this->platform) + $sql = 'SELECT ' . $columnName + . ' FROM ' . $tableName . ' WHERE ' . implode(' = ? AND ', $identifier) . ' = ?'; - $value = $this->conn->fetchColumn($sql, array_values((array)$id)); - return Type::getType($versionedClass->fieldMappings[$versionField]['type'])->convertToPHPValue($value, $this->platform); + $value = $this->conn->fetchColumn($sql, array_values((array)$id)); + $value = Type::getType($versionedClass->fieldMappings[$versionField]['type']) + ->convertToPHPValue($value, $this->platform); + + return $value; } /** @@ -344,19 +347,22 @@ class BasicEntityPersister */ public function update($entity) { - $updateData = $this->prepareUpdateData($entity); $tableName = $this->class->getTableName(); + $updateData = $this->prepareUpdateData($entity); - if (isset($updateData[$tableName]) && $updateData[$tableName]) { - $this->updateTable( - $entity, $this->quoteStrategy->getTableName($this->class, $this->platform), - $updateData[$tableName], $this->class->isVersioned - ); + if ( ! isset($updateData[$tableName]) || ! ($data = $updateData[$tableName])) { + return; + } - if ($this->class->isVersioned) { - $id = $this->em->getUnitOfWork()->getEntityIdentifier($entity); - $this->assignDefaultVersionValue($entity, $id); - } + $isVersioned = $this->class->isVersioned; + $quotedTableName = $this->quoteStrategy->getTableName($this->class, $this->platform); + + $this->updateTable($entity, $quotedTableName, $data, $isVersioned); + + if ($isVersioned) { + $id = $this->em->getUnitOfWork()->getEntityIdentifier($entity); + + $this->assignDefaultVersionValue($entity, $id); } } @@ -371,36 +377,47 @@ class BasicEntityPersister */ protected final function updateTable($entity, $quotedTableName, array $updateData, $versioned = false) { - $set = $params = $types = array(); + $set = array(); + $types = array(); + $params = array(); foreach ($updateData as $columnName => $value) { - $column = $columnName; + $placeholder = '?'; + $column = $columnName; - if (isset($this->class->fieldNames[$columnName])) { - $column = $this->quoteStrategy->getColumnName($this->class->fieldNames[$columnName], $this->class, $this->platform); + switch (true) { + case isset($this->class->fieldNames[$columnName]): + $fieldName = $this->class->fieldNames[$columnName]; + $column = $this->quoteStrategy->getColumnName($fieldName, $this->class, $this->platform); - if (isset($this->class->fieldMappings[$this->class->fieldNames[$columnName]]['requireSQLConversion'])) { - $type = Type::getType($this->columnTypes[$columnName]); - $placeholder = $type->convertToDatabaseValueSQL('?', $this->platform); - } - } else if (isset($this->quotedColumns[$columnName])) { - $column = $this->quotedColumns[$columnName]; + if (isset($this->class->fieldMappings[$fieldName]['requireSQLConversion'])) { + $type = Type::getType($this->columnTypes[$columnName]); + $placeholder = $type->convertToDatabaseValueSQL('?', $this->platform); + } + + break; + + case isset($this->quotedColumns[$columnName]): + $column = $this->quotedColumns[$columnName]; + + break; } - - $set[] = $column . ' = ' . $placeholder; - $params[] = $value; - $types[] = $this->columnTypes[$columnName]; + + $params[] = $value; + $set[] = $column . ' = ' . $placeholder; + $types[] = $this->columnTypes[$columnName]; } - $where = array(); - $id = $this->em->getUnitOfWork()->getEntityIdentifier($entity); + $where = array(); + $identifier = $this->em->getUnitOfWork()->getEntityIdentifier($entity); foreach ($this->class->identifier as $idField) { if (isset($this->class->associationMappings[$idField])) { - $targetMapping = $this->em->getClassMetadata($this->class->associationMappings[$idField]['targetEntity']); - $where[] = $this->class->associationMappings[$idField]['joinColumns'][0]['name']; - $params[] = $id[$idField]; + + $params[] = $identifier[$idField]; + $where[] = $this->class->associationMappings[$idField]['joinColumns'][0]['name']; + $targetMapping = $this->em->getClassMetadata($this->class->associationMappings[$idField]['targetEntity']); switch (true) { case (isset($targetMapping->fieldMappings[$targetMapping->identifier[0]])): @@ -414,27 +431,32 @@ class BasicEntityPersister default: throw ORMException::unrecognizedField($targetMapping->identifier[0]); } - } else { - $where[] = $this->quoteStrategy->getColumnName($idField, $this->class, $this->platform); - $params[] = $id[$idField]; - $types[] = $this->class->fieldMappings[$idField]['type']; + + continue; } + + $params[] = $identifier[$idField]; + $types[] = $this->class->fieldMappings[$idField]['type']; + $where[] = $this->quoteStrategy->getColumnName($idField, $this->class, $this->platform); } if ($versioned) { - $versionField = $this->class->versionField; - $versionFieldType = $this->class->fieldMappings[$versionField]['type']; - $versionColumn = $this->quoteStrategy->getColumnName($versionField, $this->class, $this->platform); + $versionField = $this->class->versionField; + $versionFieldType = $this->class->fieldMappings[$versionField]['type']; + $versionColumn = $this->quoteStrategy->getColumnName($versionField, $this->class, $this->platform); - if ($versionFieldType == Type::INTEGER) { - $set[] = $versionColumn . ' = ' . $versionColumn . ' + 1'; - } else if ($versionFieldType == Type::DATETIME) { - $set[] = $versionColumn . ' = CURRENT_TIMESTAMP'; + $where[] = $versionColumn; + $types[] = $this->class->fieldMappings[$versionField]['type']; + $params[] = $this->class->reflFields[$versionField]->getValue($entity); + + switch ($versionFieldType) { + case Type::INTEGER: + $set[] = $versionColumn . ' = ' . $versionColumn . ' + 1'; + break; + case Type::DATETIME: + $set[] = $versionColumn . ' = CURRENT_TIMESTAMP'; + break; } - - $where[] = $versionColumn; - $params[] = $this->class->reflFields[$versionField]->getValue($entity); - $types[] = $this->class->fieldMappings[$versionField]['type']; } $sql = 'UPDATE ' . $quotedTableName @@ -456,49 +478,53 @@ class BasicEntityPersister protected function deleteJoinTableRecords($identifier) { foreach ($this->class->associationMappings as $mapping) { - if ($mapping['type'] == ClassMetadata::MANY_TO_MANY) { - // @Todo this only covers scenarios with no inheritance or of the same level. Is there something - // like self-referential relationship between different levels of an inheritance hierachy? I hope not! - $selfReferential = ($mapping['targetEntity'] == $mapping['sourceEntity']); - $otherKeys = array(); - $keys = array(); + if ($mapping['type'] !== ClassMetadata::MANY_TO_MANY) { + continue; + } - if ( ! $mapping['isOwningSide']) { - $relatedClass = $this->em->getClassMetadata($mapping['targetEntity']); - $mapping = $relatedClass->associationMappings[$mapping['mappedBy']]; + // @Todo this only covers scenarios with no inheritance or of the same level. Is there something + // like self-referential relationship between different levels of an inheritance hierachy? I hope not! + $selfReferential = ($mapping['targetEntity'] == $mapping['sourceEntity']); + $class = $this->class; + $association = $mapping; + $otherColumns = array(); + $otherKeys = array(); + $keys = array(); - foreach ($mapping['joinTable']['inverseJoinColumns'] as $joinColumn) { - $keys[] = $this->quoteStrategy->getJoinColumnName($joinColumn, $relatedClass, $this->platform); - } + if ( ! $mapping['isOwningSide']) { + $class = $this->em->getClassMetadata($mapping['targetEntity']); + $association = $class->associationMappings[$mapping['mappedBy']]; + } - if ($selfReferential) { - foreach ($mapping['joinTable']['joinColumns'] as $joinColumn) { - $otherKeys[] = $this->quoteStrategy->getJoinColumnName($joinColumn, $relatedClass, $this->platform); - } - } - } else { + $joinColumns = $mapping['isOwningSide'] + ? $association['joinTable']['joinColumns'] + : $association['joinTable']['inverseJoinColumns']; - foreach ($mapping['joinTable']['joinColumns'] as $joinColumn) { - $keys[] = $this->quoteStrategy->getJoinColumnName($joinColumn, $this->class, $this->platform); - } - if ($selfReferential) { - foreach ($mapping['joinTable']['inverseJoinColumns'] as $joinColumn) { - $otherKeys[] = $this->quoteStrategy->getJoinColumnName($joinColumn, $this->class, $this->platform); - } - } - } + if ($selfReferential) { + $otherColumns = ! $mapping['isOwningSide'] + ? $association['joinTable']['joinColumns'] + : $association['joinTable']['inverseJoinColumns']; + } - if ( ! isset($mapping['isOnDeleteCascade'])) { + foreach ($joinColumns as $joinColumn) { + $keys[] = $this->quoteStrategy->getJoinColumnName($joinColumn, $class, $this->platform); + } - $joinTableName = $this->quoteStrategy->getJoinTableName($mapping, $this->class, $this->platform); + foreach ($otherColumns as $joinColumn) { + $otherKeys[] = $this->quoteStrategy->getJoinColumnName($joinColumn, $class, $this->platform); + } - $this->conn->delete($joinTableName, array_combine($keys, $identifier)); + if (isset($mapping['isOnDeleteCascade'])) { + continue; + } - if ($selfReferential) { - $this->conn->delete($joinTableName, array_combine($otherKeys, $identifier)); - } - } + $joinTableName = $this->quoteStrategy->getJoinTableName($association, $this->class, $this->platform); + + $this->conn->delete($joinTableName, array_combine($keys, $identifier)); + + if ($selfReferential) { + $this->conn->delete($joinTableName, array_combine($otherKeys, $identifier)); } } } @@ -516,12 +542,12 @@ class BasicEntityPersister public function delete($entity) { $identifier = $this->em->getUnitOfWork()->getEntityIdentifier($entity); + $tableName = $this->quoteStrategy->getTableName($this->class, $this->platform); + $idColumns = $this->quoteStrategy->getIdentifierColumnNames($this->class, $this->platform); + $id = array_combine($idColumns, $identifier); $this->deleteJoinTableRecords($identifier); - - $id = array_combine($this->quoteStrategy->getIdentifierColumnNames($this->class, $this->platform), $identifier); - - $this->conn->delete($this->quoteStrategy->getTableName($this->class, $this->platform), $id); + $this->conn->delete($tableName, $id); } /** @@ -547,7 +573,7 @@ class BasicEntityPersister protected function prepareUpdateData($entity) { $result = array(); - $uow = $this->em->getUnitOfWork(); + $uow = $this->em->getUnitOfWork(); if (($versioned = $this->class->isVersioned) != false) { $versionField = $this->class->versionField; @@ -606,11 +632,13 @@ class BasicEntityPersister $this->columnTypes[$sourceColumn] = $targetClass->getTypeOfColumn($targetColumn); } - } else { - $columnName = $this->class->columnNames[$field]; - $this->columnTypes[$columnName] = $this->class->fieldMappings[$field]['type']; - $result[$this->getOwningTable($field)][$columnName] = $newVal; + + continue; } + + $columnName = $this->class->columnNames[$field]; + $this->columnTypes[$columnName] = $this->class->fieldMappings[$field]['type']; + $result[$this->getOwningTable($field)][$columnName] = $newVal; } return $result; @@ -662,13 +690,13 @@ class BasicEntityPersister */ public function load(array $criteria, $entity = null, $assoc = null, array $hints = array(), $lockMode = 0, $limit = null, array $orderBy = null) { - $sql = $this->getSelectEntitiesSQL($criteria, $assoc, $lockMode, $limit, null, $orderBy); + $sql = $this->getSelectSQL($criteria, $assoc, $lockMode, $limit, null, $orderBy); list($params, $types) = $this->expandParameters($criteria); $stmt = $this->conn->executeQuery($sql, $params, $types); if ($entity !== null) { - $hints[Query::HINT_REFRESH] = true; - $hints[Query::HINT_REFRESH_ENTITY] = $entity; + $hints[Query::HINT_REFRESH] = true; + $hints[Query::HINT_REFRESH_ENTITY] = $entity; } $hydrator = $this->em->newHydrator($this->selectJoinSql ? Query::HYDRATE_OBJECT : Query::HYDRATE_SIMPLEOBJECT); @@ -758,10 +786,10 @@ class BasicEntityPersister */ public function refresh(array $id, $entity, $lockMode = 0) { - $sql = $this->getSelectEntitiesSQL($id, null, $lockMode); + $sql = $this->getSelectSQL($id, null, $lockMode); list($params, $types) = $this->expandParameters($id); $stmt = $this->conn->executeQuery($sql, $params, $types); - + $hydrator = $this->em->newHydrator(Query::HYDRATE_OBJECT); $hydrator->hydrateAll($stmt, $this->rsm, array(Query::HINT_REFRESH => true)); } @@ -778,14 +806,12 @@ class BasicEntityPersister $orderBy = $criteria->getOrderings(); $limit = $criteria->getMaxResults(); $offset = $criteria->getFirstResult(); - - $sql = $this->getSelectEntitiesSQL($criteria, null, 0, $limit, $offset, $orderBy); + $query = $this->getSelectSQL($criteria, null, 0, $limit, $offset, $orderBy); list($params, $types) = $this->expandCriteriaParameters($criteria); - $stmt = $this->conn->executeQuery($sql, $params, $types); - - $hydrator = $this->em->newHydrator(($this->selectJoinSql) ? Query::HYDRATE_OBJECT : Query::HYDRATE_SIMPLEOBJECT); + $stmt = $this->conn->executeQuery($query, $params, $types); + $hydrator = $this->em->newHydrator(($this->selectJoinSql) ? Query::HYDRATE_OBJECT : Query::HYDRATE_SIMPLEOBJECT); return $hydrator->hydrateAll($stmt, $this->rsm, array('deferEagerLoads' => true)); } @@ -837,7 +863,7 @@ class BasicEntityPersister */ public function loadAll(array $criteria = array(), array $orderBy = null, $limit = null, $offset = null) { - $sql = $this->getSelectEntitiesSQL($criteria, null, 0, $limit, $offset, $orderBy); + $sql = $this->getSelectSQL($criteria, null, 0, $limit, $offset, $orderBy); list($params, $types) = $this->expandParameters($criteria); $stmt = $this->conn->executeQuery($sql, $params, $types); @@ -872,18 +898,16 @@ class BasicEntityPersister */ private function loadArrayFromStatement($assoc, $stmt) { - $hints = array('deferEagerLoads' => true); + $rsm = $this->rsm; + $hints = array('deferEagerLoads' => true); if (isset($assoc['indexBy'])) { $rsm = clone ($this->rsm); // this is necessary because the "default rsm" should be changed. $rsm->addIndexBy('r', $assoc['indexBy']); - } else { - $rsm = $this->rsm; } - $hydrator = $this->em->newHydrator(Query::HYDRATE_OBJECT); - - return $hydrator->hydrateAll($stmt, $rsm, $hints); + return $this->em->newHydrator(Query::HYDRATE_OBJECT) + ->hydrateAll($stmt, $rsm, $hints); } /** @@ -897,18 +921,19 @@ class BasicEntityPersister */ private function loadCollectionFromStatement($assoc, $stmt, $coll) { - $hints = array('deferEagerLoads' => true, 'collection' => $coll); + $rsm = $this->rsm; + $hints = array( + 'deferEagerLoads' => true, + 'collection' => $coll + ); if (isset($assoc['indexBy'])) { $rsm = clone ($this->rsm); // this is necessary because the "default rsm" should be changed. $rsm->addIndexBy('r', $assoc['indexBy']); - } else { - $rsm = $this->rsm; } - $hydrator = $this->em->newHydrator(Query::HYDRATE_OBJECT); - - return $hydrator->hydrateAll($stmt, $rsm, $hints); + return $this->em->newHydrator(Query::HYDRATE_OBJECT) + ->hydrateAll($stmt, $rsm, $hints); } /** @@ -930,66 +955,57 @@ class BasicEntityPersister private function getManyToManyStatement(array $assoc, $sourceEntity, $offset = null, $limit = null) { - $criteria = array(); - $sourceClass = $this->em->getClassMetadata($assoc['sourceEntity']); + $sourceClass = $this->em->getClassMetadata($assoc['sourceEntity']); + $class = $sourceClass; + $association = $assoc; + $criteria = array(); - if ($assoc['isOwningSide']) { - $quotedJoinTable = $this->quoteStrategy->getJoinTableName($assoc, $sourceClass, $this->platform); - foreach ($assoc['joinTable']['joinColumns'] as $joinColumn) { - $relationKeyColumn = $joinColumn['name']; - $sourceKeyColumn = $joinColumn['referencedColumnName']; - $quotedKeyColumn = $this->quoteStrategy->getJoinColumnName($joinColumn, $sourceClass, $this->platform); - - if ($sourceClass->containsForeignIdentifier) { - $field = $sourceClass->getFieldForColumn($sourceKeyColumn); - $value = $sourceClass->reflFields[$field]->getValue($sourceEntity); - - if (isset($sourceClass->associationMappings[$field])) { - $value = $this->em->getUnitOfWork()->getEntityIdentifier($value); - $value = $value[$this->em->getClassMetadata($sourceClass->associationMappings[$field]['targetEntity'])->identifier[0]]; - } - - $criteria[$quotedJoinTable . "." . $quotedKeyColumn] = $value; - } else if (isset($sourceClass->fieldNames[$sourceKeyColumn])) { - $criteria[$quotedJoinTable . "." . $quotedKeyColumn] = $sourceClass->reflFields[$sourceClass->fieldNames[$sourceKeyColumn]]->getValue($sourceEntity); - } else { - throw MappingException::joinColumnMustPointToMappedField( - $sourceClass->name, $sourceKeyColumn - ); - } - } - } else { - $owningAssoc = $this->em->getClassMetadata($assoc['targetEntity'])->associationMappings[$assoc['mappedBy']]; - $quotedJoinTable = $this->quoteStrategy->getJoinTableName($owningAssoc, $sourceClass, $this->platform); - - // TRICKY: since the association is inverted source and target are flipped - foreach ($owningAssoc['joinTable']['inverseJoinColumns'] as $joinColumn) { - $relationKeyColumn = $joinColumn['name']; - $sourceKeyColumn = $joinColumn['referencedColumnName']; - $quotedKeyColumn = $this->quoteStrategy->getJoinColumnName($joinColumn, $sourceClass, $this->platform); - - if ($sourceClass->containsForeignIdentifier) { - $field = $sourceClass->getFieldForColumn($sourceKeyColumn); - $value = $sourceClass->reflFields[$field]->getValue($sourceEntity); - - if (isset($sourceClass->associationMappings[$field])) { - $value = $this->em->getUnitOfWork()->getEntityIdentifier($value); - $value = $value[$this->em->getClassMetadata($sourceClass->associationMappings[$field]['targetEntity'])->identifier[0]]; - } - - $criteria[$quotedJoinTable . "." . $quotedKeyColumn] = $value; - } else if (isset($sourceClass->fieldNames[$sourceKeyColumn])) { - $criteria[$quotedJoinTable . "." . $quotedKeyColumn] = $sourceClass->reflFields[$sourceClass->fieldNames[$sourceKeyColumn]]->getValue($sourceEntity); - } else { - throw MappingException::joinColumnMustPointToMappedField( - $sourceClass->name, $sourceKeyColumn - ); - } - } + if ( ! $assoc['isOwningSide']) { + $class = $this->em->getClassMetadata($assoc['targetEntity']); + $association = $class->associationMappings[$assoc['mappedBy']]; } - $sql = $this->getSelectEntitiesSQL($criteria, $assoc, 0, $limit, $offset); + $joinColumns = $assoc['isOwningSide'] + ? $association['joinTable']['joinColumns'] + : $association['joinTable']['inverseJoinColumns']; + + $quotedJoinTable = $this->quoteStrategy->getJoinTableName($association, $class, $this->platform); + + foreach ($joinColumns as $joinColumn) { + + $relationKeyColumn = $joinColumn['name']; + $sourceKeyColumn = $joinColumn['referencedColumnName']; + $quotedKeyColumn = $this->quoteStrategy->getJoinColumnName($joinColumn, $class, $this->platform); + + switch (true) { + case $sourceClass->containsForeignIdentifier: + $field = $sourceClass->getFieldForColumn($sourceKeyColumn); + $value = $sourceClass->reflFields[$field]->getValue($sourceEntity); + + if (isset($sourceClass->associationMappings[$field])) { + $value = $this->em->getUnitOfWork()->getEntityIdentifier($value); + $value = $value[$this->em->getClassMetadata($sourceClass->associationMappings[$field]['targetEntity'])->identifier[0]]; + } + + break; + + case isset($sourceClass->fieldNames[$sourceKeyColumn]): + $field = $sourceClass->fieldNames[$sourceKeyColumn]; + $value = $sourceClass->reflFields[$field]->getValue($sourceEntity); + + break; + + default: + throw MappingException::joinColumnMustPointToMappedField( + $sourceClass->name, $sourceKeyColumn + ); + } + + $criteria[$quotedJoinTable . '.' . $quotedKeyColumn] = $value; + } + + $sql = $this->getSelectSQL($criteria, $assoc, 0, $limit, $offset); list($params, $types) = $this->expandParameters($criteria); return $this->conn->executeQuery($sql, $params, $types); @@ -1006,41 +1022,60 @@ class BasicEntityPersister * @param int $offset * @param array $orderBy * @return string - * @todo Refactor: getSelectSQL(...) */ - protected function getSelectEntitiesSQL($criteria, $assoc = null, $lockMode = 0, $limit = null, $offset = null, array $orderBy = null) + protected function getSelectSQL($criteria, $assoc = null, $lockMode = 0, $limit = null, $offset = null, array $orderBy = null) { - $joinSql = ($assoc != null && $assoc['type'] == ClassMetadata::MANY_TO_MANY) ? $this->getSelectManyToManyJoinSQL($assoc) : ''; + $lockSql = ''; + $joinSql = ''; + $orderBySql = ''; + + if ($assoc != null && $assoc['type'] == ClassMetadata::MANY_TO_MANY) { + $joinSql = $this->getSelectManyToManyJoinSQL($assoc); + } + + if (isset($assoc['orderBy'])) { + $orderBy = $assoc['orderBy']; + } + + if ($orderBy) { + $orderBySql = $this->getOrderBySQL($orderBy, $this->getSQLTableAlias($this->class->name)); + } + $conditionSql = ($criteria instanceof Criteria) ? $this->getSelectConditionCriteriaSQL($criteria) : $this->getSelectConditionSQL($criteria, $assoc); - $orderBy = ($assoc !== null && isset($assoc['orderBy'])) ? $assoc['orderBy'] : $orderBy; - $orderBySql = $orderBy ? $this->getOrderBySQL($orderBy, $this->getSQLTableAlias($this->class->name)) : ''; + switch ($lockMode) { + case LockMode::PESSIMISTIC_READ: + $lockSql = ' ' . $this->platform->getReadLockSql(); - $lockSql = ''; + break; + case LockMode::PESSIMISTIC_WRITE: + $lockSql = ' ' . $this->platform->getWriteLockSql(); - if ($lockMode == LockMode::PESSIMISTIC_READ) { - $lockSql = ' ' . $this->platform->getReadLockSql(); - } else if ($lockMode == LockMode::PESSIMISTIC_WRITE) { - $lockSql = ' ' . $this->platform->getWriteLockSql(); + break; } - $alias = $this->getSQLTableAlias($this->class->name); + $columnList = $this->getSelectColumnsSQL(); + $tableAlias = $this->getSQLTableAlias($this->class->name); + $filterSql = $this->generateFilterConditionSQL($this->class, $tableAlias); + $tableName = $this->quoteStrategy->getTableName($this->class, $this->platform); - if ($filterSql = $this->generateFilterConditionSQL($this->class, $alias)) { - if ($conditionSql) { - $conditionSql .= ' AND '; - } - - $conditionSql .= $filterSql; + if ('' !== $filterSql) { + $conditionSql = $conditionSql + ? $conditionSql . ' AND ' . $filterSql + : $filterSql; } - return $this->platform->modifyLimitQuery('SELECT ' . $this->getSelectColumnListSQL() - . $this->platform->appendLockHint(' FROM ' . $this->quoteStrategy->getTableName($this->class, $this->platform) . ' ' - . $alias, $lockMode) - . $this->selectJoinSql . $joinSql - . ($conditionSql ? ' WHERE ' . $conditionSql : '') + $select = 'SELECT ' . $columnList; + $from = ' FROM ' . $tableName . ' '. $tableAlias; + $join = $this->selectJoinSql . $joinSql; + $where = ($conditionSql ? ' WHERE ' . $conditionSql : ''); + + return $this->platform->modifyLimitQuery($select + . $this->platform->appendLockHint($from, $lockMode) + . $join + . $where . $orderBySql, $limit, $offset) . $lockSql; } @@ -1089,106 +1124,105 @@ class BasicEntityPersister * Subclasses may or may not do the same. * * @return string The SQL fragment. - * @todo Rename: getSelectColumnsSQL() */ - protected function getSelectColumnListSQL() + protected function getSelectColumnsSQL() { if ($this->selectColumnListSql !== null) { return $this->selectColumnListSql; } - $columnList = ''; - $this->rsm = new Query\ResultSetMapping(); + $columnList = array(); + $this->rsm = new Query\ResultSetMapping(); $this->rsm->addEntityResult($this->class->name, 'r'); // r for root // Add regular columns to select list foreach ($this->class->fieldNames as $field) { - if ($columnList) $columnList .= ', '; - - $columnList .= $this->getSelectColumnSQL($field, $this->class); + $columnList[] = $this->getSelectColumnSQL($field, $this->class); } - $this->selectJoinSql = ''; - $eagerAliasCounter = 0; + $this->selectJoinSql = ''; + $eagerAliasCounter = 0; foreach ($this->class->associationMappings as $assocField => $assoc) { $assocColumnSQL = $this->getSelectColumnAssociationSQL($assocField, $assoc, $this->class); if ($assocColumnSQL) { - if ($columnList) $columnList .= ', '; - - $columnList .= $assocColumnSQL; + $columnList[] = $assocColumnSQL; } - if ($assoc['type'] & ClassMetadata::TO_ONE && ($assoc['fetch'] == ClassMetadata::FETCH_EAGER || !$assoc['isOwningSide'])) { + if ( ! (($assoc['type'] & ClassMetadata::TO_ONE) && ($assoc['fetch'] == ClassMetadata::FETCH_EAGER || !$assoc['isOwningSide']))) { + continue; + } + + $eagerEntity = $this->em->getClassMetadata($assoc['targetEntity']); + + if ($eagerEntity->inheritanceType != ClassMetadata::INHERITANCE_TYPE_NONE) { + continue; // now this is why you shouldn't use inheritance + } + + $assocAlias = 'e' . ($eagerAliasCounter++); + $this->rsm->addJoinedEntityResult($assoc['targetEntity'], $assocAlias, 'r', $assocField); + + foreach ($eagerEntity->fieldNames as $field) { + $columnList[] = $this->getSelectColumnSQL($field, $eagerEntity, $assocAlias); + } + + foreach ($eagerEntity->associationMappings as $eagerAssocField => $eagerAssoc) { + $eagerAssocColumnSQL = $this->getSelectColumnAssociationSQL( + $eagerAssocField, $eagerAssoc, $eagerEntity, $assocAlias + ); + + if ($eagerAssocColumnSQL) { + $columnList[] = $eagerAssocColumnSQL; + } + } + + $association = $assoc; + $joinCondition = array(); + + if ( ! $assoc['isOwningSide']) { $eagerEntity = $this->em->getClassMetadata($assoc['targetEntity']); + $association = $eagerEntity->getAssociationMapping($assoc['mappedBy']); + } - if ($eagerEntity->inheritanceType != ClassMetadata::INHERITANCE_TYPE_NONE) { - continue; // now this is why you shouldn't use inheritance + $joinTableAlias = $this->getSQLTableAlias($eagerEntity->name, $assocAlias); + $joinTableName = $this->quoteStrategy->getTableName($eagerEntity, $this->platform); + + if ($assoc['isOwningSide']) { + + $tableAlias = $this->getSQLTableAlias($association['targetEntity'], $assocAlias); + $this->selectJoinSql .= ' ' . $this->getJoinSQLForJoinColumns($association['joinColumns']); + + foreach ($association['joinColumns'] as $joinColumn) { + $sourceCol = $this->quoteStrategy->getJoinColumnName($joinColumn, $this->class, $this->platform); + $targetCol = $this->quoteStrategy->getReferencedJoinColumnName($joinColumn, $this->class, $this->platform); + $joinCondition[] = $this->getSQLTableAlias($association['sourceEntity']) + . '.' . $sourceCol . ' = ' . $tableAlias . '.' . $targetCol; } - $assocAlias = 'e' . ($eagerAliasCounter++); - $this->rsm->addJoinedEntityResult($assoc['targetEntity'], $assocAlias, 'r', $assocField); - - foreach ($eagerEntity->fieldNames as $field) { - if ($columnList) $columnList .= ', '; - - $columnList .= $this->getSelectColumnSQL($field, $eagerEntity, $assocAlias); + // Add filter SQL + if ($filterSql = $this->generateFilterConditionSQL($eagerEntity, $tableAlias)) { + $joinCondition[] = $filterSql; } - foreach ($eagerEntity->associationMappings as $assoc2Field => $assoc2) { - $assoc2ColumnSQL = $this->getSelectColumnAssociationSQL($assoc2Field, $assoc2, $eagerEntity, $assocAlias); + } else { - if ($assoc2ColumnSQL) { - if ($columnList) $columnList .= ', '; - $columnList .= $assoc2ColumnSQL; - } - } - $first = true; + $this->selectJoinSql .= ' LEFT JOIN'; - if ($assoc['isOwningSide']) { - $this->selectJoinSql .= ' ' . $this->getJoinSQLForJoinColumns($assoc['joinColumns']); - $this->selectJoinSql .= ' ' . $this->quoteStrategy->getTableName($eagerEntity, $this->platform) . ' ' . $this->getSQLTableAlias($eagerEntity->name, $assocAlias) .' ON '; + foreach ($association['joinColumns'] as $joinColumn) { + $sourceCol = $this->quoteStrategy->getJoinColumnName($joinColumn, $this->class, $this->platform); + $targetCol = $this->quoteStrategy->getReferencedJoinColumnName($joinColumn, $this->class, $this->platform); - $tableAlias = $this->getSQLTableAlias($assoc['targetEntity'], $assocAlias); - foreach ($assoc['joinColumns'] as $joinColumn) { - $sourceCol = $this->quoteStrategy->getJoinColumnName($joinColumn, $this->class, $this->platform); - $targetCol = $this->quoteStrategy->getReferencedJoinColumnName($joinColumn, $this->class, $this->platform); - - if ( ! $first) { - $this->selectJoinSql .= ' AND '; - } - $this->selectJoinSql .= $this->getSQLTableAlias($assoc['sourceEntity']) . '.' . $sourceCol . ' = ' - . $tableAlias . '.' . $targetCol; - $first = false; - } - - // Add filter SQL - if ($filterSql = $this->generateFilterConditionSQL($eagerEntity, $tableAlias)) { - $this->selectJoinSql .= ' AND ' . $filterSql; - } - } else { - $eagerEntity = $this->em->getClassMetadata($assoc['targetEntity']); - $owningAssoc = $eagerEntity->getAssociationMapping($assoc['mappedBy']); - - $this->selectJoinSql .= ' LEFT JOIN'; - $this->selectJoinSql .= ' ' . $this->quoteStrategy->getTableName($eagerEntity, $this->platform) . ' ' - . $this->getSQLTableAlias($eagerEntity->name, $assocAlias) . ' ON '; - - foreach ($owningAssoc['sourceToTargetKeyColumns'] as $sourceCol => $targetCol) { - if ( ! $first) { - $this->selectJoinSql .= ' AND '; - } - - $this->selectJoinSql .= $this->getSQLTableAlias($owningAssoc['sourceEntity'], $assocAlias) . '.' . $sourceCol . ' = ' - . $this->getSQLTableAlias($owningAssoc['targetEntity']) . '.' . $targetCol; - $first = false; - } + $joinCondition[] = $this->getSQLTableAlias($association['sourceEntity'], $assocAlias) . '.' . $sourceCol . ' = ' + . $this->getSQLTableAlias($association['targetEntity']) . '.' . $targetCol; } } + + $this->selectJoinSql .= ' ' . $joinTableName . ' ' . $joinTableAlias . ' ON '; + $this->selectJoinSql .= implode(' AND ', $joinCondition); } - $this->selectColumnListSql = $columnList; + $this->selectColumnListSql = implode(', ', $columnList); return $this->selectColumnListSql; } @@ -1205,19 +1239,20 @@ class BasicEntityPersister */ protected function getSelectColumnAssociationSQL($field, $assoc, ClassMetadata $class, $alias = 'r') { + if ( ! ($assoc['isOwningSide'] && $assoc['type'] & ClassMetadata::TO_ONE) ) { + return ''; + } + $columnList = array(); - if ($assoc['isOwningSide'] && $assoc['type'] & ClassMetadata::TO_ONE) { + foreach ($assoc['joinColumns'] as $joinColumn) { - foreach ($assoc['joinColumns'] as $joinColumn) { + $quotedColumn = $this->quoteStrategy->getJoinColumnName($joinColumn, $this->class, $this->platform); + $resultColumnName = $this->getSQLColumnAlias($joinColumn['name']); + $columnList[] = $this->getSQLTableAlias($class->name, ($alias == 'r' ? '' : $alias) ) + . '.' . $quotedColumn . ' AS ' . $resultColumnName; - $quotedColumn = $this->quoteStrategy->getJoinColumnName($joinColumn, $this->class, $this->platform); - $resultColumnName = $this->getSQLColumnAlias($joinColumn['name']); - $columnList[] = $this->getSQLTableAlias($class->name, ($alias == 'r' ? '' : $alias) ) - . '.' . $quotedColumn . ' AS ' . $resultColumnName; - - $this->rsm->addMetaResult($alias, $resultColumnName, $quotedColumn, isset($assoc['id']) && $assoc['id'] === true); - } + $this->rsm->addMetaResult($alias, $resultColumnName, $quotedColumn, isset($assoc['id']) && $assoc['id'] === true); } return implode(', ', $columnList); @@ -1262,39 +1297,42 @@ class BasicEntityPersister */ protected function getInsertSQL() { - if ($this->insertSql === null) { - $insertSql = ''; - $columns = $this->getInsertColumnList(); + if ($this->insertSql !== null) { + return $this->insertSql; + } - if (empty($columns)) { - $insertSql = $this->platform->getEmptyIdentityInsertSQL( - $this->quoteStrategy->getTableName($this->class, $this->platform), - $this->quoteStrategy->getColumnName($this->class->identifier[0], $this->class, $this->platform) - ); - } else { - $columns = array_unique($columns); + $insertSql = ''; + $columns = $this->getInsertColumnList(); - $values = array(); - foreach ($columns as $column) { - $placeholder = '?'; + if (empty($columns)) { + $this->insertSql = $this->platform->getEmptyIdentityInsertSQL( + $this->quoteStrategy->getTableName($this->class, $this->platform), + $this->quoteStrategy->getColumnName($this->class->identifier[0], $this->class, $this->platform) + ); - if (isset($this->class->fieldNames[$column]) && - isset($this->columnTypes[$this->class->fieldNames[$column]]) && - isset($this->class->fieldMappings[$this->class->fieldNames[$column]]['requireSQLConversion'])) { - $type = Type::getType($this->columnTypes[$this->class->fieldNames[$column]]); - $placeholder = $type->convertToDatabaseValueSQL('?', $this->platform); - } + return $this->insertSql; + } - $values[] = $placeholder; - } + $values = array(); + $columns = array_unique($columns); - $insertSql = 'INSERT INTO ' . $this->quoteStrategy->getTableName($this->class, $this->platform) - . ' (' . implode(', ', $columns) . ') VALUES (' . implode(', ', $values) . ')'; + foreach ($columns as $column) { + $placeholder = '?'; + + if (isset($this->class->fieldNames[$column]) + && isset($this->columnTypes[$this->class->fieldNames[$column]]) + && isset($this->class->fieldMappings[$this->class->fieldNames[$column]]['requireSQLConversion'])) { + + $type = Type::getType($this->columnTypes[$this->class->fieldNames[$column]]); + $placeholder = $type->convertToDatabaseValueSQL('?', $this->platform); } - $this->insertSql = $insertSql; + $values[] = $placeholder; } + $this->insertSql = 'INSERT INTO ' . $this->quoteStrategy->getTableName($this->class, $this->platform) + . ' (' . implode(', ', $columns) . ') VALUES (' . implode(', ', $values) . ')'; + return $this->insertSql; } @@ -1322,7 +1360,11 @@ class BasicEntityPersister $columns[] = $this->quoteStrategy->getJoinColumnName($joinColumn, $this->class, $this->platform); } } - } else if ($this->class->generatorType != ClassMetadata::GENERATOR_TYPE_IDENTITY || $this->class->identifier[0] != $name) { + + continue; + } + + if ($this->class->generatorType != ClassMetadata::GENERATOR_TYPE_IDENTITY || $this->class->identifier[0] != $name) { $columns[] = $this->quoteStrategy->getColumnName($name, $this->class, $this->platform); $this->columnTypes[$name] = $this->class->fieldMappings[$name]['type']; } @@ -1341,15 +1383,17 @@ class BasicEntityPersister */ protected function getSelectColumnSQL($field, ClassMetadata $class, $alias = 'r') { - $sql = $this->getSQLTableAlias($class->name, $alias == 'r' ? '' : $alias) - . '.' . $this->quoteStrategy->getColumnName($field, $class, $this->platform); - $columnAlias = $this->getSQLColumnAlias($class->columnNames[$field]); - + $root = $alias == 'r' ? '' : $alias ; + $tableAlias = $this->getSQLTableAlias($class->name, $root); + $columnName = $this->quoteStrategy->getColumnName($field, $class, $this->platform); + $sql = $tableAlias . '.' . $columnName; + $columnAlias = $this->getSQLColumnAlias($class->columnNames[$field]); + $this->rsm->addFieldResult($alias, $columnAlias, $field); if (isset($class->fieldMappings[$field]['requireSQLConversion'])) { - $type = Type::getType($class->getTypeOfField($field)); - $sql = $type->convertToPHPValueSQL($sql, $this->platform); + $type = Type::getType($class->getTypeOfField($field)); + $sql = $type->convertToPHPValueSQL($sql, $this->platform); } return $sql . ' AS ' . $columnAlias; @@ -1388,17 +1432,26 @@ class BasicEntityPersister */ public function lock(array $criteria, $lockMode) { + $lockSql = ''; $conditionSql = $this->getSelectConditionSQL($criteria); - if ($lockMode == LockMode::PESSIMISTIC_READ) { - $lockSql = $this->platform->getReadLockSql(); - } else if ($lockMode == LockMode::PESSIMISTIC_WRITE) { - $lockSql = $this->platform->getWriteLockSql(); + switch ($lockMode) { + case LockMode::PESSIMISTIC_READ: + $lockSql = $this->platform->getReadLockSql(); + + break; + case LockMode::PESSIMISTIC_WRITE: + + $lockSql = $this->platform->getWriteLockSql(); + break; } + $lock = $this->platform->appendLockHint($this->getLockTablesSql(), $lockMode); + $where = ($conditionSql ? ' WHERE ' . $conditionSql : '') . ' '; $sql = 'SELECT 1 ' - . $this->platform->appendLockHint($this->getLockTablesSql(), $lockMode) - . ($conditionSql ? ' WHERE ' . $conditionSql : '') . ' ' . $lockSql; + . $lock + . $where + . $lockSql; list($params, $types) = $this->expandParameters($criteria); @@ -1412,7 +1465,8 @@ class BasicEntityPersister */ protected function getLockTablesSql() { - return 'FROM ' . $this->quoteStrategy->getTableName($this->class, $this->platform) . ' ' + return 'FROM ' + . $this->quoteStrategy->getTableName($this->class, $this->platform) . ' ' . $this->getSQLTableAlias($this->class->name); } @@ -1516,14 +1570,13 @@ class BasicEntityPersister */ protected function getSelectConditionSQL(array $criteria, $assoc = null) { - $conditionSql = ''; + $conditions = array(); foreach ($criteria as $field => $value) { - $conditionSql .= $conditionSql ? ' AND ' : ''; - $conditionSql .= $this->getSelectConditionStatementSQL($field, $value, $assoc); + $conditions[] = $this->getSelectConditionStatementSQL($field, $value, $assoc); } - return $conditionSql; + return implode(' AND ', $conditions); } /** @@ -1586,12 +1639,14 @@ class BasicEntityPersister } $criteria[$tableAlias . "." . $targetKeyColumn] = $value; - } else { - $criteria[$tableAlias . "." . $targetKeyColumn] = $sourceClass->reflFields[$sourceClass->fieldNames[$sourceKeyColumn]]->getValue($sourceEntity); + + continue; } + + $criteria[$tableAlias . "." . $targetKeyColumn] = $sourceClass->reflFields[$sourceClass->fieldNames[$sourceKeyColumn]]->getValue($sourceEntity); } - $sql = $this->getSelectEntitiesSQL($criteria, $assoc, 0, $limit, $offset); + $sql = $this->getSelectSQL($criteria, $assoc, 0, $limit, $offset); list($params, $types) = $this->expandParameters($criteria); return $this->conn->executeQuery($sql, $params, $types); @@ -1605,7 +1660,8 @@ class BasicEntityPersister */ private function expandParameters($criteria) { - $params = $types = array(); + $params = array(); + $types = array(); foreach ($criteria as $field => $value) { if ($value === null) { @@ -1653,8 +1709,9 @@ class BasicEntityPersister default: $type = null; } + if (is_array($value)) { - $type = Type::getType( $type )->getBindingType(); + $type = Type::getType($type)->getBindingType(); $type += Connection::ARRAY_PARAM_OFFSET; } @@ -1669,17 +1726,17 @@ class BasicEntityPersister */ private function getValue($value) { - if (is_array($value)) { - $newValue = array(); - - foreach ($value as $itemValue) { - $newValue[] = $this->getIndividualValue($itemValue); - } - - return $newValue; + if ( ! is_array($value)) { + return $this->getIndividualValue($value); } - return $this->getIndividualValue($value); + $newValue = array(); + + foreach ($value as $itemValue) { + $newValue[] = $this->getIndividualValue($itemValue); + } + + return $newValue; } /** @@ -1690,22 +1747,20 @@ class BasicEntityPersister */ private function getIndividualValue($value) { - if (is_object($value) && $this->em->getMetadataFactory()->hasMetadataFor(ClassUtils::getClass($value))) { - if ($this->em->getUnitOfWork()->getEntityState($value) === UnitOfWork::STATE_MANAGED) { - $idValues = $this->em->getUnitOfWork()->getEntityIdentifier($value); - } else { - $class = $this->em->getClassMetadata(get_class($value)); - $idValues = $class->getIdentifierValues($value); - } - - $key = key($idValues); - - if (null !== $key){ - $value = $idValues[$key]; - } + if ( ! is_object($value) || ! $this->em->getMetadataFactory()->hasMetadataFor(ClassUtils::getClass($value))) { + return $value; } - return $value; + if ($this->em->getUnitOfWork()->getEntityState($value) === UnitOfWork::STATE_MANAGED) { + $idValues = $this->em->getUnitOfWork()->getEntityIdentifier($value); + + return reset($idValues); + } + + $class = $this->em->getClassMetadata(get_class($value)); + $idValues = $class->getIdentifierValues($value); + + return reset($idValues); } /** diff --git a/lib/Doctrine/ORM/Persisters/JoinedSubclassPersister.php b/lib/Doctrine/ORM/Persisters/JoinedSubclassPersister.php index 0843cbb0d..1c82eedff 100644 --- a/lib/Doctrine/ORM/Persisters/JoinedSubclassPersister.php +++ b/lib/Doctrine/ORM/Persisters/JoinedSubclassPersister.php @@ -266,7 +266,7 @@ class JoinedSubclassPersister extends AbstractEntityInheritancePersister /** * {@inheritdoc} */ - protected function getSelectEntitiesSQL($criteria, $assoc = null, $lockMode = 0, $limit = null, $offset = null, array $orderBy = null) + protected function getSelectSQL($criteria, $assoc = null, $lockMode = 0, $limit = null, $offset = null, array $orderBy = null) { $idColumns = $this->class->getIdentifierColumnNames(); $baseTableAlias = $this->getSQLTableAlias($this->class->name); @@ -442,7 +442,7 @@ class JoinedSubclassPersister extends AbstractEntityInheritancePersister /* * Ensure this method is never called. This persister overrides getSelectEntitiesSQL directly. */ - protected function getSelectColumnListSQL() + protected function getSelectColumnsSQL() { throw new \BadMethodCallException("Illegal invocation of ".__METHOD__."."); } diff --git a/lib/Doctrine/ORM/Persisters/SingleTablePersister.php b/lib/Doctrine/ORM/Persisters/SingleTablePersister.php index 61cee2432..487dd1c05 100644 --- a/lib/Doctrine/ORM/Persisters/SingleTablePersister.php +++ b/lib/Doctrine/ORM/Persisters/SingleTablePersister.php @@ -41,13 +41,13 @@ class SingleTablePersister extends AbstractEntityInheritancePersister } /** {@inheritdoc} */ - protected function getSelectColumnListSQL() + protected function getSelectColumnsSQL() { if ($this->selectColumnListSql !== null) { return $this->selectColumnListSql; } - $columnList = parent::getSelectColumnListSQL(); + $columnList = parent::getSelectColumnsSQL(); $rootClass = $this->em->getClassMetadata($this->class->rootEntityName); $tableAlias = $this->getSQLTableAlias($rootClass->name); @@ -92,7 +92,9 @@ class SingleTablePersister extends AbstractEntityInheritancePersister return $this->selectColumnListSql; } - /** {@inheritdoc} */ + /** + * {@inheritdoc} + */ protected function getInsertColumnList() { $columns = parent::getInsertColumnList(); @@ -103,13 +105,17 @@ class SingleTablePersister extends AbstractEntityInheritancePersister return $columns; } - /** {@inheritdoc} */ + /** + * {@inheritdoc} + */ protected function getSQLTableAlias($className, $assocName = '') { return parent::getSQLTableAlias($this->class->rootEntityName, $assocName); } - /** {@inheritdoc} */ + /** + * {@inheritdoc} + */ protected function getSelectConditionSQL(array $criteria, $assoc = null) { $conditionSql = parent::getSelectConditionSQL($criteria, $assoc); @@ -121,7 +127,9 @@ class SingleTablePersister extends AbstractEntityInheritancePersister return $conditionSql . $this->getSelectConditionDiscriminatorValueSQL(); } - /** {@inheritdoc} */ + /** + * {@inheritdoc} + */ protected function getSelectConditionCriteriaSQL(Criteria $criteria) { $conditionSql = parent::getSelectConditionCriteriaSQL($criteria); @@ -151,7 +159,9 @@ class SingleTablePersister extends AbstractEntityInheritancePersister . ' IN (' . implode(', ', $values) . ')'; } - /** {@inheritdoc} */ + /** + * {@inheritdoc} + */ protected function generateFilterConditionSQL(ClassMetadata $targetEntity, $targetTableAlias) { // Ensure that the filters are applied to the root entity of the inheritance tree diff --git a/tests/Doctrine/Tests/ORM/Persisters/BasicEntityPersisterTypeValueSqlTest.php b/tests/Doctrine/Tests/ORM/Persisters/BasicEntityPersisterTypeValueSqlTest.php index 6ba86f228..d74335171 100644 --- a/tests/Doctrine/Tests/ORM/Persisters/BasicEntityPersisterTypeValueSqlTest.php +++ b/tests/Doctrine/Tests/ORM/Persisters/BasicEntityPersisterTypeValueSqlTest.php @@ -83,7 +83,7 @@ class BasicEntityPersisterTypeValueSqlTest extends \Doctrine\Tests\OrmTestCase public function testStripNonAlphanumericCharactersFromSelectColumnListSQL() { $persister = new BasicEntityPersister($this->_em, $this->_em->getClassMetadata('Doctrine\Tests\Models\Quote\SimpleEntity')); - $method = new \ReflectionMethod($persister, 'getSelectColumnListSQL'); + $method = new \ReflectionMethod($persister, 'getSelectColumnsSQL'); $method->setAccessible(true); $this->assertEquals('t0."simple-entity-id" AS simpleentityid1, t0."simple-entity-value" AS simpleentityvalue2', $method->invoke($persister)); From 3156c1549dd4aaa3fbff244ff40a6b7aa7a3a289 Mon Sep 17 00:00:00 2001 From: "Fabio B. Silva" Date: Wed, 25 Jul 2012 22:18:25 -0300 Subject: [PATCH 05/16] code refactoring on JoinedSubclassPersister --- .../Persisters/JoinedSubclassPersister.php | 391 ++++++++++-------- 1 file changed, 224 insertions(+), 167 deletions(-) diff --git a/lib/Doctrine/ORM/Persisters/JoinedSubclassPersister.php b/lib/Doctrine/ORM/Persisters/JoinedSubclassPersister.php index 1c82eedff..5f2bd11c6 100644 --- a/lib/Doctrine/ORM/Persisters/JoinedSubclassPersister.php +++ b/lib/Doctrine/ORM/Persisters/JoinedSubclassPersister.php @@ -96,18 +96,25 @@ class JoinedSubclassPersister extends AbstractEntityInheritancePersister return $this->owningTableMap[$fieldName]; } - if (isset($this->class->associationMappings[$fieldName]['inherited'])) { - $cm = $this->em->getClassMetadata($this->class->associationMappings[$fieldName]['inherited']); - } else if (isset($this->class->fieldMappings[$fieldName]['inherited'])) { - $cm = $this->em->getClassMetadata($this->class->fieldMappings[$fieldName]['inherited']); - } else { - $cm = $this->class; + switch (true) { + case isset($this->class->associationMappings[$fieldName]['inherited']): + $cm = $this->em->getClassMetadata($this->class->associationMappings[$fieldName]['inherited']); + break; + + case isset($this->class->fieldMappings[$fieldName]['inherited']): + $cm = $this->em->getClassMetadata($this->class->fieldMappings[$fieldName]['inherited']); + break; + + default: + $cm = $this->class; + break; } - $tableName = $cm->getTableName(); + $tableName = $cm->getTableName(); + $quotedTableName = $this->quoteStrategy->getTableName($cm, $this->platform); $this->owningTableMap[$fieldName] = $tableName; - $this->quotedTableMap[$tableName] = $this->quoteStrategy->getTableName($cm, $this->platform); + $this->quotedTableMap[$tableName] = $quotedTableName; return $tableName; } @@ -121,12 +128,14 @@ class JoinedSubclassPersister extends AbstractEntityInheritancePersister return; } - $postInsertIds = array(); - $idGen = $this->class->idGenerator; - $isPostInsertId = $idGen->isPostInsertGenerator(); + $postInsertIds = array(); + $idGenerator = $this->class->idGenerator; + $isPostInsertId = $idGenerator->isPostInsertGenerator(); + $rootClass = ($this->class->name !== $this->class->rootEntityName) + ? $this->em->getClassMetadata($this->class->rootEntityName) + : $this->class; // Prepare statement for the root table - $rootClass = ($this->class->name !== $this->class->rootEntityName) ? $this->em->getClassMetadata($this->class->rootEntityName) : $this->class; $rootPersister = $this->em->getUnitOfWork()->getEntityPersister($rootClass->name); $rootTableName = $rootClass->getTableName(); $rootTableStmt = $this->conn->prepare($rootPersister->getInsertSQL()); @@ -164,7 +173,7 @@ class JoinedSubclassPersister extends AbstractEntityInheritancePersister $rootTableStmt->execute(); if ($isPostInsertId) { - $id = $idGen->generate($this->em, $entity); + $id = $idGenerator->generate($this->em, $entity); $postInsertIds[$id] = $entity; } else { $id = $this->em->getUnitOfWork()->getEntityIdentifier($entity); @@ -173,8 +182,10 @@ class JoinedSubclassPersister extends AbstractEntityInheritancePersister // Execute inserts on subtables. // The order doesn't matter because all child tables link to the root table via FK. foreach ($subTableStmts as $tableName => $stmt) { - $data = isset($insertData[$tableName]) ? $insertData[$tableName] : array(); $paramIndex = 1; + $data = isset($insertData[$tableName]) + ? $insertData[$tableName] + : array(); foreach ((array) $id as $idName => $idVal) { $type = isset($this->columnTypes[$idName]) ? $this->columnTypes[$idName] : Type::STRING; @@ -212,26 +223,32 @@ class JoinedSubclassPersister extends AbstractEntityInheritancePersister { $updateData = $this->prepareUpdateData($entity); - if (($isVersioned = $this->class->isVersioned) != false) { - $versionedClass = $this->getVersionedClassMetadata(); - $versionedTable = $versionedClass->getTableName(); + if ( ! $updateData) { + return; } - if ($updateData) { - foreach ($updateData as $tableName => $data) { - $this->updateTable( - $entity, $this->quotedTableMap[$tableName], $data, $isVersioned && $versionedTable == $tableName - ); - } + if (($isVersioned = $this->class->isVersioned) === false) { + return; + } - // Make sure the table with the version column is updated even if no columns on that - // table were affected. - if ($isVersioned && ! isset($updateData[$versionedTable])) { - $this->updateTable($entity, $this->quoteStrategy->getTableName($versionedClass, $this->platform), array(), true); + $versionedClass = $this->getVersionedClassMetadata(); + $versionedTable = $versionedClass->getTableName(); - $id = $this->em->getUnitOfWork()->getEntityIdentifier($entity); - $this->assignDefaultVersionValue($entity, $id); - } + foreach ($updateData as $tableName => $data) { + $tableName = $this->quotedTableMap[$tableName]; + $versioned = $isVersioned && $versionedTable === $tableName; + + $this->updateTable($entity, $tableName, $data, $versioned); + } + + // Make sure the table with the version column is updated even if no columns on that + // table were affected. + if ($isVersioned && ! isset($updateData[$versionedTable])) { + $tableName = $this->quoteStrategy->getTableName($versionedClass, $this->platform); + $identifiers = $this->em->getUnitOfWork()->getEntityIdentifier($entity); + + $this->updateTable($entity, $tableName, array(), true); + $this->assignDefaultVersionValue($entity, $identifiers); } } @@ -241,25 +258,32 @@ class JoinedSubclassPersister extends AbstractEntityInheritancePersister public function delete($entity) { $identifier = $this->em->getUnitOfWork()->getEntityIdentifier($entity); - $this->deleteJoinTableRecords($identifier); + $id = array_combine($this->class->getIdentifierColumnNames(), $identifier); - $id = array_combine($this->class->getIdentifierColumnNames(), $identifier); + $this->deleteJoinTableRecords($identifier); // If the database platform supports FKs, just // delete the row from the root table. Cascades do the rest. if ($this->platform->supportsForeignKeyConstraints()) { - $this->conn->delete( - $this->quoteStrategy->getTableName($this->em->getClassMetadata($this->class->rootEntityName), $this->platform), $id - ); - } else { - // Delete from all tables individually, starting from this class' table up to the root table. - $this->conn->delete($this->quoteStrategy->getTableName($this->class, $this->platform), $id); + $rootClass = $this->em->getClassMetadata($this->class->rootEntityName); + $rootTable = $this->quoteStrategy->getTableName($rootClass, $this->platform); - foreach ($this->class->parentClasses as $parentClass) { - $this->conn->delete( - $this->quoteStrategy->getTableName($this->em->getClassMetadata($parentClass), $this->platform), $id - ); - } + $this->conn->delete($rootTable, $id); + $this->conn->delete($tableName, $identifier); + + return; + } + + // Delete from all tables individually, starting from this class' table up to the root table. + $rootTable = $this->quoteStrategy->getTableName($this->class, $this->platform); + + $this->conn->delete($rootTable, $id); + + foreach ($this->class->parentClasses as $parentClass) { + $parentMetadata = $this->em->getClassMetadata($parentClass); + $parentTable = $this->quoteStrategy->getTableName($parentMetadata, $this->platform); + + $this->conn->delete($parentTable, $id); } } @@ -268,112 +292,43 @@ class JoinedSubclassPersister extends AbstractEntityInheritancePersister */ protected function getSelectSQL($criteria, $assoc = null, $lockMode = 0, $limit = null, $offset = null, array $orderBy = null) { - $idColumns = $this->class->getIdentifierColumnNames(); - $baseTableAlias = $this->getSQLTableAlias($this->class->name); + $joinSql = ''; + $identifierColumn = $this->class->getIdentifierColumnNames(); + $baseTableAlias = $this->getSQLTableAlias($this->class->name); - // Create the column list fragment only once - if ($this->selectColumnListSql === null) { - - $this->rsm = new ResultSetMapping(); - $this->rsm->addEntityResult($this->class->name, 'r'); - - // Add regular columns - $columnList = ''; - - foreach ($this->class->fieldMappings as $fieldName => $mapping) { - if ($columnList != '') $columnList .= ', '; - - $columnList .= $this->getSelectColumnSQL( - $fieldName, - isset($mapping['inherited']) ? $this->em->getClassMetadata($mapping['inherited']) : $this->class - ); - } - - // Add foreign key columns - foreach ($this->class->associationMappings as $assoc2) { - if ($assoc2['isOwningSide'] && $assoc2['type'] & ClassMetadata::TO_ONE) { - $tableAlias = isset($assoc2['inherited']) ? $this->getSQLTableAlias($assoc2['inherited']) : $baseTableAlias; - - foreach ($assoc2['targetToSourceKeyColumns'] as $srcColumn) { - if ($columnList != '') $columnList .= ', '; - - $columnList .= $this->getSelectJoinColumnSQL( - $tableAlias, - $srcColumn, - isset($assoc2['inherited']) ? $assoc2['inherited'] : $this->class->name - ); - } - } - } - - // Add discriminator column (DO NOT ALIAS, see AbstractEntityInheritancePersister#_processSQLResult). - $discrColumn = $this->class->discriminatorColumn['name']; - $tableAlias = ($this->class->rootEntityName == $this->class->name) ? $baseTableAlias : $this->getSQLTableAlias($this->class->rootEntityName); - $columnList .= ', ' . $tableAlias . '.' . $discrColumn; - - $resultColumnName = $this->platform->getSQLResultCasing($discrColumn); - - $this->rsm->setDiscriminatorColumn('r', $resultColumnName); - $this->rsm->addMetaResult('r', $resultColumnName, $discrColumn); - } // INNER JOIN parent tables - $joinSql = ''; - foreach ($this->class->parentClasses as $parentClassName) { - $parentClass = $this->em->getClassMetadata($parentClassName); - $tableAlias = $this->getSQLTableAlias($parentClassName); - $joinSql .= ' INNER JOIN ' . $this->quoteStrategy->getTableName($parentClass, $this->platform) . ' ' . $tableAlias . ' ON '; - $first = true; + $contitions = array(); + $parentClass = $this->em->getClassMetadata($parentClassName); + $tableAlias = $this->getSQLTableAlias($parentClassName); + $joinSql .= ' INNER JOIN ' . $this->quoteStrategy->getTableName($parentClass, $this->platform) . ' ' . $tableAlias . ' ON '; - foreach ($idColumns as $idColumn) { - if ($first) $first = false; else $joinSql .= ' AND '; - $joinSql .= $baseTableAlias . '.' . $idColumn . ' = ' . $tableAlias . '.' . $idColumn; + foreach ($identifierColumn as $idColumn) { + $contitions[] = $baseTableAlias . '.' . $idColumn . ' = ' . $tableAlias . '.' . $idColumn; } + + $joinSql .= implode(' AND ', $contitions); } // OUTER JOIN sub tables foreach ($this->class->subClasses as $subClassName) { - $subClass = $this->em->getClassMetadata($subClassName); - $tableAlias = $this->getSQLTableAlias($subClassName); + $contitions = array(); + $subClass = $this->em->getClassMetadata($subClassName); + $tableAlias = $this->getSQLTableAlias($subClassName); + $joinSql .= ' LEFT JOIN ' . $this->quoteStrategy->getTableName($subClass, $this->platform) . ' ' . $tableAlias . ' ON '; - if ($this->selectColumnListSql === null) { - // Add subclass columns - foreach ($subClass->fieldMappings as $fieldName => $mapping) { - if (isset($mapping['inherited'])) continue; - - $columnList .= ', ' . $this->getSelectColumnSQL($fieldName, $subClass); - } - - // Add join columns (foreign keys) - foreach ($subClass->associationMappings as $assoc2) { - if ($assoc2['isOwningSide'] && $assoc2['type'] & ClassMetadata::TO_ONE && ! isset($assoc2['inherited'])) { - foreach ($assoc2['targetToSourceKeyColumns'] as $srcColumn) { - if ($columnList != '') $columnList .= ', '; - - $columnList .= $this->getSelectJoinColumnSQL( - $tableAlias, - $srcColumn, - isset($assoc2['inherited']) ? $assoc2['inherited'] : $subClass->name - ); - } - } - } + foreach ($identifierColumn as $idColumn) { + $contitions[] = $baseTableAlias . '.' . $idColumn . ' = ' . $tableAlias . '.' . $idColumn; } - // Add LEFT JOIN - $joinSql .= ' LEFT JOIN ' . $this->quoteStrategy->getTableName($subClass, $this->platform) . ' ' . $tableAlias . ' ON '; - $first = true; - - foreach ($idColumns as $idColumn) { - if ($first) $first = false; else $joinSql .= ' AND '; - - $joinSql .= $baseTableAlias . '.' . $idColumn . ' = ' . $tableAlias . '.' . $idColumn; - } + $joinSql .= implode(' AND ', $contitions); } - $joinSql .= ($assoc != null && $assoc['type'] == ClassMetadata::MANY_TO_MANY) ? $this->getSelectManyToManyJoinSQL($assoc) : ''; + if ($assoc != null && $assoc['type'] == ClassMetadata::MANY_TO_MANY) { + $joinSql .= $this->getSelectManyToManyJoinSQL($assoc); + } $conditionSql = ($criteria instanceof Criteria) ? $this->getSelectConditionCriteriaSQL($criteria) @@ -381,33 +336,48 @@ class JoinedSubclassPersister extends AbstractEntityInheritancePersister // If the current class in the root entity, add the filters if ($filterSql = $this->generateFilterConditionSQL($this->em->getClassMetadata($this->class->rootEntityName), $this->getSQLTableAlias($this->class->rootEntityName))) { - if ($conditionSql) { - $conditionSql .= ' AND '; - } - - $conditionSql .= $filterSql; + $conditionSql .= $conditionSql + ? ' AND ' . $filterSql + : $filterSql; } - $orderBy = ($assoc !== null && isset($assoc['orderBy'])) ? $assoc['orderBy'] : $orderBy; - $orderBySql = $orderBy ? $this->getOrderBySQL($orderBy, $baseTableAlias) : ''; + $orderBySql = ''; - if ($this->selectColumnListSql === null) { - $this->selectColumnListSql = $columnList; + if ($assoc !== null && isset($assoc['orderBy'])) { + $orderBy = $assoc['orderBy']; + } + + if ($orderBy) { + $orderBySql = $this->getOrderBySQL($orderBy, $baseTableAlias); } $lockSql = ''; - if ($lockMode == LockMode::PESSIMISTIC_READ) { - $lockSql = ' ' . $this->platform->getReadLockSql(); - } else if ($lockMode == LockMode::PESSIMISTIC_WRITE) { - $lockSql = ' ' . $this->platform->getWriteLockSql(); + switch ($lockMode) { + case LockMode::PESSIMISTIC_READ: + + $lockSql = ' ' . $this->platform->getReadLockSql(); + + break; + + case LockMode::PESSIMISTIC_WRITE: + + $lockSql = ' ' . $this->platform->getWriteLockSql(); + + break; } - return $this->platform->modifyLimitQuery('SELECT ' . $this->selectColumnListSql - . ' FROM ' . $this->quoteStrategy->getTableName($this->class, $this->platform) . ' ' . $baseTableAlias - . $joinSql - . ($conditionSql != '' ? ' WHERE ' . $conditionSql : '') . $orderBySql, $limit, $offset) - . $lockSql; + $tableName = $this->quoteStrategy->getTableName($this->class, $this->platform); + $where = $conditionSql != '' ? ' WHERE ' . $conditionSql : ''; + $columnList = $this->getSelectColumnsSQL(); + $query = 'SELECT ' . $columnList + . ' FROM ' + . $tableName . ' ' . $baseTableAlias + . $joinSql + . $where + . $orderBySql; + + return $this->platform->modifyLimitQuery($query, $limit, $offset) . $lockSql; } /** @@ -417,26 +387,26 @@ class JoinedSubclassPersister extends AbstractEntityInheritancePersister */ public function getLockTablesSql() { - $idColumns = $this->class->getIdentifierColumnNames(); - $baseTableAlias = $this->getSQLTableAlias($this->class->name); + $joinSql = ''; + $identifierColumns = $this->class->getIdentifierColumnNames(); + $baseTableAlias = $this->getSQLTableAlias($this->class->name); + $quotedTableName = $this->quoteStrategy->getTableName($this->class, $this->platform); // INNER JOIN parent tables - $joinSql = ''; - foreach ($this->class->parentClasses as $parentClassName) { - $parentClass = $this->em->getClassMetadata($parentClassName); - $tableAlias = $this->getSQLTableAlias($parentClassName); - $joinSql .= ' INNER JOIN ' . $this->quoteStrategy->getTableName($parentClass, $this->platform) . ' ' . $tableAlias . ' ON '; - $first = true; + $conditions = array(); + $tableAlias = $this->getSQLTableAlias($parentClassName); + $parentClass = $this->em->getClassMetadata($parentClassName); + $joinSql .= ' INNER JOIN ' . $this->quoteStrategy->getTableName($parentClass, $this->platform) . ' ' . $tableAlias . ' ON '; - foreach ($idColumns as $idColumn) { - if ($first) $first = false; else $joinSql .= ' AND '; - - $joinSql .= $baseTableAlias . '.' . $idColumn . ' = ' . $tableAlias . '.' . $idColumn; + foreach ($identifierColumns as $idColumn) { + $conditions[] = $baseTableAlias . '.' . $idColumn . ' = ' . $tableAlias . '.' . $idColumn; } + + $joinSql .= implode(' AND ', $conditions); } - return 'FROM ' .$this->quoteStrategy->getTableName($this->class, $this->platform) . ' ' . $baseTableAlias . $joinSql; + return 'FROM ' . $quotedTableName . ' ' . $baseTableAlias . $joinSql; } /* @@ -444,7 +414,91 @@ class JoinedSubclassPersister extends AbstractEntityInheritancePersister */ protected function getSelectColumnsSQL() { - throw new \BadMethodCallException("Illegal invocation of ".__METHOD__."."); + // Create the column list fragment only once + if ($this->selectColumnListSql !== null) { + return $this->selectColumnListSql; + } + + $columnList = array(); + $this->rsm = new ResultSetMapping(); + $discrColumn = $this->class->discriminatorColumn['name']; + $baseTableAlias = $this->getSQLTableAlias($this->class->name); + $resultColumnName = $this->platform->getSQLResultCasing($discrColumn); + + $this->rsm->addEntityResult($this->class->name, 'r'); + $this->rsm->setDiscriminatorColumn('r', $resultColumnName); + $this->rsm->addMetaResult('r', $resultColumnName, $discrColumn); + + // Add regular columns + foreach ($this->class->fieldMappings as $fieldName => $mapping) { + $class = isset($mapping['inherited']) + ? $this->em->getClassMetadata($mapping['inherited']) + : $this->class; + + $columnList[] = $this->getSelectColumnSQL($fieldName, $class); + } + + // Add foreign key columns + foreach ($this->class->associationMappings as $mapping) { + if ( ! $mapping['isOwningSide'] || ! ($mapping['type'] & ClassMetadata::TO_ONE)) { + continue; + } + + $tableAlias = isset($mapping['inherited']) + ? $this->getSQLTableAlias($mapping['inherited']) + : $baseTableAlias; + + foreach ($mapping['targetToSourceKeyColumns'] as $srcColumn) { + $className = isset($mapping['inherited']) + ? $mapping['inherited'] + : $this->class->name; + + $columnList[] = $this->getSelectJoinColumnSQL($tableAlias, $srcColumn, $className); + } + } + + // Add discriminator column (DO NOT ALIAS, see AbstractEntityInheritancePersister#processSQLResult). + $tableAlias = ($this->class->rootEntityName == $this->class->name) + ? $baseTableAlias + : $this->getSQLTableAlias($this->class->rootEntityName); + + $columnList[] = $tableAlias . '.' . $discrColumn; + + // sub tables + foreach ($this->class->subClasses as $subClassName) { + $subClass = $this->em->getClassMetadata($subClassName); + $tableAlias = $this->getSQLTableAlias($subClassName); + + // Add subclass columns + foreach ($subClass->fieldMappings as $fieldName => $mapping) { + if (isset($mapping['inherited'])) { + continue; + } + + $columnList[] = $this->getSelectColumnSQL($fieldName, $subClass); + } + + // Add join columns (foreign keys) + foreach ($subClass->associationMappings as $mapping) { + if ( ! $mapping['isOwningSide'] + || ! ($mapping['type'] & ClassMetadata::TO_ONE) + || isset($mapping['inherited'])) { + continue; + } + + foreach ($mapping['targetToSourceKeyColumns'] as $srcColumn) { + $className = isset($mapping['inherited']) + ? $mapping['inherited'] + : $subClass->name; + + $columnList[] = $this->getSelectJoinColumnSQL($tableAlias, $srcColumn, $className); + } + } + } + + $this->selectColumnListSql = implode(', ', $columnList); + + return $this->selectColumnListSql; } /** @@ -453,10 +507,13 @@ class JoinedSubclassPersister extends AbstractEntityInheritancePersister protected function getInsertColumnList() { // Identifier columns must always come first in the column list of subclasses. - $columns = $this->class->parentClasses ? $this->class->getIdentifierColumnNames() : array(); + $columns = $this->class->parentClasses + ? $this->class->getIdentifierColumnNames() + : array(); foreach ($this->class->reflFields as $name => $field) { - if (isset($this->class->fieldMappings[$name]['inherited']) && ! isset($this->class->fieldMappings[$name]['id']) + if (isset($this->class->fieldMappings[$name]['inherited']) + && ! isset($this->class->fieldMappings[$name]['id']) || isset($this->class->associationMappings[$name]['inherited']) || ($this->class->isVersioned && $this->class->versionField == $name)) { continue; From 07492bda9db79f35b832a263bcbeecdb870effe2 Mon Sep 17 00:00:00 2001 From: "Fabio B. Silva" Date: Wed, 25 Jul 2012 22:28:45 -0300 Subject: [PATCH 06/16] fix JoinedSubclassPersister#delete when supports foreign key --- lib/Doctrine/ORM/Persisters/JoinedSubclassPersister.php | 1 - 1 file changed, 1 deletion(-) diff --git a/lib/Doctrine/ORM/Persisters/JoinedSubclassPersister.php b/lib/Doctrine/ORM/Persisters/JoinedSubclassPersister.php index 5f2bd11c6..58aa7df17 100644 --- a/lib/Doctrine/ORM/Persisters/JoinedSubclassPersister.php +++ b/lib/Doctrine/ORM/Persisters/JoinedSubclassPersister.php @@ -269,7 +269,6 @@ class JoinedSubclassPersister extends AbstractEntityInheritancePersister $rootTable = $this->quoteStrategy->getTableName($rootClass, $this->platform); $this->conn->delete($rootTable, $id); - $this->conn->delete($tableName, $identifier); return; } From 2b1aaebe18e2fb2071cf85422891494e1518ba09 Mon Sep 17 00:00:00 2001 From: "Fabio B. Silva" Date: Fri, 27 Jul 2012 21:23:40 -0300 Subject: [PATCH 07/16] code refactoring on ManyToManyPersister --- .../ORM/Persisters/ManyToManyPersister.php | 54 +++++++++++-------- 1 file changed, 31 insertions(+), 23 deletions(-) diff --git a/lib/Doctrine/ORM/Persisters/ManyToManyPersister.php b/lib/Doctrine/ORM/Persisters/ManyToManyPersister.php index d8d9b7bf4..315888676 100644 --- a/lib/Doctrine/ORM/Persisters/ManyToManyPersister.php +++ b/lib/Doctrine/ORM/Persisters/ManyToManyPersister.php @@ -40,9 +40,10 @@ class ManyToManyPersister extends AbstractCollectionPersister */ protected function getDeleteRowSQL(PersistentCollection $coll) { - $columns = array(); - $mapping = $coll->getMapping(); - $class = $this->em->getClassMetadata(get_class($coll->getOwner())); + $columns = array(); + $mapping = $coll->getMapping(); + $class = $this->em->getClassMetadata(get_class($coll->getOwner())); + $tableName = $this->quoteStrategy->getJoinTableName($mapping, $class, $this->platform); foreach ($mapping['joinTable']['joinColumns'] as $joinColumn) { $columns[] = $this->quoteStrategy->getJoinColumnName($joinColumn, $class, $this->platform); @@ -52,7 +53,7 @@ class ManyToManyPersister extends AbstractCollectionPersister $columns[] = $this->quoteStrategy->getJoinColumnName($joinColumn, $class, $this->platform); } - return 'DELETE FROM ' . $this->quoteStrategy->getJoinTableName($mapping, $class, $this->platform) + return 'DELETE FROM ' . $tableName . ' WHERE ' . implode(' = ? AND ', $columns) . ' = ?'; } @@ -328,8 +329,9 @@ class ManyToManyPersister extends AbstractCollectionPersister */ private function getJoinTableRestrictions(PersistentCollection $coll, $element, $addFilters) { - $uow = $this->em->getUnitOfWork(); - $mapping = $filterMapping = $coll->getMapping(); + $uow = $this->em->getUnitOfWork(); + $filterMapping = $coll->getMapping(); + $mapping = $filterMapping; if ( ! $mapping['isOwningSide']) { $sourceClass = $this->em->getClassMetadata($mapping['targetEntity']); @@ -392,32 +394,38 @@ class ManyToManyPersister extends AbstractCollectionPersister public function getFilterSql($mapping) { $targetClass = $this->em->getClassMetadata($mapping['targetEntity']); + $rootClass = $this->em->getClassMetadata($targetClass->rootEntityName); + $filterSql = $this->generateFilterConditionSQL($rootClass, 'te'); - if ($mapping['isOwningSide']) { - $joinColumns = $mapping['relationToTargetKeyColumns']; - } else { - $mapping = $targetClass->associationMappings[$mapping['mappedBy']]; - $joinColumns = $mapping['relationToSourceKeyColumns']; + if ('' === $filterSql) { + return array('', ''); } - $targetClass = $this->em->getClassMetadata($targetClass->rootEntityName); + $conditions = array(); + $association = $mapping; + + if ( ! $mapping['isOwningSide']) { + $class = $this->em->getClassMetadata($mapping['targetEntity']); + $association = $class->associationMappings[$mapping['mappedBy']]; + } // A join is needed if there is filtering on the target entity - $joinTargetEntitySQL = ''; - if ($filterSql = $this->generateFilterConditionSQL($targetClass, 'te')) { - $joinTargetEntitySQL = ' JOIN ' - . $this->quoteStrategy->getTableName($targetClass, $this->platform) . ' te' - . ' ON'; + $tableName = $this->quoteStrategy->getTableName($rootClass, $this->platform); + $joinSql = ' JOIN ' . $tableName . ' te' . ' ON'; + $joinColumns = $mapping['isOwningSide'] + ? $association['joinTable']['inverseJoinColumns'] + : $association['joinTable']['joinColumns']; - $joinTargetEntitySQLClauses = array(); - foreach ($joinColumns as $joinTableColumn => $targetTableColumn) { - $joinTargetEntitySQLClauses[] = ' t.' . $joinTableColumn . ' = ' . 'te.' . $targetTableColumn; - } + foreach ($joinColumns as $joinColumn) { + $joinColumnName = $this->quoteStrategy->getJoinColumnName($joinColumn, $targetClass, $this->platform); + $refColumnName = $this->quoteStrategy->getReferencedJoinColumnName($joinColumn, $targetClass, $this->platform); - $joinTargetEntitySQL .= implode(' AND ', $joinTargetEntitySQLClauses); + $conditions[] = ' t.' . $joinColumnName . ' = ' . 'te.' . $refColumnName; } - return array($joinTargetEntitySQL, $filterSql); + $joinSql .= implode(' AND ', $conditions); + + return array($joinSql, $filterSql); } /** From 9a041c8fdbb2ee92e40f00285a52b3c76f56c61c Mon Sep 17 00:00:00 2001 From: "Fabio B. Silva" Date: Fri, 27 Jul 2012 22:20:49 -0300 Subject: [PATCH 08/16] code refactoring on OneToManyPersister --- lib/Doctrine/ORM/Persisters/OneToManyPersister.php | 13 ++++++++----- 1 file changed, 8 insertions(+), 5 deletions(-) diff --git a/lib/Doctrine/ORM/Persisters/OneToManyPersister.php b/lib/Doctrine/ORM/Persisters/OneToManyPersister.php index c88d755b3..100e834cf 100644 --- a/lib/Doctrine/ORM/Persisters/OneToManyPersister.php +++ b/lib/Doctrine/ORM/Persisters/OneToManyPersister.php @@ -42,11 +42,13 @@ class OneToManyPersister extends AbstractCollectionPersister */ protected function getDeleteRowSQL(PersistentCollection $coll) { - $mapping = $coll->getMapping(); - $class = $this->em->getClassMetadata($mapping['targetEntity']); + $mapping = $coll->getMapping(); + $class = $this->em->getClassMetadata($mapping['targetEntity']); + $tableName = $this->quoteStrategy->getTableName($class, $this->platform); + $idColumns = $class->getIdentifierColumnNames(); - return 'DELETE FROM ' . $this->quoteStrategy->getTableName($class, $this->platform) - . ' WHERE ' . implode('= ? AND ', $class->getIdentifierColumnNames()) . ' = ?'; + return 'DELETE FROM ' . $tableName + . ' WHERE ' . implode('= ? AND ', $idColumns) . ' = ?'; } /** @@ -119,7 +121,8 @@ class OneToManyPersister extends AbstractCollectionPersister $whereClauses = array(); $params = array(); - foreach ($targetClass->associationMappings[$mapping['mappedBy']]['joinColumns'] as $joinColumn) { + $joinColumns = $targetClass->associationMappings[$mapping['mappedBy']]['joinColumns']; + foreach ($joinColumns as $joinColumn) { $whereClauses[] = $joinColumn['name'] . ' = ?'; $params[] = ($targetClass->containsForeignIdentifier) From 14a2b616716bdc1c774d020758791d8154f8bffa Mon Sep 17 00:00:00 2001 From: "Fabio B. Silva" Date: Fri, 27 Jul 2012 22:29:52 -0300 Subject: [PATCH 09/16] code refactoring on SingleTablePersister --- .../ORM/Persisters/SingleTablePersister.php | 47 +++++++++++-------- 1 file changed, 28 insertions(+), 19 deletions(-) diff --git a/lib/Doctrine/ORM/Persisters/SingleTablePersister.php b/lib/Doctrine/ORM/Persisters/SingleTablePersister.php index 487dd1c05..ed3924fbe 100644 --- a/lib/Doctrine/ORM/Persisters/SingleTablePersister.php +++ b/lib/Doctrine/ORM/Persisters/SingleTablePersister.php @@ -34,27 +34,31 @@ use Doctrine\Common\Collections\Criteria; */ class SingleTablePersister extends AbstractEntityInheritancePersister { - /** {@inheritdoc} */ + /** + * {@inheritdoc} + */ protected function getDiscriminatorColumnTableName() { return $this->class->getTableName(); } - /** {@inheritdoc} */ + /** + * {@inheritdoc} + */ protected function getSelectColumnsSQL() { if ($this->selectColumnListSql !== null) { return $this->selectColumnListSql; } - $columnList = parent::getSelectColumnsSQL(); + $columnList[] = parent::getSelectColumnsSQL(); $rootClass = $this->em->getClassMetadata($this->class->rootEntityName); $tableAlias = $this->getSQLTableAlias($rootClass->name); // Append discriminator column - $discrColumn = $this->class->discriminatorColumn['name']; - $columnList .= ', ' . $tableAlias . '.' . $discrColumn; + $discrColumn = $this->class->discriminatorColumn['name']; + $columnList[] = $tableAlias . '.' . $discrColumn; $resultColumnName = $this->platform->getSQLResultCasing($discrColumn); @@ -67,28 +71,30 @@ class SingleTablePersister extends AbstractEntityInheritancePersister // Regular columns foreach ($subClass->fieldMappings as $fieldName => $mapping) { - if ( ! isset($mapping['inherited'])) { - $columnList .= ', ' . $this->getSelectColumnSQL($fieldName, $subClass); + if (isset($mapping['inherited'])) { + continue; } + + $columnList[] = $this->getSelectColumnSQL($fieldName, $subClass); } // Foreign key columns foreach ($subClass->associationMappings as $assoc) { - if ($assoc['isOwningSide'] && $assoc['type'] & ClassMetadata::TO_ONE && ! isset($assoc['inherited'])) { - foreach ($assoc['targetToSourceKeyColumns'] as $srcColumn) { - if ($columnList != '') $columnList .= ', '; + if ( ! $assoc['isOwningSide'] + || ! ($assoc['type'] & ClassMetadata::TO_ONE) + || isset($assoc['inherited'])) { + continue; + } - $columnList .= $this->getSelectJoinColumnSQL( - $tableAlias, - $srcColumn, - isset($assoc['inherited']) ? $assoc['inherited'] : $this->class->name - ); - } + foreach ($assoc['targetToSourceKeyColumns'] as $srcColumn) { + $className = isset($assoc['inherited']) ? $assoc['inherited'] : $this->class->name; + $columnList[] = $this->getSelectJoinColumnSQL($tableAlias, $srcColumn, $className); } } } - $this->selectColumnListSql = $columnList; + $this->selectColumnListSql = implode(', ', $columnList); + return $this->selectColumnListSql; } @@ -155,8 +161,11 @@ class SingleTablePersister extends AbstractEntityInheritancePersister $values[] = $this->conn->quote($discrValues[$subclassName]); } - return $this->getSQLTableAlias($this->class->name) . '.' . $this->class->discriminatorColumn['name'] - . ' IN (' . implode(', ', $values) . ')'; + $values = implode(', ', $values); + $discColumn = $this->class->discriminatorColumn['name']; + $tableAlias = $this->getSQLTableAlias($this->class->name); + + return $tableAlias . '.' . $discColumn . ' IN (' . $values . ')'; } /** From 04e1838c9249bf366e0c01c7ff2109aa2e9c44f2 Mon Sep 17 00:00:00 2001 From: "Fabio B. Silva" Date: Sat, 28 Jul 2012 21:44:34 -0300 Subject: [PATCH 10/16] change 'use' statements --- lib/Doctrine/ORM/Persisters/ElementCollectionPersister.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/Doctrine/ORM/Persisters/ElementCollectionPersister.php b/lib/Doctrine/ORM/Persisters/ElementCollectionPersister.php index 0b0354af7..e30d5fbce 100644 --- a/lib/Doctrine/ORM/Persisters/ElementCollectionPersister.php +++ b/lib/Doctrine/ORM/Persisters/ElementCollectionPersister.php @@ -26,5 +26,5 @@ namespace Doctrine\ORM\Persisters; */ abstract class ElementCollectionPersister extends AbstractCollectionPersister { - //put your code here + } From 827153624c494040835968ef7e2c483368c85591 Mon Sep 17 00:00:00 2001 From: "Fabio B. Silva" Date: Sat, 28 Jul 2012 23:00:26 -0300 Subject: [PATCH 11/16] Second round of refactorings on BasicEntityPersister --- .../ORM/Persisters/BasicEntityPersister.php | 189 +++++++++--------- 1 file changed, 100 insertions(+), 89 deletions(-) diff --git a/lib/Doctrine/ORM/Persisters/BasicEntityPersister.php b/lib/Doctrine/ORM/Persisters/BasicEntityPersister.php index 1fadb8036..0bc4801fb 100644 --- a/lib/Doctrine/ORM/Persisters/BasicEntityPersister.php +++ b/lib/Doctrine/ORM/Persisters/BasicEntityPersister.php @@ -413,31 +413,32 @@ class BasicEntityPersister $identifier = $this->em->getUnitOfWork()->getEntityIdentifier($entity); foreach ($this->class->identifier as $idField) { - if (isset($this->class->associationMappings[$idField])) { + if ( ! isset($this->class->associationMappings[$idField])) { - $params[] = $identifier[$idField]; - $where[] = $this->class->associationMappings[$idField]['joinColumns'][0]['name']; - $targetMapping = $this->em->getClassMetadata($this->class->associationMappings[$idField]['targetEntity']); - - switch (true) { - case (isset($targetMapping->fieldMappings[$targetMapping->identifier[0]])): - $types[] = $targetMapping->fieldMappings[$targetMapping->identifier[0]]['type']; - break; - - case (isset($targetMapping->associationMappings[$targetMapping->identifier[0]])): - $types[] = $targetMapping->associationMappings[$targetMapping->identifier[0]]['type']; - break; - - default: - throw ORMException::unrecognizedField($targetMapping->identifier[0]); - } + $params[] = $identifier[$idField]; + $types[] = $this->class->fieldMappings[$idField]['type']; + $where[] = $this->quoteStrategy->getColumnName($idField, $this->class, $this->platform); continue; } - $params[] = $identifier[$idField]; - $types[] = $this->class->fieldMappings[$idField]['type']; - $where[] = $this->quoteStrategy->getColumnName($idField, $this->class, $this->platform); + $params[] = $identifier[$idField]; + $where[] = $this->class->associationMappings[$idField]['joinColumns'][0]['name']; + $targetMapping = $this->em->getClassMetadata($this->class->associationMappings[$idField]['targetEntity']); + + switch (true) { + case (isset($targetMapping->fieldMappings[$targetMapping->identifier[0]])): + $types[] = $targetMapping->fieldMappings[$targetMapping->identifier[0]]['type']; + break; + + case (isset($targetMapping->associationMappings[$targetMapping->identifier[0]])): + $types[] = $targetMapping->associationMappings[$targetMapping->identifier[0]]['type']; + break; + + default: + throw ORMException::unrecognizedField($targetMapping->identifier[0]); + } + } if ($versioned) { @@ -586,59 +587,68 @@ class BasicEntityPersister $newVal = $change[1]; - if (isset($this->class->associationMappings[$field])) { - $assoc = $this->class->associationMappings[$field]; + if ( ! isset($this->class->associationMappings[$field])) { - // Only owning side of x-1 associations can have a FK column. - if ( ! $assoc['isOwningSide'] || ! ($assoc['type'] & ClassMetadata::TO_ONE)) { - continue; - } - - if ($newVal !== null) { - $oid = spl_object_hash($newVal); - - if (isset($this->queuedInserts[$oid]) || $uow->isScheduledForInsert($newVal)) { - // The associated entity $newVal is not yet persisted, so we must - // set $newVal = null, in order to insert a null value and schedule an - // extra update on the UnitOfWork. - $uow->scheduleExtraUpdate($entity, array( - $field => array(null, $newVal) - )); - $newVal = null; - } - } - - if ($newVal !== null) { - $newValId = $uow->getEntityIdentifier($newVal); - } - - $targetClass = $this->em->getClassMetadata($assoc['targetEntity']); - $owningTable = $this->getOwningTable($field); - - foreach ($assoc['joinColumns'] as $joinColumn) { - $sourceColumn = $joinColumn['name']; - $targetColumn = $joinColumn['referencedColumnName']; - $quotedColumn = $this->quoteStrategy->getJoinColumnName($joinColumn, $this->class, $this->platform); - - $this->quotedColumns[$sourceColumn] = $quotedColumn; - - if ($newVal === null) { - $result[$owningTable][$sourceColumn] = null; - } else if ($targetClass->containsForeignIdentifier) { - $result[$owningTable][$sourceColumn] = $newValId[$targetClass->getFieldForColumn($targetColumn)]; - } else { - $result[$owningTable][$sourceColumn] = $newValId[$targetClass->fieldNames[$targetColumn]]; - } - - $this->columnTypes[$sourceColumn] = $targetClass->getTypeOfColumn($targetColumn); - } + $columnName = $this->class->columnNames[$field]; + $this->columnTypes[$columnName] = $this->class->fieldMappings[$field]['type']; + $result[$this->getOwningTable($field)][$columnName] = $newVal; continue; } - - $columnName = $this->class->columnNames[$field]; - $this->columnTypes[$columnName] = $this->class->fieldMappings[$field]['type']; - $result[$this->getOwningTable($field)][$columnName] = $newVal; + + $assoc = $this->class->associationMappings[$field]; + + // Only owning side of x-1 associations can have a FK column. + if ( ! $assoc['isOwningSide'] || ! ($assoc['type'] & ClassMetadata::TO_ONE)) { + continue; + } + + if ($newVal !== null) { + $oid = spl_object_hash($newVal); + + if (isset($this->queuedInserts[$oid]) || $uow->isScheduledForInsert($newVal)) { + // The associated entity $newVal is not yet persisted, so we must + // set $newVal = null, in order to insert a null value and schedule an + // extra update on the UnitOfWork. + $uow->scheduleExtraUpdate($entity, array( + $field => array(null, $newVal) + )); + $newVal = null; + } + } + + if ($newVal !== null) { + $newValId = $uow->getEntityIdentifier($newVal); + } + + $targetClass = $this->em->getClassMetadata($assoc['targetEntity']); + $owningTable = $this->getOwningTable($field); + + foreach ($assoc['joinColumns'] as $joinColumn) { + $sourceColumn = $joinColumn['name']; + $targetColumn = $joinColumn['referencedColumnName']; + $quotedColumn = $this->quoteStrategy->getJoinColumnName($joinColumn, $this->class, $this->platform); + + $this->quotedColumns[$sourceColumn] = $quotedColumn; + $this->columnTypes[$sourceColumn] = $targetClass->getTypeOfColumn($targetColumn); + + switch (true) { + case $newVal === null: + $value = null; + break; + + case $targetClass->containsForeignIdentifier: + $value = $newValId[$targetClass->getFieldForColumn($targetColumn)]; + break; + + default: + $value = $newValId[$targetClass->fieldNames[$targetColumn]]; + break; + } + + $result[$owningTable][$sourceColumn] = $value; + } + } return $result; @@ -747,31 +757,33 @@ class BasicEntityPersister if ($targetEntity !== null && $isInverseSingleValued) { $targetClass->reflFields[$assoc['inversedBy']]->setValue($targetEntity, $sourceEntity); } - } else { - $sourceClass = $this->em->getClassMetadata($assoc['sourceEntity']); - $owningAssoc = $targetClass->getAssociationMapping($assoc['mappedBy']); - // TRICKY: since the association is specular source and target are flipped - foreach ($owningAssoc['targetToSourceKeyColumns'] as $sourceKeyColumn => $targetKeyColumn) { - if ( ! isset($sourceClass->fieldNames[$sourceKeyColumn])) { - throw MappingException::joinColumnMustPointToMappedField( - $sourceClass->name, $sourceKeyColumn - ); - } + return $targetEntity; + } - // unset the old value and set the new sql aliased value here. By definition - // unset($identifier[$targetKeyColumn] works here with how UnitOfWork::createEntity() calls this method. - $identifier[$this->getSQLTableAlias($targetClass->name) . "." . $targetKeyColumn] = - $sourceClass->reflFields[$sourceClass->fieldNames[$sourceKeyColumn]]->getValue($sourceEntity); + $sourceClass = $this->em->getClassMetadata($assoc['sourceEntity']); + $owningAssoc = $targetClass->getAssociationMapping($assoc['mappedBy']); - unset($identifier[$targetKeyColumn]); + // TRICKY: since the association is specular source and target are flipped + foreach ($owningAssoc['targetToSourceKeyColumns'] as $sourceKeyColumn => $targetKeyColumn) { + if ( ! isset($sourceClass->fieldNames[$sourceKeyColumn])) { + throw MappingException::joinColumnMustPointToMappedField( + $sourceClass->name, $sourceKeyColumn + ); } - $targetEntity = $this->load($identifier, null, $assoc); + // unset the old value and set the new sql aliased value here. By definition + // unset($identifier[$targetKeyColumn] works here with how UnitOfWork::createEntity() calls this method. + $identifier[$this->getSQLTableAlias($targetClass->name) . "." . $targetKeyColumn] = + $sourceClass->reflFields[$sourceClass->fieldNames[$sourceKeyColumn]]->getValue($sourceEntity); - if ($targetEntity !== null) { - $targetClass->setFieldValue($targetEntity, $assoc['mappedBy'], $sourceEntity); - } + unset($identifier[$targetKeyColumn]); + } + + $targetEntity = $this->load($identifier, null, $assoc); + + if ($targetEntity !== null) { + $targetClass->setFieldValue($targetEntity, $assoc['mappedBy'], $sourceEntity); } return $targetEntity; @@ -974,7 +986,6 @@ class BasicEntityPersister foreach ($joinColumns as $joinColumn) { - $relationKeyColumn = $joinColumn['name']; $sourceKeyColumn = $joinColumn['referencedColumnName']; $quotedKeyColumn = $this->quoteStrategy->getJoinColumnName($joinColumn, $class, $this->platform); From a295525501213c57738070c5ff7ebe33d0df5ffe Mon Sep 17 00:00:00 2001 From: "Fabio B. Silva" Date: Sun, 12 Aug 2012 15:17:47 -0300 Subject: [PATCH 12/16] Fix some CS --- .../AbstractCollectionPersister.php | 7 ++++ .../ORM/Persisters/BasicEntityPersister.php | 35 +++++++++---------- 2 files changed, 23 insertions(+), 19 deletions(-) diff --git a/lib/Doctrine/ORM/Persisters/AbstractCollectionPersister.php b/lib/Doctrine/ORM/Persisters/AbstractCollectionPersister.php index bd32fcc05..381c6e6fd 100644 --- a/lib/Doctrine/ORM/Persisters/AbstractCollectionPersister.php +++ b/lib/Doctrine/ORM/Persisters/AbstractCollectionPersister.php @@ -87,6 +87,7 @@ abstract class AbstractCollectionPersister } $sql = $this->getDeleteSQL($coll); + $this->conn->executeUpdate($sql, $this->getDeleteSQLParameters($coll)); } @@ -157,6 +158,7 @@ abstract class AbstractCollectionPersister * Count the size of this persistent collection * * @param \Doctrine\ORM\PersistentCollection $coll + * * @return integer */ public function count(PersistentCollection $coll) @@ -170,6 +172,7 @@ abstract class AbstractCollectionPersister * @param \Doctrine\ORM\PersistentCollection $coll * @param integer $offset * @param integer $length + * * @return array */ public function slice(PersistentCollection $coll, $offset, $length = null) @@ -182,6 +185,7 @@ abstract class AbstractCollectionPersister * * @param \Doctrine\ORM\PersistentCollection $coll * @param mixed \Doctrine\ORM\PersistentCollection + * * @return boolean */ public function contains(PersistentCollection $coll, $element) @@ -194,6 +198,7 @@ abstract class AbstractCollectionPersister * * @param \Doctrine\ORM\PersistentCollection $coll * @param mixed $key + * * @return boolean */ public function containsKey(PersistentCollection $coll, $key) @@ -206,6 +211,7 @@ abstract class AbstractCollectionPersister * * @param \Doctrine\ORM\PersistentCollection $coll * @param object $element + * * @return mixed */ public function removeElement(PersistentCollection $coll, $element) @@ -217,6 +223,7 @@ abstract class AbstractCollectionPersister * Remove an element by key * * @param \Doctrine\ORM\PersistentCollection $coll + * * @param mixed $key */ public function removeKey(PersistentCollection $coll, $key) diff --git a/lib/Doctrine/ORM/Persisters/BasicEntityPersister.php b/lib/Doctrine/ORM/Persisters/BasicEntityPersister.php index 0bc4801fb..7376a33a3 100644 --- a/lib/Doctrine/ORM/Persisters/BasicEntityPersister.php +++ b/lib/Doctrine/ORM/Persisters/BasicEntityPersister.php @@ -302,6 +302,7 @@ class BasicEntityPersister protected function assignDefaultVersionValue($entity, $id) { $value = $this->fetchVersionValue($this->class, $id); + $this->class->setFieldValue($entity, $this->class->versionField, $value); } @@ -324,11 +325,9 @@ class BasicEntityPersister . ' FROM ' . $tableName . ' WHERE ' . implode(' = ? AND ', $identifier) . ' = ?'; - $value = $this->conn->fetchColumn($sql, array_values((array)$id)); - $value = Type::getType($versionedClass->fieldMappings[$versionField]['type']) - ->convertToPHPValue($value, $this->platform); + $value = $this->conn->fetchColumn($sql, array_values((array) $id)); - return $value; + return Type::getType($versionedClass->fieldMappings[$versionField]['type'])->convertToPHPValue($value, $this->platform); } /** @@ -454,6 +453,7 @@ class BasicEntityPersister case Type::INTEGER: $set[] = $versionColumn . ' = ' . $versionColumn . ' + 1'; break; + case Type::DATETIME: $set[] = $versionColumn . ' = CURRENT_TIMESTAMP'; break; @@ -610,9 +610,8 @@ class BasicEntityPersister // The associated entity $newVal is not yet persisted, so we must // set $newVal = null, in order to insert a null value and schedule an // extra update on the UnitOfWork. - $uow->scheduleExtraUpdate($entity, array( - $field => array(null, $newVal) - )); + $uow->scheduleExtraUpdate($entity, array($field => array(null, $newVal))); + $newVal = null; } } @@ -918,8 +917,7 @@ class BasicEntityPersister $rsm->addIndexBy('r', $assoc['indexBy']); } - return $this->em->newHydrator(Query::HYDRATE_OBJECT) - ->hydrateAll($stmt, $rsm, $hints); + return $this->em->newHydrator(Query::HYDRATE_OBJECT)->hydrateAll($stmt, $rsm, $hints); } /** @@ -944,8 +942,7 @@ class BasicEntityPersister $rsm->addIndexBy('r', $assoc['indexBy']); } - return $this->em->newHydrator(Query::HYDRATE_OBJECT) - ->hydrateAll($stmt, $rsm, $hints); + return $this->em->newHydrator(Query::HYDRATE_OBJECT)->hydrateAll($stmt, $rsm, $hints); } /** @@ -1059,11 +1056,10 @@ class BasicEntityPersister switch ($lockMode) { case LockMode::PESSIMISTIC_READ: $lockSql = ' ' . $this->platform->getReadLockSql(); - break; + case LockMode::PESSIMISTIC_WRITE: $lockSql = ' ' . $this->platform->getWriteLockSql(); - break; } @@ -1082,13 +1078,14 @@ class BasicEntityPersister $from = ' FROM ' . $tableName . ' '. $tableAlias; $join = $this->selectJoinSql . $joinSql; $where = ($conditionSql ? ' WHERE ' . $conditionSql : ''); + $lock = $this->platform->appendLockHint($from, $lockMode); + $query = $select + . $lock + . $join + . $where + . $orderBySql; - return $this->platform->modifyLimitQuery($select - . $this->platform->appendLockHint($from, $lockMode) - . $join - . $where - . $orderBySql, $limit, $offset) - . $lockSql; + return $this->platform->modifyLimitQuery($query, $limit, $offset) . $lockSql; } /** From c8899c2b3b99883fa9afc36e58eb4cb438dec539 Mon Sep 17 00:00:00 2001 From: "Fabio B. Silva" Date: Sun, 12 Aug 2012 20:02:01 -0300 Subject: [PATCH 13/16] second round of CS fixes --- .../AbstractCollectionPersister.php | 1 + .../ORM/Persisters/BasicEntityPersister.php | 26 +++++++++---------- 2 files changed, 13 insertions(+), 14 deletions(-) diff --git a/lib/Doctrine/ORM/Persisters/AbstractCollectionPersister.php b/lib/Doctrine/ORM/Persisters/AbstractCollectionPersister.php index 381c6e6fd..d310e91e2 100644 --- a/lib/Doctrine/ORM/Persisters/AbstractCollectionPersister.php +++ b/lib/Doctrine/ORM/Persisters/AbstractCollectionPersister.php @@ -236,6 +236,7 @@ abstract class AbstractCollectionPersister * * @param \Doctrine\ORM\PersistentCollection $coll * @param mixed $index + * * @return object */ public function get(PersistentCollection $coll, $index) diff --git a/lib/Doctrine/ORM/Persisters/BasicEntityPersister.php b/lib/Doctrine/ORM/Persisters/BasicEntityPersister.php index 7376a33a3..fe8fe8ed9 100644 --- a/lib/Doctrine/ORM/Persisters/BasicEntityPersister.php +++ b/lib/Doctrine/ORM/Persisters/BasicEntityPersister.php @@ -381,7 +381,6 @@ class BasicEntityPersister $params = array(); foreach ($updateData as $columnName => $value) { - $placeholder = '?'; $column = $columnName; @@ -503,7 +502,7 @@ class BasicEntityPersister if ($selfReferential) { - $otherColumns = ! $mapping['isOwningSide'] + $otherColumns = (! $mapping['isOwningSide']) ? $association['joinTable']['joinColumns'] : $association['joinTable']['inverseJoinColumns']; } @@ -1109,9 +1108,9 @@ class BasicEntityPersister throw ORMException::invalidOrientation($this->class->name, $fieldName); } - $tableAlias = isset($this->class->fieldMappings[$fieldName]['inherited']) ? - $this->getSQLTableAlias($this->class->fieldMappings[$fieldName]['inherited']) - : $baseTableAlias; + $tableAlias = isset($this->class->fieldMappings[$fieldName]['inherited']) + ? $this->getSQLTableAlias($this->class->fieldMappings[$fieldName]['inherited']) + : $baseTableAlias; $columnName = $this->quoteStrategy->getColumnName($fieldName, $this->class, $this->platform); @@ -1197,7 +1196,6 @@ class BasicEntityPersister $joinTableName = $this->quoteStrategy->getTableName($eagerEntity, $this->platform); if ($assoc['isOwningSide']) { - $tableAlias = $this->getSQLTableAlias($association['targetEntity'], $assocAlias); $this->selectJoinSql .= ' ' . $this->getJoinSQLForJoinColumns($association['joinColumns']); @@ -1309,14 +1307,12 @@ class BasicEntityPersister return $this->insertSql; } - $insertSql = ''; $columns = $this->getInsertColumnList(); + $tableName = $this->quoteStrategy->getTableName($this->class, $this->platform); if (empty($columns)) { - $this->insertSql = $this->platform->getEmptyIdentityInsertSQL( - $this->quoteStrategy->getTableName($this->class, $this->platform), - $this->quoteStrategy->getColumnName($this->class->identifier[0], $this->class, $this->platform) - ); + $identityColumn = $this->quoteStrategy->getColumnName($this->class->identifier[0], $this->class, $this->platform); + $this->insertSql = $this->platform->getEmptyIdentityInsertSQL($tableName, $identityColumn); return $this->insertSql; } @@ -1331,15 +1327,17 @@ class BasicEntityPersister && isset($this->columnTypes[$this->class->fieldNames[$column]]) && isset($this->class->fieldMappings[$this->class->fieldNames[$column]]['requireSQLConversion'])) { - $type = Type::getType($this->columnTypes[$this->class->fieldNames[$column]]); + $type = Type::getType($this->columnTypes[$this->class->fieldNames[$column]]); $placeholder = $type->convertToDatabaseValueSQL('?', $this->platform); } $values[] = $placeholder; } - $this->insertSql = 'INSERT INTO ' . $this->quoteStrategy->getTableName($this->class, $this->platform) - . ' (' . implode(', ', $columns) . ') VALUES (' . implode(', ', $values) . ')'; + $columns = implode(', ', $columns); + $values = implode(', ', $values); + + $this->insertSql = sprintf('INSERT INTO %s (%s) VALUES (%s)', $tableName, $columns, $values); return $this->insertSql; } From ecaa3fd03e038450bb17b66405b289b8ea680697 Mon Sep 17 00:00:00 2001 From: "Fabio B. Silva" Date: Sat, 29 Sep 2012 15:20:59 -0300 Subject: [PATCH 14/16] Third round of refactorings on BasicEntityPersister --- .../ORM/Persisters/BasicEntityPersister.php | 61 +++++++++++-------- 1 file changed, 35 insertions(+), 26 deletions(-) diff --git a/lib/Doctrine/ORM/Persisters/BasicEntityPersister.php b/lib/Doctrine/ORM/Persisters/BasicEntityPersister.php index fe8fe8ed9..19988fb6c 100644 --- a/lib/Doctrine/ORM/Persisters/BasicEntityPersister.php +++ b/lib/Doctrine/ORM/Persisters/BasicEntityPersister.php @@ -1507,20 +1507,26 @@ class BasicEntityPersister */ public function getSelectConditionStatementSQL($field, $value, $assoc = null, $comparison = null) { - $conditionSql = $this->getSelectConditionStatementColumnSQL($field, $assoc); $placeholder = '?'; + $condition = $this->getSelectConditionStatementColumnSQL($field, $assoc); if (isset($this->class->fieldMappings[$field]['requireSQLConversion'])) { - $type = Type::getType($this->class->getTypeOfField($field)); - $placeholder = $type->convertToDatabaseValueSQL($placeholder, $this->platform); + $placeholder = Type::getType($this->class->getTypeOfField($field))->convertToDatabaseValueSQL($placeholder, $this->platform); } - $conditionSql .= ($comparison === null) - ? ((is_array($value)) ? ' IN (?)' : (($value === null) ? ' IS NULL' : ' = ' . $placeholder)) - : ' ' . sprintf(self::$comparisonMap[$comparison], $placeholder); + if ($comparison !== null) { + return $condition . ' ' . sprintf(self::$comparisonMap[$comparison], $placeholder); + } + if (is_array($value)) { + return sprintf('%s IN (%s)' , $condition, $placeholder); + } - return $conditionSql; + if ($value === null) { + return sprintf('%s IS NULL' , $condition); + } + + return sprintf('%s = %s' , $condition, $placeholder); } /** @@ -1533,31 +1539,34 @@ class BasicEntityPersister */ protected function getSelectConditionStatementColumnSQL($field, $assoc = null) { - switch (true) { - case (isset($this->class->columnNames[$field])): - $className = (isset($this->class->fieldMappings[$field]['inherited'])) - ? $this->class->fieldMappings[$field]['inherited'] - : $this->class->name; + if (isset($this->class->columnNames[$field])) { + $className = (isset($this->class->fieldMappings[$field]['inherited'])) + ? $this->class->fieldMappings[$field]['inherited'] + : $this->class->name; - return $this->getSQLTableAlias($className) . '.' . $this->quoteStrategy->getColumnName($field, $this->class, $this->platform); + return $this->getSQLTableAlias($className) . '.' . $this->quoteStrategy->getColumnName($field, $this->class, $this->platform); + } - case (isset($this->class->associationMappings[$field])): - if ( ! $this->class->associationMappings[$field]['isOwningSide']) { - throw ORMException::invalidFindByInverseAssociation($this->class->name, $field); - } + if (isset($this->class->associationMappings[$field])) { - $className = (isset($this->class->associationMappings[$field]['inherited'])) - ? $this->class->associationMappings[$field]['inherited'] - : $this->class->name; + if ( ! $this->class->associationMappings[$field]['isOwningSide']) { + throw ORMException::invalidFindByInverseAssociation($this->class->name, $field); + } - return $this->getSQLTableAlias($className) . '.' . $this->class->associationMappings[$field]['joinColumns'][0]['name']; + $joinColumn = $this->class->associationMappings[$field]['joinColumns'][0]; + $className = (isset($this->class->associationMappings[$field]['inherited'])) + ? $this->class->associationMappings[$field]['inherited'] + : $this->class->name; - case ($assoc !== null && strpos($field, " ") === false && strpos($field, "(") === false): - // very careless developers could potentially open up this normally hidden api for userland attacks, - // therefore checking for spaces and function calls which are not allowed. + return $this->getSQLTableAlias($className) . '.' . $this->quoteStrategy->getJoinColumnName($joinColumn, $this->class, $this->platform); + } - // found a join column condition, not really a "field" - return $field; + if ($assoc !== null && strpos($field, " ") === false && strpos($field, "(") === false) { + // very careless developers could potentially open up this normally hidden api for userland attacks, + // therefore checking for spaces and function calls which are not allowed. + + // found a join column condition, not really a "field" + return $field; } throw ORMException::unrecognizedField($field); From 0fa89647d2cf08162e8ccf91b02ca0338027caee Mon Sep 17 00:00:00 2001 From: "Fabio B. Silva" Date: Sat, 20 Oct 2012 00:39:51 -0300 Subject: [PATCH 15/16] Fix DocBlock --- .../AbstractCollectionPersister.php | 38 +++++++++---------- 1 file changed, 19 insertions(+), 19 deletions(-) diff --git a/lib/Doctrine/ORM/Persisters/AbstractCollectionPersister.php b/lib/Doctrine/ORM/Persisters/AbstractCollectionPersister.php index d310e91e2..e6cade2eb 100644 --- a/lib/Doctrine/ORM/Persisters/AbstractCollectionPersister.php +++ b/lib/Doctrine/ORM/Persisters/AbstractCollectionPersister.php @@ -157,9 +157,9 @@ abstract class AbstractCollectionPersister /** * Count the size of this persistent collection * - * @param \Doctrine\ORM\PersistentCollection $coll + * @param \Doctrine\ORM\PersistentCollection $coll * - * @return integer + * @return integer */ public function count(PersistentCollection $coll) { @@ -169,9 +169,9 @@ abstract class AbstractCollectionPersister /** * Slice elements * - * @param \Doctrine\ORM\PersistentCollection $coll - * @param integer $offset - * @param integer $length + * @param \Doctrine\ORM\PersistentCollection $coll + * @param integer $offset + * @param integer $length * * @return array */ @@ -183,10 +183,10 @@ abstract class AbstractCollectionPersister /** * Check for existance of an element * - * @param \Doctrine\ORM\PersistentCollection $coll - * @param mixed \Doctrine\ORM\PersistentCollection + * @param \Doctrine\ORM\PersistentCollection $coll + * @param \Doctrine\ORM\PersistentCollection * - * @return boolean + * @return boolean */ public function contains(PersistentCollection $coll, $element) { @@ -196,10 +196,10 @@ abstract class AbstractCollectionPersister /** * Check for existance of a key * - * @param \Doctrine\ORM\PersistentCollection $coll - * @param mixed $key + * @param \Doctrine\ORM\PersistentCollection $coll + * @param mixed $key * - * @return boolean + * @return boolean */ public function containsKey(PersistentCollection $coll, $key) { @@ -209,10 +209,10 @@ abstract class AbstractCollectionPersister /** * Remove an element * - * @param \Doctrine\ORM\PersistentCollection $coll - * @param object $element + * @param \Doctrine\ORM\PersistentCollection $coll + * @param object $element * - * @return mixed + * @return mixed */ public function removeElement(PersistentCollection $coll, $element) { @@ -222,9 +222,9 @@ abstract class AbstractCollectionPersister /** * Remove an element by key * - * @param \Doctrine\ORM\PersistentCollection $coll + * @param \Doctrine\ORM\PersistentCollection $coll * - * @param mixed $key + * @param mixed $key */ public function removeKey(PersistentCollection $coll, $key) { @@ -234,10 +234,10 @@ abstract class AbstractCollectionPersister /** * Get an element by key * - * @param \Doctrine\ORM\PersistentCollection $coll - * @param mixed $index + * @param \Doctrine\ORM\PersistentCollection $coll + * @param mixed $index * - * @return object + * @return object */ public function get(PersistentCollection $coll, $index) { From 122569eee0c34c57a4d6038d71679c34a11b3425 Mon Sep 17 00:00:00 2001 From: "Fabio B. Silva" Date: Mon, 22 Oct 2012 20:03:05 -0200 Subject: [PATCH 16/16] Fix CS --- lib/Doctrine/ORM/Persisters/AbstractCollectionPersister.php | 4 ++-- lib/Doctrine/ORM/Persisters/BasicEntityPersister.php | 1 - 2 files changed, 2 insertions(+), 3 deletions(-) diff --git a/lib/Doctrine/ORM/Persisters/AbstractCollectionPersister.php b/lib/Doctrine/ORM/Persisters/AbstractCollectionPersister.php index e6cade2eb..f6c004e5d 100644 --- a/lib/Doctrine/ORM/Persisters/AbstractCollectionPersister.php +++ b/lib/Doctrine/ORM/Persisters/AbstractCollectionPersister.php @@ -184,7 +184,7 @@ abstract class AbstractCollectionPersister * Check for existance of an element * * @param \Doctrine\ORM\PersistentCollection $coll - * @param \Doctrine\ORM\PersistentCollection + * @param object $element * * @return boolean */ @@ -237,7 +237,7 @@ abstract class AbstractCollectionPersister * @param \Doctrine\ORM\PersistentCollection $coll * @param mixed $index * - * @return object + * @return mixed */ public function get(PersistentCollection $coll, $index) { diff --git a/lib/Doctrine/ORM/Persisters/BasicEntityPersister.php b/lib/Doctrine/ORM/Persisters/BasicEntityPersister.php index 19988fb6c..bf03a744a 100644 --- a/lib/Doctrine/ORM/Persisters/BasicEntityPersister.php +++ b/lib/Doctrine/ORM/Persisters/BasicEntityPersister.php @@ -646,7 +646,6 @@ class BasicEntityPersister $result[$owningTable][$sourceColumn] = $value; } - } return $result;