From 968ebb80c29399d1be2f84704a71140ade41ad6f Mon Sep 17 00:00:00 2001 From: jwage Date: Sun, 4 Oct 2009 19:35:47 +0000 Subject: [PATCH] [2.0] Bug fixes --- lib/Doctrine/Common/IsolatedClassLoader.php | 3 +- .../ORM/Mapping/Driver/YamlDriver.php | 58 +++++----- lib/Doctrine/ORM/Query/Expr/From.php | 10 ++ lib/Doctrine/ORM/QueryBuilder.php | 100 ++++++++++++++---- lib/Doctrine/ORM/Tools/SchemaTool.php | 4 +- 5 files changed, 125 insertions(+), 50 deletions(-) diff --git a/lib/Doctrine/Common/IsolatedClassLoader.php b/lib/Doctrine/Common/IsolatedClassLoader.php index 969be465e..975ee5812 100644 --- a/lib/Doctrine/Common/IsolatedClassLoader.php +++ b/lib/Doctrine/Common/IsolatedClassLoader.php @@ -100,7 +100,7 @@ class IsolatedClassLoader return false; } - if (strpos($className, $this->_namespace) !== 0) { + if (strpos($className, $this->_namespace.$this->_namespaceSeparator) !== 0) { return false; } @@ -113,5 +113,4 @@ class IsolatedClassLoader return true; } - } \ No newline at end of file diff --git a/lib/Doctrine/ORM/Mapping/Driver/YamlDriver.php b/lib/Doctrine/ORM/Mapping/Driver/YamlDriver.php index c0c0e964e..e54c591e8 100644 --- a/lib/Doctrine/ORM/Mapping/Driver/YamlDriver.php +++ b/lib/Doctrine/ORM/Mapping/Driver/YamlDriver.php @@ -55,7 +55,7 @@ class YamlDriver extends AbstractFileDriver if ($element['type'] == 'entity') { $metadata->setCustomRepositoryClass( - isset($element['repositoryClass']) ? $xmlRoot['repositoryClass'] : null + isset($element['repositoryClass']) ? $element['repositoryClass'] : null ); } else if ($element['type'] == 'mappedSuperclass') { $metadata->isMappedSuperclass = true; @@ -113,6 +113,28 @@ class YamlDriver extends AbstractFileDriver } } + if (isset($element['id'])) { + // Evaluate identifier settings + foreach ($element['id'] as $name => $idElement) { + $mapping = array( + 'id' => true, + 'fieldName' => $name, + 'type' => $idElement['type'] + ); + + if (isset($idElement['column'])) { + $mapping['columnName'] = $idElement['column']; + } + + $metadata->mapField($mapping); + + if (isset($idElement['generator'])) { + $metadata->setIdGeneratorType(constant('Doctrine\ORM\Mapping\ClassMetadata::GENERATOR_TYPE_' + . strtoupper($idElement['generator']['strategy']))); + } + } + } + // Evaluate fields if (isset($element['fields'])) { foreach ($element['fields'] as $name => $fieldMapping) { @@ -138,13 +160,17 @@ class YamlDriver extends AbstractFileDriver } if (isset($fieldMapping['unique'])) { - $mapping['unique'] = (bool)$fieldMapping['unique']; + $mapping['unique'] = (bool)$fieldMapping['unique']; } if (isset($fieldMapping['options'])) { $mapping['options'] = $fieldMapping['options']; } - + + if (isset($fieldMapping['notnull'])) { + $mapping['notnull'] = $fieldMapping['notnull']; + } + if (isset($fieldMapping['version']) && $fieldMapping['version']) { $metadata->setVersionMapping($mapping); } @@ -153,28 +179,6 @@ class YamlDriver extends AbstractFileDriver } } - if (isset($element['id'])) { - // Evaluate identifier settings - foreach ($element['id'] as $name => $idElement) { - $mapping = array( - 'id' => true, - 'fieldName' => $name, - 'type' => $idElement['type'] - ); - - if (isset($idElement['column'])) { - $mapping['columnName'] = $idElement['column']; - } - - $metadata->mapField($mapping); - - if (isset($idElement['generator'])) { - $metadata->setIdGeneratorType(constant('Doctrine\ORM\Mapping\ClassMetadata::GENERATOR_TYPE_' - . strtoupper($idElement['generator']['strategy']))); - } - } - } - // Evaluate oneToOne relationships if (isset($element['oneToOne'])) { foreach ($element['oneToOne'] as $name => $oneToOneElement) { @@ -338,6 +342,10 @@ class YamlDriver extends AbstractFileDriver 'name' => $joinColumnElement['name'], 'referencedColumnName' => $joinColumnElement['referencedColumnName'] ); + + if (isset($joinColumnElement['fieldName'])) { + $joinColumn['fieldName'] = (string) $joinColumnElement['fieldName']; + } if (isset($joinColumnElement['unique'])) { $joinColumn['unique'] = (bool) $joinColumnElement['unique']; diff --git a/lib/Doctrine/ORM/Query/Expr/From.php b/lib/Doctrine/ORM/Query/Expr/From.php index 5f2620108..f3f61e9c6 100644 --- a/lib/Doctrine/ORM/Query/Expr/From.php +++ b/lib/Doctrine/ORM/Query/Expr/From.php @@ -43,6 +43,16 @@ class From $this->_alias = $alias; } + public function getFrom() + { + return $this->_from; + } + + public function getAlias() + { + return $this->_alias; + } + public function __toString() { return $this->_from . ($this->_alias ? ' ' . $this->_alias : ''); diff --git a/lib/Doctrine/ORM/QueryBuilder.php b/lib/Doctrine/ORM/QueryBuilder.php index 5a2ffaf8b..1a2989943 100644 --- a/lib/Doctrine/ORM/QueryBuilder.php +++ b/lib/Doctrine/ORM/QueryBuilder.php @@ -55,7 +55,7 @@ class QueryBuilder */ private $_dqlParts = array( 'select' => array(), - 'from' => null, + 'from' => array(), 'join' => array(), 'set' => array(), 'where' => null, @@ -192,10 +192,6 @@ class QueryBuilder return $this->_dql; } - if ( ! $this->_dqlParts['select'] || ! $this->_dqlParts['from']) { - throw DoctrineException::incompleteQueryBuilder(); - } - $dql = ''; switch ($this->_type) { @@ -238,6 +234,24 @@ class QueryBuilder return $this->_q; } + /** + * Get the root alias for the query. This is the first entity alias involved + * in the construction of the query + * + * [php] + * $qb = $em->createQueryBuilder() + * ->select('u') + * ->from('User', 'u'); + * + * echo $qb->getRootAlias(); // u + * + * @return string $rootAlias + */ + public function getRootAlias() + { + return $this->_dqlParts['from'][0]->getAlias(); + } + /** * Sets a query parameter. * @@ -373,7 +387,7 @@ class QueryBuilder } /** - * Add to the SELECT statement + * Set the SELECT statement * * [php] * $qb = $em->createQueryBuilder() @@ -393,6 +407,31 @@ class QueryBuilder return $this; } + return $this->add('select', new Expr\Select($selects), false); + } + + /** + * Add to the SELECT statement + * + * [php] + * $qb = $em->createQueryBuilder() + * ->select('u') + * ->addSelect('p') + * ->from('User', 'u') + * ->leftJoin('u.Phonenumbers', 'p'); + * + * @param mixed $select String SELECT statement or SELECT Expr instance + * @return QueryBuilder $qb + */ + public function addSelect($select = null) + { + $this->_type = self::SELECT; + $selects = func_get_args(); + + if (empty($selects)) { + return $this; + } + return $this->add('select', new Expr\Select($selects), true); } @@ -458,7 +497,7 @@ class QueryBuilder */ public function from($from, $alias = null) { - return $this->add('from', new Expr\From($from, $alias)); + return $this->add('from', new Expr\From($from, $alias), true); } /** @@ -571,7 +610,7 @@ class QueryBuilder */ public function andWhere($where) { - $where = $this->_getDqlQueryPart('where'); + $where = $this->getDqlPart('where'); $args = func_get_args(); if ($where instanceof Expr\Andx) { @@ -600,7 +639,7 @@ class QueryBuilder */ public function orWhere($where) { - $where = $this->_getDqlQueryPart('where'); + $where = $this->getDqlPart('where'); $args = func_get_args(); if ($where instanceof Expr\Orx) { @@ -672,7 +711,7 @@ class QueryBuilder */ public function andHaving($having) { - $having = $this->_getDqlQueryPart('having'); + $having = $this->getDqlPart('having'); $args = func_get_args(); if ($having instanceof Expr\Andx) { @@ -693,7 +732,7 @@ class QueryBuilder */ public function orHaving($having) { - $having = $this->_getDqlQueryPart('having'); + $having = $this->getDqlPart('having'); $args = func_get_args(); if ($having instanceof Expr\Orx) { @@ -730,10 +769,31 @@ class QueryBuilder return $this->add('orderBy', new Expr\OrderBy($sort, $order), true); } + /** + * Get a DQL part or parts by the part name + * + * @param string $queryPartName + * @return mixed $queryPart + */ + public function getDqlPart($queryPartName) + { + return $this->_dqlParts[$queryPartName]; + } + + /** + * Get the full DQL parts array + * + * @return array $dqlParts + */ + public function getDqlParts() + { + return $this->_dqlParts; + } + private function _getDqlForDelete() { return 'DELETE' - . $this->_getReducedDqlQueryPart('from', array('pre' => ' ')) + . $this->_getReducedDqlQueryPart('from', array('pre' => ' ', 'separator' => ', ')) . $this->_getReducedDqlQueryPart('where', array('pre' => ' WHERE ')) . $this->_getReducedDqlQueryPart('orderBy', array('pre' => ' ORDER BY ', 'separator' => ', ')); } @@ -741,7 +801,7 @@ class QueryBuilder private function _getDqlForUpdate() { return 'UPDATE' - . $this->_getReducedDqlQueryPart('from', array('pre' => ' ')) + . $this->_getReducedDqlQueryPart('from', array('pre' => ' ', 'separator' => ', ')) . $this->_getReducedDqlQueryPart('set', array('pre' => ' SET ', 'separator' => ', ')) . $this->_getReducedDqlQueryPart('where', array('pre' => ' WHERE ')) . $this->_getReducedDqlQueryPart('orderBy', array('pre' => ' ORDER BY ', 'separator' => ', ')); @@ -751,7 +811,7 @@ class QueryBuilder { return 'SELECT' . $this->_getReducedDqlQueryPart('select', array('pre' => ' ', 'separator' => ', ')) - . $this->_getReducedDqlQueryPart('from', array('pre' => ' FROM ')) + . $this->_getReducedDqlQueryPart('from', array('pre' => ' FROM ', 'separator' => ', ')) . $this->_getReducedDqlQueryPart('join', array('pre' => ' ', 'separator' => ' ')) . $this->_getReducedDqlQueryPart('where', array('pre' => ' WHERE ')) . $this->_getReducedDqlQueryPart('groupBy', array('pre' => ' GROUP BY ', 'separator' => ', ')) @@ -761,7 +821,7 @@ class QueryBuilder private function _getReducedDqlQueryPart($queryPartName, $options = array()) { - $queryPart = $this->_getDqlQueryPart($queryPartName); + $queryPart = $this->getDqlPart($queryPartName); if (empty($queryPart)) { return (isset($options['empty']) ? $options['empty'] : ''); @@ -772,13 +832,13 @@ class QueryBuilder . (isset($options['post']) ? $options['post'] : ''); } - private function _getDqlQueryPart($queryPartName) - { - return $this->_dqlParts[$queryPartName]; - } - public function __toString() { return $this->getDql(); } + + public function __clone() + { + $this->_q = clone $this->_q; + } } \ No newline at end of file diff --git a/lib/Doctrine/ORM/Tools/SchemaTool.php b/lib/Doctrine/ORM/Tools/SchemaTool.php index f27700cbc..69f907a6a 100644 --- a/lib/Doctrine/ORM/Tools/SchemaTool.php +++ b/lib/Doctrine/ORM/Tools/SchemaTool.php @@ -510,9 +510,7 @@ class SchemaTool // Column exists, check for changes $columnInfo = $column; $columnChanged = false; - - echo $column['name'] . ' '; - + // 1. check for nullability change $columnInfo['notnull'] = ( ! isset($columnInfo['notnull'])) ? false : $columnInfo['notnull'];