Commit 120e6942 authored by beberlei's avatar beberlei

[2.0] DDC-115 - Fix for multiple class metadata instances because of classes...

[2.0] DDC-115 - Fix for multiple class metadata instances because of classes being addressed with or without prepending namespace separator.
parent 69ef1636
...@@ -102,6 +102,10 @@ class ClassMetadataFactory ...@@ -102,6 +102,10 @@ class ClassMetadataFactory
*/ */
public function getMetadataFor($className) public function getMetadataFor($className)
{ {
if($className[0] == "\\") {
$className = substr($className, 1);
}
if ( ! isset($this->_loadedMetadata[$className])) { if ( ! isset($this->_loadedMetadata[$className])) {
$cacheKey = "$className\$CLASSMETADATA"; $cacheKey = "$className\$CLASSMETADATA";
if ($this->_cacheDriver) { if ($this->_cacheDriver) {
...@@ -126,6 +130,10 @@ class ClassMetadataFactory ...@@ -126,6 +130,10 @@ class ClassMetadataFactory
*/ */
public function hasMetadataFor($className) public function hasMetadataFor($className)
{ {
if($className[0] == "\\") {
$className = substr($className, 1);
}
return isset($this->_loadedMetadata[$className]); return isset($this->_loadedMetadata[$className]);
} }
...@@ -139,6 +147,10 @@ class ClassMetadataFactory ...@@ -139,6 +147,10 @@ class ClassMetadataFactory
*/ */
public function setMetadataFor($className, $class) public function setMetadataFor($className, $class)
{ {
if($className[0] == "\\") {
$className = substr($className, 1);
}
$this->_loadedMetadata[$className] = $class; $this->_loadedMetadata[$className] = $class;
} }
......
<?php
/**
* @entity
* @table(name="articles")
*/
class DoctrineGlobal_Article
{
/**
* @id
* @column(type="int")
*/
protected $id;
/**
* @column(type="string")
*/
protected $headline;
/**
* @column(type="text")
*/
protected $text;
/**
* @ManyToMany(targetEntity="DoctrineGlobal_User")
* @JoinTable(name="author_articles",
* joinColumns={@JoinColumn(name="article_id", referencedColumnName="id")},
* inverseJoinColumns={@JoinColumn(name="author_id", referencedColumnName="id", unique=true)}
* )
*/
protected $author;
/**
* @ManyToMany(targetEntity="\DoctrineGlobal_User")
* @JoinTable(name="editor_articles",
* joinColumns={@JoinColumn(name="article_id", referencedColumnName="id")},
* inverseJoinColumns={@JoinColumn(name="editor_id", referencedColumnName="id", unique=true)}
* )
*/
protected $editor;
}
/**
* @Entity
* @Table(name="users")
*/
class DoctrineGlobal_User
{
/**
* @Id
* @column(type="integer")
* @var int
*/
private $id;
/**
* @column(type="string", length=64)
* @var string
*/
private $username;
/**
* @column(type="string", length=128)
* @var string
*/
private $email;
}
\ No newline at end of file
...@@ -16,17 +16,10 @@ class ClassMetadataFactoryTest extends \Doctrine\Tests\OrmTestCase ...@@ -16,17 +16,10 @@ class ClassMetadataFactoryTest extends \Doctrine\Tests\OrmTestCase
{ {
public function testGetMetadataForSingleClass() public function testGetMetadataForSingleClass()
{ {
$driverMock = new DriverMock();
$config = new \Doctrine\ORM\Configuration();
$config->setProxyDir(__DIR__ . '/../../Proxies');
$config->setProxyNamespace('Doctrine\Tests\Proxies');
$eventManager = new EventManager();
$conn = new ConnectionMock(array(), $driverMock, $config, $eventManager);
$mockDriver = new MetadataDriverMock(); $mockDriver = new MetadataDriverMock();
$config->setMetadataDriverImpl($mockDriver); $entityManager = $this->_createEntityManager($mockDriver);
$entityManager = EntityManagerMock::create($conn, $config, $eventManager);
$conn = $entityManager->getConnection();
$mockPlatform = $conn->getDatabasePlatform(); $mockPlatform = $conn->getDatabasePlatform();
$mockPlatform->setPrefersSequences(true); $mockPlatform->setPrefersSequences(true);
$mockPlatform->setPrefersIdentityColumns(false); $mockPlatform->setPrefersIdentityColumns(false);
...@@ -65,6 +58,40 @@ class ClassMetadataFactoryTest extends \Doctrine\Tests\OrmTestCase ...@@ -65,6 +58,40 @@ class ClassMetadataFactoryTest extends \Doctrine\Tests\OrmTestCase
$this->assertTrue($cm1->hasField('name')); $this->assertTrue($cm1->hasField('name'));
$this->assertEquals(ClassMetadata::GENERATOR_TYPE_SEQUENCE, $cm1->generatorType); $this->assertEquals(ClassMetadata::GENERATOR_TYPE_SEQUENCE, $cm1->generatorType);
} }
public function testGetMetadataGlobalNamespaceModel()
{
require_once __DIR__."/../../Models/Global/GlobalNamespaceModel.php";
$reader = new \Doctrine\Common\Annotations\AnnotationReader(new \Doctrine\Common\Cache\ArrayCache);
$reader->setDefaultAnnotationNamespace('Doctrine\ORM\Mapping\\');
$metadataDriver = new \Doctrine\ORM\Mapping\Driver\AnnotationDriver($reader);
$metadataDriver->setClassDirectory(__DIR__."/../../Models/Global/");
$entityManager = $this->_createEntityManager($metadataDriver);
$mf = $entityManager->getMetadataFactory();
$m1 = $mf->getMetadataFor("DoctrineGlobal_Article");
$h2 = $mf->hasMetadataFor("\DoctrineGlobal_Article");
$m2 = $mf->getMetadataFor("\DoctrineGlobal_Article");
$this->assertSame($m1, $m2);
$this->assertTrue($h2);
}
protected function _createEntityManager($metadataDriver)
{
$driverMock = new DriverMock();
$config = new \Doctrine\ORM\Configuration();
$config->setProxyDir(__DIR__ . '/../../Proxies');
$config->setProxyNamespace('Doctrine\Tests\Proxies');
$eventManager = new EventManager();
$conn = new ConnectionMock(array(), $driverMock, $config, $eventManager);
$mockDriver = new MetadataDriverMock();
$config->setMetadataDriverImpl($metadataDriver);
return EntityManagerMock::create($conn, $config, $eventManager);
}
} }
/* Test subject class with overriden factory method for mocking purposes */ /* Test subject class with overriden factory method for mocking purposes */
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment