Skip to content

Instantly share code, notes, and snippets.

@doctrinebot
Created December 13, 2015 18:47
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save doctrinebot/4c8b61c3a41a4aa6ea8e to your computer and use it in GitHub Desktop.
Save doctrinebot/4c8b61c3a41a4aa6ea8e to your computer and use it in GitHub Desktop.
Attachments to Doctrine Jira Issue DDC-618 - https://github.com/doctrine/doctrine2/issues/5125
<?php
namespace Doctrine\Tests\ORM\Functional\Ticket;
use DateTime;
require_once __DIR__ . '/../../../TestInit.php';
class DDC618Test extends \Doctrine\Tests\OrmFunctionalTestCase
{
protected function setUp()
{
parent::setUp();
$this->_em->getConnection()->getConfiguration()->setSQLLogger(new \Doctrine\DBAL\Logging\EchoSQLLogger);
$this->_schemaTool->createSchema(array(
$this->_em->getClassMetadata(__NAMESPACE__ . '\DDC618Author'),
$this->_em->getClassMetadata(__NAMESPACE__ . '\DDC618Book')
));
// Create author 10/Joe with two books 22/JoeA and 20/JoeB
$author = new DDC618Author();
$author->id = 10;
$author->name = 'Joe';
$this->_em->persist($author);
$book = new DDC618Book();
$book->id = 22;
$book->name = "JoeA";
$book->author = $author;
$this->_em->persist($book);
$book = new DDC618Book();
$book->id = 20;
$book->name = "JoeB";
$book->author = $author;
$this->_em->persist($book);
// Create author 11/Alice with two books 21/AliceA and 23/AliceB
$author = new DDC618Author();
$author->id = 11;
$author->name = 'Alice';
$this->_em->persist($author);
$book = new DDC618Book();
$book->id = 21;
$book->name = "AliceA";
$book->author = $author;
$this->_em->persist($book);
$book = new DDC618Book();
$book->id = 23;
$book->name = "AliceB";
$book->author = $author;
$this->_em->persist($book);
$this->_em->flush();
}
public function testIndexByHydrateArray()
{
$this->_em->clear();
// Fetch all authors indexed by name ascending and their books indexed by name ascending, hydrated as objects
$result = $this->_em
->createQuery(
'SELECT A, B '.
'FROM Doctrine\Tests\ORM\Functional\Ticket\DDC618Author A INDEX BY A.name '.
'LEFT JOIN A.books B INDEX BY B.name '.
'ORDER BY A.name ASC, B.name ASC'
)
->getResult(\Doctrine\ORM\Query::HYDRATE_ARRAY);
$expected = array();
foreach($result as $author)
{
$books = array();
foreach($author['books'] as $book)
{
$books[$book['name']] = $book;
}
$author['books'] = $books;
$expected[$author['name']] = $author;
}
$this->assertEquals($expected, $result);
}
}
/**
* @Entity
* @Table (name="ddc618author", uniqueConstraints={ @Index (name="UQ_authorname", columns={ "name" }) })
*/
class DDC618Author
{
/**
* @Id
* @Column(type="integer")
*/
public $id;
/** @Column(type="string") */
public $name;
/** @OneToMany(targetEntity="DDC618Book", mappedBy="author", cascade={"persist"}) */
public $books;
public function __construct()
{
$this->books = new \Doctrine\Common\Collections\ArrayCollection;
}
}
/**
* @Entity
* @Table (name="ddc618book", uniqueConstraints={ @Index (name="UQ_bookname", columns={ "name" }) })
*/
class DDC618Book
{
/**
* @Id
* @Column(type="integer")
*/
public $id;
/** @Column(type="string") */
public $name;
/**
* @ManyToOne(targetEntity="DDC618Author", inversedBy="books")
* @JoinColumn(name="author_id", referencedColumnName="id", nullable=false)
*/
public $author;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment