diff --git a/lib/Doctrine/ORM/EntityManager.php b/lib/Doctrine/ORM/EntityManager.php index 0f35ee869..010c0b703 100644 --- a/lib/Doctrine/ORM/EntityManager.php +++ b/lib/Doctrine/ORM/EntityManager.php @@ -262,11 +262,13 @@ class EntityManager } /** - * @todo Implementation. + * Create a QueryBuilder instance + * + * @return QueryBuilder $qb */ public function createQueryBuilder() { - //... + return new QueryBuilder($this); } /** diff --git a/lib/Doctrine/ORM/Query.php b/lib/Doctrine/ORM/Query.php index a993e4b1d..6879193e5 100644 --- a/lib/Doctrine/ORM/Query.php +++ b/lib/Doctrine/ORM/Query.php @@ -291,7 +291,6 @@ final class Query extends AbstractQuery */ public function setDql($dqlQuery) { - $this->free(); if ($dqlQuery !== null) { $this->_dql = $dqlQuery; $this->_state = self::STATE_DIRTY; diff --git a/lib/Doctrine/ORM/Query/Expr/Having.php b/lib/Doctrine/ORM/Query/Expr/Having.php new file mode 100644 index 000000000..0eb0f727a --- /dev/null +++ b/lib/Doctrine/ORM/Query/Expr/Having.php @@ -0,0 +1,43 @@ +. + */ + +namespace Doctrine\ORM\Query\Expr; + +/** + * Expression class for building DQL Having parts + * + * @author Jonathan H. Wage + * @author Guilherme Blanco + * @license http://www.opensource.org/licenses/lgpl-license.php LGPL + * @link http://www.phpdoctrine.org + * @since 2.0 + * @version $Revision$ + */ +class Having extends Base +{ + protected $_preSeparator = ''; + protected $_postSeparator = ''; + protected $_allowedClasses = array( + 'Doctrine\ORM\Query\Expr\Comparison', + 'Doctrine\ORM\Query\Expr\Orx', + 'Doctrine\ORM\Query\Expr\Func' + ); +} \ No newline at end of file diff --git a/lib/Doctrine/ORM/QueryBuilder.php b/lib/Doctrine/ORM/QueryBuilder.php index 95f9bc1f7..07f4b113c 100644 --- a/lib/Doctrine/ORM/QueryBuilder.php +++ b/lib/Doctrine/ORM/QueryBuilder.php @@ -79,28 +79,14 @@ class QueryBuilder private $_dql; /** - * @var array $params Parameters of this query. + * @var Query $q The Query instance used for this QueryBuilder */ - private $_params = array(); - - /** - * @var integer The first result to return (the "offset"). - */ - private $_firstResult = null; - - /** - * @var integer The maximum number of results to return (the "limit"). - */ - private $_maxResults = null; + private $_q; public function __construct(EntityManager $entityManager) { $this->_em = $entityManager; - } - - public static function create(EntityManager $entityManager) - { - return new self($entityManager); + $this->_q = $entityManager->createQuery(); } public function getType() @@ -148,18 +134,9 @@ class QueryBuilder public function getQuery() { - $q = new Query($this->_em); - $q->setDql($this->getDql()); - $q->setParameters($this->getParameters()); - $q->setFirstResult($this->getFirstResult()); - $q->setMaxResults($this->getMaxResults()); + $this->_q->setDql($this->getDql()); - return $q; - } - - public function execute($params = array(), $hydrationMode = null) - { - return $this->getQuery()->execute($params, $hydrationMode); + return $this->_q; } /** @@ -170,7 +147,7 @@ class QueryBuilder */ public function setParameter($key, $value) { - $this->_params[$key] = $value; + $this->_q->setParameter($key, $value); return $this; } @@ -182,9 +159,7 @@ class QueryBuilder */ public function setParameters(array $params) { - foreach ($params as $key => $value) { - $this->setParameter($key, $value); - } + $this->_q->setParameters($params); return $this; } @@ -196,10 +171,7 @@ class QueryBuilder */ public function getParameters($params = array()) { - if ($params) { - return array_merge($this->_params, $params); - } - return $this->_params; + return $this->_q->getParameters($params); } /** @@ -210,7 +182,7 @@ class QueryBuilder */ public function getParameter($key) { - return isset($this->_params[$key]) ? $this->_params[$key] : null; + return $this->_q->getParameter($key); } /** @@ -400,52 +372,6 @@ class QueryBuilder return $this->add('orderBy', Expr::orderBy($sort, $order), true); } - /** - * Sets the position of the first result to retrieve (the "offset"). - * - * @param integer $firstResult The first result to return. - * @return Query This query object. - */ - public function setFirstResult($firstResult) - { - $this->_firstResult = $firstResult; - return $this; - } - - /** - * Gets the position of the first result the query object was set to retrieve (the "offset"). - * Returns NULL if {@link setFirstResult} was not applied to this query. - * - * @return integer The position of the first result. - */ - public function getFirstResult() - { - return $this->_firstResult; - } - - /** - * Sets the maximum number of results to retrieve (the "limit"). - * - * @param integer $maxResults - * @return Query This query object. - */ - public function setMaxResults($maxResults) - { - $this->_maxResults = $maxResults; - return $this; - } - - /** - * Gets the maximum number of results the query object was set to retrieve (the "limit"). - * Returns NULL if {@link setMaxResults} was not applied to this query. - * - * @return integer Maximum number of results. - */ - public function getMaxResults() - { - return $this->_maxResults; - } - /** * Get the DQL query string for DELETE queries * diff --git a/tests/Doctrine/Tests/ORM/Functional/AllTests.php b/tests/Doctrine/Tests/ORM/Functional/AllTests.php index 3c53a7ee6..43fe7034f 100644 --- a/tests/Doctrine/Tests/ORM/Functional/AllTests.php +++ b/tests/Doctrine/Tests/ORM/Functional/AllTests.php @@ -34,7 +34,6 @@ class AllTests $suite->addTestSuite('Doctrine\Tests\ORM\Functional\OneToOneSelfReferentialAssociationTest'); $suite->addTestSuite('Doctrine\Tests\ORM\Functional\OneToManySelfReferentialAssociationTest'); $suite->addTestSuite('Doctrine\Tests\ORM\Functional\ManyToManySelfReferentialAssociationTest'); - $suite->addTestSuite('Doctrine\Tests\ORM\Functional\QueryBuilderTest'); return $suite; } diff --git a/tests/Doctrine/Tests/ORM/Functional/QueryBuilderTest.php b/tests/Doctrine/Tests/ORM/Functional/QueryBuilderTest.php deleted file mode 100644 index 30dde2729..000000000 --- a/tests/Doctrine/Tests/ORM/Functional/QueryBuilderTest.php +++ /dev/null @@ -1,73 +0,0 @@ -useModelSet('cms'); - parent::setUp(); - } - - public function testExecute() - { - $qb = QueryBuilder::create($this->_em) - ->select('u') - ->from('Doctrine\Tests\Models\CMS\CmsUser', 'u'); - - $results = $qb->execute(); - $this->assertEquals('Doctrine\Common\Collections\Collection', get_class($results)); - } - - public function testSetMaxResultsAndSetFirstResultZero() - { - $qb = QueryBuilder::create($this->_em) - ->select('u') - ->from('Doctrine\Tests\Models\CMS\CmsUser', 'u') - ->setMaxResults(10) - ->setFirstResult(0); - - $this->assertEquals('SELECT c0_.id AS id0, c0_.status AS status1, c0_.username AS username2, c0_.name AS name3 FROM cms_users c0_ OFFSET 0 LIMIT 10', $qb->getQuery()->getSql()); - } - - public function testSetMaxResultsAndSetFirstResult() - { - $qb = QueryBuilder::create($this->_em) - ->select('u') - ->from('Doctrine\Tests\Models\CMS\CmsUser', 'u') - ->setMaxResults(10) - ->setFirstResult(10); - - $this->assertEquals('SELECT c0_.id AS id0, c0_.status AS status1, c0_.username AS username2, c0_.name AS name3 FROM cms_users c0_ OFFSET 10 LIMIT 10', $qb->getQuery()->getSql()); - } - - public function testRemoveSetMaxResultsAndSetFirstResult() - { - $qb = QueryBuilder::create($this->_em) - ->select('u') - ->from('Doctrine\Tests\Models\CMS\CmsUser', 'u') - ->setMaxResults(10) - ->setFirstResult(0) - ->setMaxResults(null) - ->setFirstResult(null); - - $this->assertEquals('SELECT c0_.id AS id0, c0_.status AS status1, c0_.username AS username2, c0_.name AS name3 FROM cms_users c0_', $qb->getQuery()->getSql()); - } - - public function testOnlyFirstResult() - { - $qb = QueryBuilder::create($this->_em) - ->select('u') - ->from('Doctrine\Tests\Models\CMS\CmsUser', 'u') - ->setMaxResults(10); - - $this->assertEquals('SELECT c0_.id AS id0, c0_.status AS status1, c0_.username AS username2, c0_.name AS name3 FROM cms_users c0_ LIMIT 10', $qb->getQuery()->getSql()); - } -} \ No newline at end of file diff --git a/tests/Doctrine/Tests/ORM/QueryBuilderTest.php b/tests/Doctrine/Tests/ORM/QueryBuilderTest.php index 3164ed444..642022a7f 100644 --- a/tests/Doctrine/Tests/ORM/QueryBuilderTest.php +++ b/tests/Doctrine/Tests/ORM/QueryBuilderTest.php @@ -58,7 +58,7 @@ class QueryBuilderTest extends \Doctrine\Tests\OrmTestCase public function testSelectSetsType() { - $qb = QueryBuilder::create($this->_em) + $qb = $this->_em->createQueryBuilder() ->delete('Doctrine\Tests\Models\CMS\CmsUser', 'u') ->select('u.id', 'u.username'); @@ -67,7 +67,7 @@ class QueryBuilderTest extends \Doctrine\Tests\OrmTestCase public function testEmptySelectSetsType() { - $qb = QueryBuilder::create($this->_em) + $qb = $this->_em->createQueryBuilder() ->delete('Doctrine\Tests\Models\CMS\CmsUser', 'u') ->select(); @@ -76,7 +76,7 @@ class QueryBuilderTest extends \Doctrine\Tests\OrmTestCase public function testDeleteSetsType() { - $qb = QueryBuilder::create($this->_em) + $qb = $this->_em->createQueryBuilder() ->from('Doctrine\Tests\Models\CMS\CmsUser', 'u') ->delete(); @@ -85,7 +85,7 @@ class QueryBuilderTest extends \Doctrine\Tests\OrmTestCase public function testUpdateSetsType() { - $qb = QueryBuilder::create($this->_em) + $qb = $this->_em->createQueryBuilder() ->from('Doctrine\Tests\Models\CMS\CmsUser', 'u') ->update(); @@ -94,7 +94,7 @@ class QueryBuilderTest extends \Doctrine\Tests\OrmTestCase public function testSimpleSelect() { - $qb = QueryBuilder::create($this->_em) + $qb = $this->_em->createQueryBuilder() ->from('Doctrine\Tests\Models\CMS\CmsUser', 'u') ->select('u.id', 'u.username'); @@ -103,7 +103,7 @@ class QueryBuilderTest extends \Doctrine\Tests\OrmTestCase public function testSimpleDelete() { - $qb = QueryBuilder::create($this->_em) + $qb = $this->_em->createQueryBuilder() ->delete('Doctrine\Tests\Models\CMS\CmsUser', 'u'); $this->assertValidQueryBuilder($qb, 'DELETE Doctrine\Tests\Models\CMS\CmsUser u'); @@ -111,7 +111,7 @@ class QueryBuilderTest extends \Doctrine\Tests\OrmTestCase public function testSimpleUpdate() { - $qb = QueryBuilder::create($this->_em) + $qb = $this->_em->createQueryBuilder() ->update('Doctrine\Tests\Models\CMS\CmsUser', 'u') ->set('u.username', ':username'); @@ -120,7 +120,7 @@ class QueryBuilderTest extends \Doctrine\Tests\OrmTestCase public function testInnerJoin() { - $qb = QueryBuilder::create($this->_em) + $qb = $this->_em->createQueryBuilder() ->select('u', 'a') ->from('Doctrine\Tests\Models\CMS\CmsUser', 'u') ->innerJoin('u', 'articles', 'a'); @@ -130,7 +130,7 @@ class QueryBuilderTest extends \Doctrine\Tests\OrmTestCase public function testLeftJoin() { - $qb = QueryBuilder::create($this->_em) + $qb = $this->_em->createQueryBuilder() ->select('u', 'a') ->from('Doctrine\Tests\Models\CMS\CmsUser', 'u') ->leftJoin('u', 'articles', 'a'); @@ -140,7 +140,7 @@ class QueryBuilderTest extends \Doctrine\Tests\OrmTestCase public function testWhere() { - $qb = QueryBuilder::create($this->_em) + $qb = $this->_em->createQueryBuilder() ->select('u') ->from('Doctrine\Tests\Models\CMS\CmsUser', 'u') ->where('u.id = :uid'); @@ -150,7 +150,7 @@ class QueryBuilderTest extends \Doctrine\Tests\OrmTestCase public function testAndWhere() { - $qb = QueryBuilder::create($this->_em) + $qb = $this->_em->createQueryBuilder() ->select('u') ->from('Doctrine\Tests\Models\CMS\CmsUser', 'u') ->where('u.id = :uid') @@ -161,7 +161,7 @@ class QueryBuilderTest extends \Doctrine\Tests\OrmTestCase public function testOrWhere() { - $qb = QueryBuilder::create($this->_em) + $qb = $this->_em->createQueryBuilder() ->select('u') ->from('Doctrine\Tests\Models\CMS\CmsUser', 'u') ->where('u.id = :uid') @@ -172,7 +172,7 @@ class QueryBuilderTest extends \Doctrine\Tests\OrmTestCase public function testAndWhereIn() { - $qb = QueryBuilder::create($this->_em) + $qb = $this->_em->createQueryBuilder() ->select('u') ->from('Doctrine\Tests\Models\CMS\CmsUser', 'u') ->where('u.id = :uid') @@ -183,7 +183,7 @@ class QueryBuilderTest extends \Doctrine\Tests\OrmTestCase public function testOrWhereIn() { - $qb = QueryBuilder::create($this->_em) + $qb = $this->_em->createQueryBuilder() ->select('u') ->from('Doctrine\Tests\Models\CMS\CmsUser', 'u') ->where('u.id = :uid') @@ -194,7 +194,7 @@ class QueryBuilderTest extends \Doctrine\Tests\OrmTestCase public function testAndWhereNotIn() { - $qb = QueryBuilder::create($this->_em) + $qb = $this->_em->createQueryBuilder() ->select('u') ->from('Doctrine\Tests\Models\CMS\CmsUser', 'u') ->where('u.id = :uid') @@ -205,7 +205,7 @@ class QueryBuilderTest extends \Doctrine\Tests\OrmTestCase public function testOrWhereNotIn() { - $qb = QueryBuilder::create($this->_em) + $qb = $this->_em->createQueryBuilder() ->select('u') ->from('Doctrine\Tests\Models\CMS\CmsUser', 'u') ->where('u.id = :uid') @@ -216,7 +216,7 @@ class QueryBuilderTest extends \Doctrine\Tests\OrmTestCase public function testGroupBy() { - $qb = QueryBuilder::create($this->_em) + $qb = $this->_em->createQueryBuilder() ->select('u') ->from('Doctrine\Tests\Models\CMS\CmsUser', 'u') ->groupBy('u.id') @@ -227,7 +227,7 @@ class QueryBuilderTest extends \Doctrine\Tests\OrmTestCase public function testHaving() { - $qb = QueryBuilder::create($this->_em) + $qb = $this->_em->createQueryBuilder() ->select('u') ->from('Doctrine\Tests\Models\CMS\CmsUser', 'u') ->groupBy('u.id') @@ -238,7 +238,7 @@ class QueryBuilderTest extends \Doctrine\Tests\OrmTestCase public function testAndHaving() { - $qb = QueryBuilder::create($this->_em) + $qb = $this->_em->createQueryBuilder() ->select('u') ->from('Doctrine\Tests\Models\CMS\CmsUser', 'u') ->groupBy('u.id') @@ -250,7 +250,7 @@ class QueryBuilderTest extends \Doctrine\Tests\OrmTestCase public function testOrHaving() { - $qb = QueryBuilder::create($this->_em) + $qb = $this->_em->createQueryBuilder() ->select('u') ->from('Doctrine\Tests\Models\CMS\CmsUser', 'u') ->groupBy('u.id') @@ -263,7 +263,7 @@ class QueryBuilderTest extends \Doctrine\Tests\OrmTestCase public function testOrderBy() { - $qb = QueryBuilder::create($this->_em) + $qb = $this->_em->createQueryBuilder() ->select('u') ->from('Doctrine\Tests\Models\CMS\CmsUser', 'u') ->orderBy('u.username', 'ASC'); @@ -273,7 +273,7 @@ class QueryBuilderTest extends \Doctrine\Tests\OrmTestCase public function testAddOrderBy() { - $qb = QueryBuilder::create($this->_em) + $qb = $this->_em->createQueryBuilder() ->select('u') ->from('Doctrine\Tests\Models\CMS\CmsUser', 'u') ->orderBy('u.username', 'ASC') @@ -284,45 +284,41 @@ class QueryBuilderTest extends \Doctrine\Tests\OrmTestCase public function testGetQuery() { - $qb = QueryBuilder::create($this->_em) + $qb = $this->_em->createQueryBuilder() ->select('u') ->from('Doctrine\Tests\Models\CMS\CmsUser', 'u'); $q = $qb->getQuery(); - $this->assertEquals(get_class($q), 'Doctrine\ORM\Query'); + $this->assertEquals('Doctrine\ORM\Query', get_class($q)); } public function testSetParameter() { - $qb = QueryBuilder::create($this->_em) + $qb = $this->_em->createQueryBuilder() ->select('u') ->from('Doctrine\Tests\Models\CMS\CmsUser', 'u') ->where('u.id = :id') ->setParameter('id', 1); - $q = $qb->getQuery(); - - $this->assertEquals($q->getParameters(), array('id' => 1)); + $this->assertEquals(array('id' => 1), $qb->getParameters()); } public function testSetParameters() { - $qb = QueryBuilder::create($this->_em) + $qb = $this->_em->createQueryBuilder() ->select('u') ->from('Doctrine\Tests\Models\CMS\CmsUser', 'u') ->where('u.username = :username OR u.username = :username2'); $qb->setParameters(array('username' => 'jwage', 'username2' => 'jonwage')); - $q = $qb->getQuery(); - - $this->assertEquals($q->getParameters(), array('username' => 'jwage', 'username2' => 'jonwage')); + $this->assertEquals(array('username' => 'jwage', 'username2' => 'jonwage'), $qb->getQuery()->getParameters()); } public function testGetParameters() { - $qb = QueryBuilder::create($this->_em) + $qb = $this->_em->createQueryBuilder() ->select('u') ->from('Doctrine\Tests\Models\CMS\CmsUser', 'u') ->where('u.id = :id'); @@ -333,7 +329,7 @@ class QueryBuilderTest extends \Doctrine\Tests\OrmTestCase public function testGetParameter() { - $qb = QueryBuilder::create($this->_em) + $qb = $this->_em->createQueryBuilder() ->select('u') ->from('Doctrine\Tests\Models\CMS\CmsUser', 'u') ->where('u.id = :id'); @@ -344,7 +340,7 @@ class QueryBuilderTest extends \Doctrine\Tests\OrmTestCase public function testMultipleWhere() { - $qb = QueryBuilder::create($this->_em) + $qb = $this->_em->createQueryBuilder() ->select('u') ->from('Doctrine\Tests\Models\CMS\CmsUser', 'u') ->where('u.id = :uid', 'u.id = :uid2'); @@ -354,7 +350,7 @@ class QueryBuilderTest extends \Doctrine\Tests\OrmTestCase public function testMultipleAndWhere() { - $qb = QueryBuilder::create($this->_em) + $qb = $this->_em->createQueryBuilder() ->select('u') ->from('Doctrine\Tests\Models\CMS\CmsUser', 'u') ->andWhere('u.id = :uid', 'u.id = :uid2'); @@ -364,7 +360,7 @@ class QueryBuilderTest extends \Doctrine\Tests\OrmTestCase public function testMultipleOrWhere() { - $qb = QueryBuilder::create($this->_em) + $qb = $this->_em->createQueryBuilder() ->select('u') ->from('Doctrine\Tests\Models\CMS\CmsUser', 'u') ->orWhere('u.id = :uid', Expr::eq('u.id', ':uid2')); @@ -378,7 +374,7 @@ class QueryBuilderTest extends \Doctrine\Tests\OrmTestCase $orExpr->add(Expr::eq('u.id', ':uid3')); $orExpr->add(Expr::in('u.id', array(1))); - $qb = QueryBuilder::create($this->_em) + $qb = $this->_em->createQueryBuilder() ->select('u') ->from('Doctrine\Tests\Models\CMS\CmsUser', 'u') ->where($orExpr); @@ -388,19 +384,19 @@ class QueryBuilderTest extends \Doctrine\Tests\OrmTestCase public function testGetEntityManager() { - $qb = QueryBuilder::create($this->_em); + $qb = $this->_em->createQueryBuilder(); $this->assertEquals($this->_em, $qb->getEntityManager()); } public function testInitialStateIsClean() { - $qb = QueryBuilder::create($this->_em); + $qb = $this->_em->createQueryBuilder(); $this->assertEquals(QueryBuilder::STATE_CLEAN, $qb->getState()); } public function testAlteringQueryChangesStateToDirty() { - $qb = QueryBuilder::create($this->_em) + $qb = $this->_em->createQueryBuilder() ->select('u') ->from('Doctrine\Tests\Models\CMS\CmsUser', 'u');