added support for Inherited Named Queries
This commit is contained in:
parent
81cc6d9da8
commit
8af0f9d071
@ -313,6 +313,10 @@ class ClassMetadataFactory implements ClassMetadataFactoryInterface
|
|||||||
$class->containsForeignIdentifier = true;
|
$class->containsForeignIdentifier = true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if ($parent && !empty ($parent->namedQueries)) {
|
||||||
|
$this->addInheritedNamedQueries($class, $parent);
|
||||||
|
}
|
||||||
|
|
||||||
$class->setParentClasses($visited);
|
$class->setParentClasses($visited);
|
||||||
|
|
||||||
if ($this->evm->hasListeners(Events::loadClassMetadata)) {
|
if ($this->evm->hasListeners(Events::loadClassMetadata)) {
|
||||||
@ -429,6 +433,25 @@ class ClassMetadataFactory implements ClassMetadataFactoryInterface
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Adds inherited named queries to the subclass mapping.
|
||||||
|
*
|
||||||
|
* @since 2.2
|
||||||
|
* @param Doctrine\ORM\Mapping\ClassMetadata $subClass
|
||||||
|
* @param Doctrine\ORM\Mapping\ClassMetadata $parentClass
|
||||||
|
*/
|
||||||
|
private function addInheritedNamedQueries(ClassMetadata $subClass, ClassMetadata $parentClass)
|
||||||
|
{
|
||||||
|
foreach ($parentClass->namedQueries as $name => $query) {
|
||||||
|
if (!isset ($subClass->namedQueries[$name])) {
|
||||||
|
$subClass->addNamedQuery(array(
|
||||||
|
'name' => $query['name'],
|
||||||
|
'query' => $query['query']
|
||||||
|
));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Completes the ID generator mapping. If "auto" is specified we choose the generator
|
* Completes the ID generator mapping. If "auto" is specified we choose the generator
|
||||||
* most appropriate for the targeted database platform.
|
* most appropriate for the targeted database platform.
|
||||||
|
@ -685,7 +685,7 @@ class ClassMetadataInfo implements ClassMetadata
|
|||||||
if ( ! isset($this->namedQueries[$queryName])) {
|
if ( ! isset($this->namedQueries[$queryName])) {
|
||||||
throw MappingException::queryNotFound($this->name, $queryName);
|
throw MappingException::queryNotFound($this->name, $queryName);
|
||||||
}
|
}
|
||||||
return $this->namedQueries[$queryName];
|
return $this->namedQueries[$queryName]['dql'];
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -1448,8 +1448,15 @@ class ClassMetadataInfo implements ClassMetadata
|
|||||||
if (isset($this->namedQueries[$queryMapping['name']])) {
|
if (isset($this->namedQueries[$queryMapping['name']])) {
|
||||||
throw MappingException::duplicateQueryMapping($this->name, $queryMapping['name']);
|
throw MappingException::duplicateQueryMapping($this->name, $queryMapping['name']);
|
||||||
}
|
}
|
||||||
$query = str_replace('__CLASS__', $this->name, $queryMapping['query']);
|
|
||||||
$this->namedQueries[$queryMapping['name']] = $query;
|
$name = $queryMapping['name'];
|
||||||
|
$query = $queryMapping['query'];
|
||||||
|
$dql = str_replace('__CLASS__', $this->name, $query);
|
||||||
|
$this->namedQueries[$name] = array(
|
||||||
|
'name' => $name,
|
||||||
|
'query' => $query,
|
||||||
|
'dql' => $dql
|
||||||
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
129
tests/Doctrine/Tests/ORM/Functional/Ticket/DDC1404Test.php
Normal file
129
tests/Doctrine/Tests/ORM/Functional/Ticket/DDC1404Test.php
Normal file
@ -0,0 +1,129 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace Doctrine\Tests\ORM\Functional\Ticket;
|
||||||
|
|
||||||
|
require_once __DIR__ . '/../../../TestInit.php';
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @group DDC-1404
|
||||||
|
*/
|
||||||
|
class DDC1404Test extends \Doctrine\Tests\OrmFunctionalTestCase
|
||||||
|
{
|
||||||
|
|
||||||
|
protected function setUp()
|
||||||
|
{
|
||||||
|
parent::setUp();
|
||||||
|
|
||||||
|
try {
|
||||||
|
$this->_schemaTool->createSchema(array(
|
||||||
|
$this->_em->getClassMetadata(__NAMESPACE__ . '\DDC1404ParentEntity'),
|
||||||
|
$this->_em->getClassMetadata(__NAMESPACE__ . '\DDC1404ChildEntity'),
|
||||||
|
));
|
||||||
|
|
||||||
|
$this->loadFixtures();
|
||||||
|
|
||||||
|
} catch (Exception $exc) {
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public function testTicket()
|
||||||
|
{
|
||||||
|
$repository = $this->_em->getRepository(__NAMESPACE__ . '\DDC1404ChildEntity');
|
||||||
|
$queryAll = $repository->createNamedQuery('all');
|
||||||
|
$queryFirst = $repository->createNamedQuery('first');
|
||||||
|
$querySecond = $repository->createNamedQuery('second');
|
||||||
|
|
||||||
|
|
||||||
|
$this->assertEquals('SELECT p FROM Doctrine\Tests\ORM\Functional\Ticket\DDC1404ChildEntity p', $queryAll->getDQL());
|
||||||
|
$this->assertEquals('SELECT p FROM Doctrine\Tests\ORM\Functional\Ticket\DDC1404ChildEntity p WHERE p.id = 1', $queryFirst->getDQL());
|
||||||
|
$this->assertEquals('SELECT p FROM Doctrine\Tests\ORM\Functional\Ticket\DDC1404ChildEntity p WHERE p.id = 2', $querySecond->getDQL());
|
||||||
|
|
||||||
|
|
||||||
|
$this->assertEquals(sizeof($queryAll->getResult()), 2);
|
||||||
|
$this->assertEquals(sizeof($queryFirst->getResult()), 1);
|
||||||
|
$this->assertEquals(sizeof($querySecond->getResult()), 1);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
public function loadFixtures()
|
||||||
|
{
|
||||||
|
$c1 = new DDC1404ChildEntity("ChildEntity 1");
|
||||||
|
$c2 = new DDC1404ChildEntity("ChildEntity 2");
|
||||||
|
|
||||||
|
$this->_em->persist($c1);
|
||||||
|
$this->_em->persist($c2);
|
||||||
|
|
||||||
|
$this->_em->flush();
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @MappedSuperclass
|
||||||
|
*
|
||||||
|
* @NamedQueries({
|
||||||
|
* @NamedQuery(name="all", query="SELECT p FROM __CLASS__ p"),
|
||||||
|
* @NamedQuery(name="first", query="SELECT p FROM __CLASS__ p WHERE p.id = 1"),
|
||||||
|
* })
|
||||||
|
*/
|
||||||
|
class DDC1404ParentEntity
|
||||||
|
{
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @Id
|
||||||
|
* @Column(type="integer")
|
||||||
|
* @GeneratedValue()
|
||||||
|
*/
|
||||||
|
protected $id;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @return integer
|
||||||
|
*/
|
||||||
|
public function getId()
|
||||||
|
{
|
||||||
|
return $this->id;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @Entity
|
||||||
|
*
|
||||||
|
* @NamedQueries({
|
||||||
|
* @NamedQuery(name="first", query="SELECT p FROM __CLASS__ p WHERE p.id = 1"),
|
||||||
|
* @NamedQuery(name="second", query="SELECT p FROM __CLASS__ p WHERE p.id = 2")
|
||||||
|
* })
|
||||||
|
*/
|
||||||
|
class DDC1404ChildEntity extends DDC1404ParentEntity
|
||||||
|
{
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @column(type="string")
|
||||||
|
*/
|
||||||
|
private $name;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param string $name
|
||||||
|
*/
|
||||||
|
public function __construct($name)
|
||||||
|
{
|
||||||
|
$this->name = $name;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @return string
|
||||||
|
*/
|
||||||
|
public function getName()
|
||||||
|
{
|
||||||
|
return $this->name;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param string $name
|
||||||
|
*/
|
||||||
|
public function setName($name)
|
||||||
|
{
|
||||||
|
$this->name = $name;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
@ -54,7 +54,7 @@ class ClassMetadataTest extends \Doctrine\Tests\OrmTestCase
|
|||||||
$this->assertEquals('phonenumbers', $oneOneMapping['fieldName']);
|
$this->assertEquals('phonenumbers', $oneOneMapping['fieldName']);
|
||||||
$this->assertEquals('Doctrine\Tests\Models\CMS\Bar', $oneOneMapping['targetEntity']);
|
$this->assertEquals('Doctrine\Tests\Models\CMS\Bar', $oneOneMapping['targetEntity']);
|
||||||
$this->assertTrue($cm->isReadOnly);
|
$this->assertTrue($cm->isReadOnly);
|
||||||
$this->assertEquals(array('dql' => 'foo'), $cm->namedQueries);
|
$this->assertEquals(array('dql' => array('name'=>'dql','query'=>'foo','dql'=>'foo')), $cm->namedQueries);
|
||||||
}
|
}
|
||||||
|
|
||||||
public function testFieldIsNullable()
|
public function testFieldIsNullable()
|
||||||
|
Loading…
x
Reference in New Issue
Block a user