3 namespace Doctrine\Tests\ORM\Functional\Ticket;
7 require_once __DIR__ . '/../../../TestInit.php';
12 class DDC618Test extends \Doctrine\Tests\OrmFunctionalTestCase
14 protected function setUp()
18 $this->_schemaTool->createSchema(array(
19 $this->_em->getClassMetadata(__NAMESPACE__ . '\DDC618Author'),
20 $this->_em->getClassMetadata(__NAMESPACE__ . '\DDC618Book')
23 // Create author 10/Joe with two books 22/JoeA and 20/JoeB
24 $author = new DDC618Author();
26 $author->name = 'Joe';
27 $this->_em->persist($author);
29 // Create author 11/Alice with two books 21/AliceA and 23/AliceB
30 $author = new DDC618Author();
32 $author->name = 'Alice';
33 $author->addBook('In Wonderland');
34 $author->addBook('Reloaded');
35 $author->addBook('Test');
37 $this->_em->persist($author);
41 } catch(\Exception $e) {
46 public function testIndexByHydrateObject()
48 $dql = 'SELECT A FROM Doctrine\Tests\ORM\Functional\Ticket\DDC618Author A INDEX BY A.name ORDER BY A.name ASC';
49 $result = $this->_em->createQuery($dql)->getResult(\Doctrine\ORM\Query::HYDRATE_OBJECT);
51 $joe = $this->_em->find('Doctrine\Tests\ORM\Functional\Ticket\DDC618Author', 10);
52 $alice = $this->_em->find('Doctrine\Tests\ORM\Functional\Ticket\DDC618Author', 11);
54 $this->assertArrayHasKey('Joe', $result, "INDEX BY A.name should return an index by the name of 'Joe'.");
55 $this->assertArrayHasKey('Alice', $result, "INDEX BY A.name should return an index by the name of 'Alice'.");
58 public function testIndexByHydrateArray()
60 $dql = 'SELECT A FROM Doctrine\Tests\ORM\Functional\Ticket\DDC618Author A INDEX BY A.name ORDER BY A.name ASC';
61 $result = $this->_em->createQuery($dql)->getResult(\Doctrine\ORM\Query::HYDRATE_ARRAY);
63 $joe = $this->_em->find('Doctrine\Tests\ORM\Functional\Ticket\DDC618Author', 10);
64 $alice = $this->_em->find('Doctrine\Tests\ORM\Functional\Ticket\DDC618Author', 11);
66 $this->assertArrayHasKey('Joe', $result, "INDEX BY A.name should return an index by the name of 'Joe'.");
67 $this->assertArrayHasKey('Alice', $result, "INDEX BY A.name should return an index by the name of 'Alice'.");
73 public function testIndexByJoin()
75 $dql = 'SELECT A, B FROM Doctrine\Tests\ORM\Functional\Ticket\DDC618Author A '.
76 'INNER JOIN A.books B INDEX BY B.title ORDER BY A.name ASC';
77 $result = $this->_em->createQuery($dql)->getResult(\Doctrine\ORM\Query::HYDRATE_OBJECT);
79 $this->assertEquals(3, count($result[0]->books)); // Alice, Joe doesnt appear because he has no books.
80 $this->assertEquals('Alice', $result[0]->name);
81 $this->assertTrue( isset($result[0]->books["In Wonderland"] ), "Indexing by title should have books by title.");
82 $this->assertTrue( isset($result[0]->books["Reloaded"] ), "Indexing by title should have books by title.");
83 $this->assertTrue( isset($result[0]->books["Test"] ), "Indexing by title should have books by title.");
85 $result = $this->_em->createQuery($dql)->getResult(\Doctrine\ORM\Query::HYDRATE_ARRAY);
87 $this->assertEquals(3, count($result[0]['books'])); // Alice, Joe doesnt appear because he has no books.
88 $this->assertEquals('Alice', $result[0]['name']);
89 $this->assertTrue( isset($result[0]['books']["In Wonderland"] ), "Indexing by title should have books by title.");
90 $this->assertTrue( isset($result[0]['books']["Reloaded"] ), "Indexing by title should have books by title.");
91 $this->assertTrue( isset($result[0]['books']["Test"] ), "Indexing by title should have books by title.");
97 public function testIndexByToOneJoinSilentlyIgnored()
99 $dql = 'SELECT B, A FROM Doctrine\Tests\ORM\Functional\Ticket\DDC618Book B '.
100 'INNER JOIN B.author A INDEX BY A.name ORDER BY A.name ASC';
101 $result = $this->_em->createQuery($dql)->getResult(\Doctrine\ORM\Query::HYDRATE_OBJECT);
103 $this->assertInstanceOf('Doctrine\Tests\ORM\Functional\Ticket\DDC618Book', $result[0]);
104 $this->assertInstanceOf('Doctrine\Tests\ORM\Functional\Ticket\DDC618Author', $result[0]->author);
106 $dql = 'SELECT B, A FROM Doctrine\Tests\ORM\Functional\Ticket\DDC618Book B '.
107 'INNER JOIN B.author A INDEX BY A.name ORDER BY A.name ASC';
108 $result = $this->_em->createQuery($dql)->getResult(\Doctrine\ORM\Query::HYDRATE_ARRAY);
110 $this->assertEquals("Alice", $result[0]['author']['name']);
116 public function testCombineIndexBy()
118 $dql = 'SELECT A, B FROM Doctrine\Tests\ORM\Functional\Ticket\DDC618Author A INDEX BY A.id '.
119 'INNER JOIN A.books B INDEX BY B.title ORDER BY A.name ASC';
120 $result = $this->_em->createQuery($dql)->getResult(\Doctrine\ORM\Query::HYDRATE_OBJECT);
122 $this->assertArrayHasKey(11, $result); // Alice
124 $this->assertEquals(3, count($result[11]->books)); // Alice, Joe doesnt appear because he has no books.
125 $this->assertEquals('Alice', $result[11]->name);
126 $this->assertTrue( isset($result[11]->books["In Wonderland"] ), "Indexing by title should have books by title.");
127 $this->assertTrue( isset($result[11]->books["Reloaded"] ), "Indexing by title should have books by title.");
128 $this->assertTrue( isset($result[11]->books["Test"] ), "Indexing by title should have books by title.");
139 * @Column(type="integer")
143 /** @Column(type="string") */
147 * @OneToMany(targetEntity="DDC618Book", mappedBy="author", cascade={"persist"})
151 public function __construct()
153 $this->books = new \Doctrine\Common\Collections\ArrayCollection;
156 public function addBook($title)
158 $book = new DDC618Book($title, $this);
159 $this->books[] = $book;
169 * @Id @GeneratedValue
170 * @Column(type="integer")
174 /** @column(type="string") */
177 /** @ManyToOne(targetEntity="DDC618Author", inversedBy="books") */
180 function __construct($title, $author)
182 $this->title = $title;
183 $this->author = $author;