1
0
mirror of synced 2024-12-14 07:06:04 +03:00

Fix DDC-671 - The sourceEntity field has to be corrected to the subclass name when copied from a mapped superclass. Otherwise DQL queries will be wrong, generating wrong table aliases.

This commit is contained in:
Benjamin Eberlei 2010-09-21 23:14:45 +02:00
parent 72f65c3665
commit 7dc8ef1db9
4 changed files with 92 additions and 53 deletions

View File

@ -379,6 +379,10 @@ class ClassMetadataFactory
private function addInheritedRelations(ClassMetadata $subClass, ClassMetadata $parentClass)
{
foreach ($parentClass->associationMappings as $field => $mapping) {
if ($parentClass->isMappedSuperclass) {
$mapping['sourceEntity'] = $subClass->name;
}
//$subclassMapping = $mapping;
if ( ! isset($mapping['inherited']) && ! $parentClass->isMappedSuperclass) {
$mapping['inherited'] = $parentClass->name;

View File

@ -459,3 +459,48 @@ class User
));
}
}
abstract class AbstractContentItem
{
/**
* Doctrine2 entity id
* @var integer
*/
private $id;
/**
* The parent directory
* @var Directory
*/
protected $parentDirectory;
/**
* Filename (without extension) or directory name
* @var string
*/
protected $name;
}
class Directory extends AbstractContentItem
{
protected $subDirectories;
/**
* This is a collection of files that are contained in this Directory. Files, for example, could be Pages, but even other files
* like media files (css, images etc) are possible.
*
* @var \Doctrine\Common\Collections\Collection
* @access protected
*/
protected $containedFiles;
/**
* @var string
*/
protected $url;
}
class Page extends AbstractContentItem
{
protected $extension = "html";
}

View File

@ -109,6 +109,34 @@ class AnnotationDriverTest extends AbstractMappingDriverTest
{
new $entityClassName;
}
/**
* @group DDC-671
*
* Entities for this test are in AbstractMappingDriverTest
*/
public function testJoinTablesWithMappedSuperclassForAnnotationDriver()
{
$em = $this->_getTestEntityManager();
$em->getConfiguration()->setMetadataDriverImpl($this->_loadDriver());
$classPage = $em->getClassMetadata('Doctrine\Tests\ORM\Mapping\Page');
$this->assertEquals('Doctrine\Tests\ORM\Mapping\Page', $classPage->associationMappings['parentDirectory']['sourceEntity']);
$classDirectory = $em->getClassMetadata('Doctrine\Tests\ORM\Mapping\Directory');
$this->assertEquals('Doctrine\Tests\ORM\Mapping\Directory', $classDirectory->associationMappings['parentDirectory']['sourceEntity']);
$dql = "SELECT f FROM Doctrine\Tests\ORM\Mapping\Page f JOIN f.parentDirectory d " .
"WHERE (d.url = :url) AND (f.extension = :extension)";
$query = $em->createQuery($dql)
->setParameter('url', "test")
->setParameter('extension', "extension");
$this->assertEquals(
'SELECT c0_.id AS id0, c0_.extension AS extension1, c0_.parent_directory_id AS parent_directory_id2 FROM core_content_pages c0_ INNER JOIN core_content_directories c1_ ON c0_.parent_directory_id = c1_.id WHERE (c1_.url = ?) AND (c0_.extension = ?)',
$query->getSql()
);
}
}
/**

View File

@ -19,13 +19,20 @@ class YamlMappingDriverTest extends AbstractMappingDriverTest
return new YamlDriver(__DIR__ . DIRECTORY_SEPARATOR . 'yaml');
}
/**
* @group DDC-671
*
* Entities for this test are in AbstractMappingDriverTest
*/
public function testJoinTablesWithMappedSuperclassForYamlDriver()
{
$em = $this->_getTestEntityManager();
$em->getConfiguration()->setMetadataDriverImpl(new \Doctrine\ORM\Mapping\Driver\YamlDriver(__DIR__ . '/yaml/'));
$em->getConfiguration()->setMetadataDriverImpl($this->_loadDriver());
var_dump($em->getClassMetadata('Doctrine\Tests\ORM\Mapping\Page'));
var_dump($em->getClassMetadata('Doctrine\Tests\ORM\Mapping\Directory'));
$classPage = $em->getClassMetadata('Doctrine\Tests\ORM\Mapping\Page');
$this->assertEquals('Doctrine\Tests\ORM\Mapping\Page', $classPage->associationMappings['parentDirectory']['sourceEntity']);
$classDirectory = $em->getClassMetadata('Doctrine\Tests\ORM\Mapping\Directory');
$this->assertEquals('Doctrine\Tests\ORM\Mapping\Directory', $classDirectory->associationMappings['parentDirectory']['sourceEntity']);
$dql = "SELECT f FROM Doctrine\Tests\ORM\Mapping\Page f JOIN f.parentDirectory d " .
"WHERE (d.url = :url) AND (f.extension = :extension)";
@ -34,55 +41,10 @@ class YamlMappingDriverTest extends AbstractMappingDriverTest
->setParameter('url', "test")
->setParameter('extension', "extension");
var_dump($query->getSql());
// Is there a way to generalize this more? (Instead of a2_., check if the table prefix in the ON clause is set within a FROM or JOIN clause..)
$this->assertTrue(strpos($query->getSql(), 'a2_.') === false);
$this->assertEquals(
'SELECT c0_.id AS id0, c0_.extension AS extension1, c0_.parent_directory_id AS parent_directory_id2 FROM core_content_pages c0_ INNER JOIN core_content_directories c1_ ON c0_.parent_directory_id = c1_.id WHERE (c1_.url = ?) AND (c0_.extension = ?)',
$query->getSql()
);
}
}
class Directory extends AbstractContentItem
{
protected $subDirectories;
/**
* This is a collection of files that are contained in this Directory. Files, for example, could be Pages, but even other files
* like media files (css, images etc) are possible.
*
* @var \Doctrine\Common\Collections\Collection
* @access protected
*/
protected $containedFiles;
/**
* @var string
*/
protected $url;
}
class Page extends AbstractContentItem
{
protected $extension = "html";
}
abstract class AbstractContentItem
{
/**
* Doctrine2 entity id
* @var integer
*/
private $id;
/**
* The parent directory
* @var Directory
*/
protected $parentDirectory;
/**
* Filename (without extension) or directory name
* @var string
*/
protected $name;
}