From f4c5c4ba01fb789f81590b8fa7c2c645f6e3dbfa Mon Sep 17 00:00:00 2001 From: "Fabio B. Silva" Date: Fri, 23 Sep 2011 18:10:58 -0300 Subject: [PATCH 1/4] branch for DDC-1335 --- lib/Doctrine/ORM/Query/Expr/From.php | 39 ++++++++++-- lib/Doctrine/ORM/QueryBuilder.php | 3 +- .../ORM/Functional/Ticket/DDC1335Test.php | 60 +++++++++++++++++++ 3 files changed, 95 insertions(+), 7 deletions(-) create mode 100644 tests/Doctrine/Tests/ORM/Functional/Ticket/DDC1335Test.php diff --git a/lib/Doctrine/ORM/Query/Expr/From.php b/lib/Doctrine/ORM/Query/Expr/From.php index 6646e1de2..a4ad22c3f 100644 --- a/lib/Doctrine/ORM/Query/Expr/From.php +++ b/lib/Doctrine/ORM/Query/Expr/From.php @@ -34,27 +34,54 @@ namespace Doctrine\ORM\Query\Expr; */ class From { + /** + * @var string + */ private $_from; + + /** + * @var string + */ private $_alias; - - public function __construct($from, $alias) + + /** + * @var string + */ + private $_indexBy; + + /** + * @param string $from The class name. + * @param string $alias The alias of the class. + * @param string $indexBy The index for the from. + */ + public function __construct($from, $alias, $indexBy = null) { - $this->_from = $from; - $this->_alias = $alias; + $this->_from = $from; + $this->_alias = $alias; + $this->_indexBy = $indexBy; } - + + /** + * @return string + */ public function getFrom() { return $this->_from; } + /** + * @return string + */ public function getAlias() { return $this->_alias; } + /** + * @return string + */ public function __toString() { - return $this->_from . ' ' . $this->_alias; + return $this->_from . ' ' . $this->_alias . ($this->_indexBy ? ' INDEX BY ' . $this->_indexBy : ''); } } \ No newline at end of file diff --git a/lib/Doctrine/ORM/QueryBuilder.php b/lib/Doctrine/ORM/QueryBuilder.php index 9b2829408..f5914fd57 100644 --- a/lib/Doctrine/ORM/QueryBuilder.php +++ b/lib/Doctrine/ORM/QueryBuilder.php @@ -595,9 +595,10 @@ class QueryBuilder * * @param string $from The class name. * @param string $alias The alias of the class. + * @param string $indexBy The index for the from. * @return QueryBuilder This QueryBuilder instance. */ - public function from($from, $alias) + public function from($from, $alias,$indexBy = null) { return $this->add('from', new Expr\From($from, $alias), true); } diff --git a/tests/Doctrine/Tests/ORM/Functional/Ticket/DDC1335Test.php b/tests/Doctrine/Tests/ORM/Functional/Ticket/DDC1335Test.php new file mode 100644 index 000000000..3bd478a45 --- /dev/null +++ b/tests/Doctrine/Tests/ORM/Functional/Ticket/DDC1335Test.php @@ -0,0 +1,60 @@ +_em->getClassMetadata(__NAMESPACE__ . '\DDC1135User') + ); + + try { + $this->_schemaTool->dropSchema($classes); + $this->_schemaTool->createSchema($classes); + } catch(\Exception $e) { + + } + } + + + public function testTicket() + { + $this->markTestIncomplete(); + + $builder = $this->_em->createQueryBuilder(); + $builder->select('u')->from('Doctrine\Tests\ORM\Functional\Ticket\DDC1135User', 'u', 'u.id'); + + + $sql = $builder->getQuery()->getSQL(); + + $this->assertEquals('SELECT d0_.id AS id0, d0_.name AS name1 FROM DDC1135User INDEX BY d0_.id', $sql); + } + +} + +/** + * @Entity + */ +class DDC1135User +{ + + /** + * @Id @Column(type="integer") + * @GeneratedValue + */ + protected $id; + + /** + * @Column(type="string", length=255) + */ + protected $name; + +} \ No newline at end of file From e94b902a9bb6c5a34c1847958de4adc1e6a6da33 Mon Sep 17 00:00:00 2001 From: "Fabio B. Silva" Date: Sat, 24 Sep 2011 16:10:10 -0300 Subject: [PATCH 2/4] tests for DDC-1335 --- lib/Doctrine/ORM/QueryBuilder.php | 4 +- .../ORM/Functional/Ticket/DDC1335Test.php | 184 ++++++++++++++++-- 2 files changed, 175 insertions(+), 13 deletions(-) diff --git a/lib/Doctrine/ORM/QueryBuilder.php b/lib/Doctrine/ORM/QueryBuilder.php index f5914fd57..938a429ae 100644 --- a/lib/Doctrine/ORM/QueryBuilder.php +++ b/lib/Doctrine/ORM/QueryBuilder.php @@ -598,9 +598,9 @@ class QueryBuilder * @param string $indexBy The index for the from. * @return QueryBuilder This QueryBuilder instance. */ - public function from($from, $alias,$indexBy = null) + public function from($from, $alias, $indexBy = null) { - return $this->add('from', new Expr\From($from, $alias), true); + return $this->add('from', new Expr\From($from, $alias, $indexBy), true); } /** diff --git a/tests/Doctrine/Tests/ORM/Functional/Ticket/DDC1335Test.php b/tests/Doctrine/Tests/ORM/Functional/Ticket/DDC1335Test.php index 3bd478a45..3abd93b17 100644 --- a/tests/Doctrine/Tests/ORM/Functional/Ticket/DDC1335Test.php +++ b/tests/Doctrine/Tests/ORM/Functional/Ticket/DDC1335Test.php @@ -13,29 +13,142 @@ class DDC1135Test extends \Doctrine\Tests\OrmFunctionalTestCase parent::setUp(); $classes = array( - $this->_em->getClassMetadata(__NAMESPACE__ . '\DDC1135User') + $this->_em->getClassMetadata(__NAMESPACE__ . '\DDC1135User'), + $this->_em->getClassMetadata(__NAMESPACE__ . '\DDC1135Phone'), ); try { $this->_schemaTool->dropSchema($classes); $this->_schemaTool->createSchema($classes); } catch(\Exception $e) { - + try { + $this->_schemaTool->createSchema($classes); + } catch (Exception $exc) { + $this->fail($exc->getMessage()); + } } + $this->loadFixture(); } + + public function testDql() + { + $dql = 'SELECT u FROM ' . __NAMESPACE__ . '\DDC1135User u INDEX BY u.id'; + $query = $this->_em->createQuery($dql); + $result = $query->getResult(); + + $this->assertEquals(sizeof($result), 3); + $this->assertArrayHasKey(1, $result); + $this->assertArrayHasKey(2, $result); + $this->assertArrayHasKey(3, $result); + + + $dql = 'SELECT u, p FROM '.__NAMESPACE__ . '\DDC1135User u INDEX BY u.email INNER JOIN u.phones p INDEX BY p.id'; + $query = $this->_em->createQuery($dql); + $result = $query->getResult(); + + $this->assertEquals(sizeof($result), 3); + $this->assertArrayHasKey('foo@foo.com', $result); + $this->assertArrayHasKey('bar@bar.com', $result); + $this->assertArrayHasKey('foobar@foobar.com', $result); + + $this->assertEquals(sizeof($result['foo@foo.com']->phones), 3); + $this->assertEquals(sizeof($result['bar@bar.com']->phones), 3); + $this->assertEquals(sizeof($result['foobar@foobar.com']->phones), 3); + + $this->assertArrayHasKey(1, $result['foo@foo.com']->phones->toArray()); + $this->assertArrayHasKey(2, $result['foo@foo.com']->phones->toArray()); + $this->assertArrayHasKey(3, $result['foo@foo.com']->phones->toArray()); + + $this->assertArrayHasKey(4, $result['bar@bar.com']->phones->toArray()); + $this->assertArrayHasKey(5, $result['bar@bar.com']->phones->toArray()); + $this->assertArrayHasKey(6, $result['bar@bar.com']->phones->toArray()); + + $this->assertArrayHasKey(7, $result['foobar@foobar.com']->phones->toArray()); + $this->assertArrayHasKey(8, $result['foobar@foobar.com']->phones->toArray()); + $this->assertArrayHasKey(9, $result['foobar@foobar.com']->phones->toArray()); + } public function testTicket() { - $this->markTestIncomplete(); - $builder = $this->_em->createQueryBuilder(); - $builder->select('u')->from('Doctrine\Tests\ORM\Functional\Ticket\DDC1135User', 'u', 'u.id'); + $builder->select('u')->from(__NAMESPACE__ . '\DDC1135User', 'u', 'u.id'); + $dql = $builder->getQuery()->getDQL(); + $result = $builder->getQuery()->getResult(); + + $this->assertEquals(sizeof($result), 3); + $this->assertArrayHasKey(1, $result); + $this->assertArrayHasKey(2, $result); + $this->assertArrayHasKey(3, $result); + $this->assertEquals('SELECT u FROM Doctrine\Tests\ORM\Functional\Ticket\DDC1135User u INDEX BY u.id', $dql); + } + + public function testIndexByUnique() + { + $builder = $this->_em->createQueryBuilder(); + $builder->select('u')->from(__NAMESPACE__ . '\DDC1135User', 'u', 'u.email'); - $sql = $builder->getQuery()->getSQL(); + $dql = $builder->getQuery()->getDQL(); + $result = $builder->getQuery()->getResult(); + + $this->assertEquals(sizeof($result), 3); + $this->assertArrayHasKey('foo@foo.com', $result); + $this->assertArrayHasKey('bar@bar.com', $result); + $this->assertArrayHasKey('foobar@foobar.com', $result); + $this->assertEquals('SELECT u FROM Doctrine\Tests\ORM\Functional\Ticket\DDC1135User u INDEX BY u.email', $dql); + } + + public function testIndexWithJoin() + { + $builder = $this->_em->createQueryBuilder(); + $builder->select('u','p') + ->from(__NAMESPACE__ . '\DDC1135User', 'u', 'u.email') + ->join('u.phones', 'p', null, null, 'p.id'); - $this->assertEquals('SELECT d0_.id AS id0, d0_.name AS name1 FROM DDC1135User INDEX BY d0_.id', $sql); + + $dql = $builder->getQuery()->getDQL(); + $result = $builder->getQuery()->getResult(); + + $this->assertEquals(sizeof($result), 3); + $this->assertArrayHasKey('foo@foo.com', $result); + $this->assertArrayHasKey('bar@bar.com', $result); + $this->assertArrayHasKey('foobar@foobar.com', $result); + + $this->assertEquals(sizeof($result['foo@foo.com']->phones), 3); + $this->assertEquals(sizeof($result['bar@bar.com']->phones), 3); + $this->assertEquals(sizeof($result['foobar@foobar.com']->phones), 3); + + $this->assertArrayHasKey(1, $result['foo@foo.com']->phones->toArray()); + $this->assertArrayHasKey(2, $result['foo@foo.com']->phones->toArray()); + $this->assertArrayHasKey(3, $result['foo@foo.com']->phones->toArray()); + + $this->assertArrayHasKey(4, $result['bar@bar.com']->phones->toArray()); + $this->assertArrayHasKey(5, $result['bar@bar.com']->phones->toArray()); + $this->assertArrayHasKey(6, $result['bar@bar.com']->phones->toArray()); + + $this->assertArrayHasKey(7, $result['foobar@foobar.com']->phones->toArray()); + $this->assertArrayHasKey(8, $result['foobar@foobar.com']->phones->toArray()); + $this->assertArrayHasKey(9, $result['foobar@foobar.com']->phones->toArray()); + + $this->assertEquals('SELECT u, p FROM '.__NAMESPACE__ . '\DDC1135User u INDEX BY u.email INNER JOIN u.phones p INDEX BY p.id', $dql); + } + + private function loadFixture() + { + $p1 = array('11 xxxx-xxxx','11 yyyy-yyyy','11 zzzz-zzzz'); + $p2 = array('22 xxxx-xxxx','22 yyyy-yyyy','22 zzzz-zzzz'); + $p3 = array('33 xxxx-xxxx','33 yyyy-yyyy','33 zzzz-zzzz'); + + $u1 = new DDC1135User("foo@foo.com", "Foo",$p1); + $u2 = new DDC1135User("bar@bar.com", "Bar",$p2); + $u3 = new DDC1135User("foobar@foobar.com", "Foo Bar",$p3); + + $this->_em->persist($u1); + $this->_em->persist($u2); + $this->_em->persist($u3); + $this->_em->flush(); + $this->_em->clear(); } } @@ -45,16 +158,65 @@ class DDC1135Test extends \Doctrine\Tests\OrmFunctionalTestCase */ class DDC1135User { - /** * @Id @Column(type="integer") * @GeneratedValue */ - protected $id; + public $id; /** - * @Column(type="string", length=255) + * @Column(type="string", unique=true) */ - protected $name; + public $email; + + /** + * @Column(type="string") + */ + public $name; + + /** + * @OneToMany(targetEntity="DDC1135Phone", mappedBy="user", cascade={"persist", "remove"}) + */ + public $phones; + + public function __construct($email, $name,array $numbers) + { + $this->name = $name; + $this->email = $email; + $this->phones = new \Doctrine\Common\Collections\ArrayCollection(); + + foreach ($numbers as $number) { + $this->phones->add(new DDC1135Phone($this,$number)); + } + } +} +/** + * @Entity + */ +class DDC1135Phone +{ + /** + * @Id + * @Column(name="id", type="integer") + * @GeneratedValue(strategy="AUTO") + */ + public $id; + + /** + * @Column(name="number", type="string", nullable = false) + */ + public $number; + + /** + * @ManyToOne(targetEntity="DDC1135User", inversedBy="phones") + * @JoinColumn(name="user_id", referencedColumnName="id", nullable = false) + */ + public $user; + + public function __construct($user, $number) + { + $this->user = $user; + $this->number = $number; + } } \ No newline at end of file From 728724bed5349aa40952c81d3580636036592e23 Mon Sep 17 00:00:00 2001 From: "Fabio B. Silva" Date: Sat, 24 Sep 2011 16:16:25 -0300 Subject: [PATCH 3/4] fix DDC-1135 --- tests/Doctrine/Tests/ORM/Functional/Ticket/DDC1335Test.php | 3 +++ 1 file changed, 3 insertions(+) diff --git a/tests/Doctrine/Tests/ORM/Functional/Ticket/DDC1335Test.php b/tests/Doctrine/Tests/ORM/Functional/Ticket/DDC1335Test.php index 3abd93b17..ea7f53842 100644 --- a/tests/Doctrine/Tests/ORM/Functional/Ticket/DDC1335Test.php +++ b/tests/Doctrine/Tests/ORM/Functional/Ticket/DDC1335Test.php @@ -6,6 +6,9 @@ use DateTime; require_once __DIR__ . '/../../../TestInit.php'; +/** + * @group DDC-1135 + */ class DDC1135Test extends \Doctrine\Tests\OrmFunctionalTestCase { protected function setUp() From 5fe996baf9b6ebde5e9f29fea826cb74fce6c201 Mon Sep 17 00:00:00 2001 From: "Fabio B. Silva" Date: Sun, 25 Sep 2011 12:20:48 -0300 Subject: [PATCH 4/4] change tests for DDC-1135 --- lib/Doctrine/ORM/Query/Expr/From.php | 3 ++- .../ORM/Functional/Ticket/DDC1335Test.php | 27 ++++++------------- 2 files changed, 10 insertions(+), 20 deletions(-) diff --git a/lib/Doctrine/ORM/Query/Expr/From.php b/lib/Doctrine/ORM/Query/Expr/From.php index a4ad22c3f..4c0c8175a 100644 --- a/lib/Doctrine/ORM/Query/Expr/From.php +++ b/lib/Doctrine/ORM/Query/Expr/From.php @@ -82,6 +82,7 @@ class From */ public function __toString() { - return $this->_from . ' ' . $this->_alias . ($this->_indexBy ? ' INDEX BY ' . $this->_indexBy : ''); + return $this->_from . ' ' . $this->_alias . + ($this->_indexBy ? ' INDEX BY ' . $this->_indexBy : ''); } } \ No newline at end of file diff --git a/tests/Doctrine/Tests/ORM/Functional/Ticket/DDC1335Test.php b/tests/Doctrine/Tests/ORM/Functional/Ticket/DDC1335Test.php index ea7f53842..e12ee9ab7 100644 --- a/tests/Doctrine/Tests/ORM/Functional/Ticket/DDC1335Test.php +++ b/tests/Doctrine/Tests/ORM/Functional/Ticket/DDC1335Test.php @@ -14,23 +14,14 @@ class DDC1135Test extends \Doctrine\Tests\OrmFunctionalTestCase protected function setUp() { parent::setUp(); - - $classes = array( - $this->_em->getClassMetadata(__NAMESPACE__ . '\DDC1135User'), - $this->_em->getClassMetadata(__NAMESPACE__ . '\DDC1135Phone'), - ); - try { - $this->_schemaTool->dropSchema($classes); - $this->_schemaTool->createSchema($classes); + $this->_schemaTool->createSchema(array( + $this->_em->getClassMetadata(__NAMESPACE__ . '\DDC1135User'), + $this->_em->getClassMetadata(__NAMESPACE__ . '\DDC1135Phone'), + )); + $this->loadFixture(); } catch(\Exception $e) { - try { - $this->_schemaTool->createSchema($classes); - } catch (Exception $exc) { - $this->fail($exc->getMessage()); - } } - $this->loadFixture(); } @@ -45,7 +36,6 @@ class DDC1135Test extends \Doctrine\Tests\OrmFunctionalTestCase $this->assertArrayHasKey(2, $result); $this->assertArrayHasKey(3, $result); - $dql = 'SELECT u, p FROM '.__NAMESPACE__ . '\DDC1135User u INDEX BY u.email INNER JOIN u.phones p INDEX BY p.id'; $query = $this->_em->createQuery($dql); $result = $query->getResult(); @@ -84,7 +74,7 @@ class DDC1135Test extends \Doctrine\Tests\OrmFunctionalTestCase $this->assertArrayHasKey(1, $result); $this->assertArrayHasKey(2, $result); $this->assertArrayHasKey(3, $result); - $this->assertEquals('SELECT u FROM Doctrine\Tests\ORM\Functional\Ticket\DDC1135User u INDEX BY u.id', $dql); + $this->assertEquals('SELECT u FROM ' . __NAMESPACE__ . '\DDC1135User u INDEX BY u.id', $dql); } public function testIndexByUnique() @@ -99,7 +89,7 @@ class DDC1135Test extends \Doctrine\Tests\OrmFunctionalTestCase $this->assertArrayHasKey('foo@foo.com', $result); $this->assertArrayHasKey('bar@bar.com', $result); $this->assertArrayHasKey('foobar@foobar.com', $result); - $this->assertEquals('SELECT u FROM Doctrine\Tests\ORM\Functional\Ticket\DDC1135User u INDEX BY u.email', $dql); + $this->assertEquals('SELECT u FROM ' . __NAMESPACE__ . '\DDC1135User u INDEX BY u.email', $dql); } public function testIndexWithJoin() @@ -108,7 +98,6 @@ class DDC1135Test extends \Doctrine\Tests\OrmFunctionalTestCase $builder->select('u','p') ->from(__NAMESPACE__ . '\DDC1135User', 'u', 'u.email') ->join('u.phones', 'p', null, null, 'p.id'); - $dql = $builder->getQuery()->getDQL(); $result = $builder->getQuery()->getResult(); @@ -182,7 +171,7 @@ class DDC1135User */ public $phones; - public function __construct($email, $name,array $numbers) + public function __construct($email, $name, array $numbers = array()) { $this->name = $name; $this->email = $email;