DDC-1141: DisconnectedClassMetadataFactory forbids direct XML-to-SQL conversion #1741

Closed
doctrinebot opened this Issue May 10, 2011 · 4 comments

2 participants

@doctrinebot

Jira issue originally created by user fzaninotto:

Doctrine\ORM\Tools\DisconnectedClassMetadataFactory::newClassMetadataInstance() returns instances of ClassMetadataInfo, while the parent returns instances of ClassMetadata. This creates problems when using SchemaTool to create a schema from an XML mapping for instance:

Fatal error: Call to undefined method Doctrine\ORM\Mapping\ClassMetadataInfo::getQuotedTableName() 
in path/to/Doctrine/ORM/Tools/SchemaTool.php on line 133

This is because schemaTool expects ClassMetadata objects, while it got ClassMetadataInfo objects.

My setup:

$config = new Configuration();
$config->setMetadataCacheImpl(new \Doctrine\Common\Cache\ArrayCache);
$driverImpl = new XmlDriver(*_DIR_* . '/fixtures');
$config->setMetadataDriverImpl($driverImpl);
$config->setProxyDir(*_DIR_* . '/Proxies');
$config->setProxyNamespace('Proxies');
$connectionOptions = array(
    'driver' => 'pdo_sqlite',
    'path' => 'database.sqlite'
);
$em = \Doctrine\ORM\EntityManager::create($connectionOptions, $config);
$cmf = new DisconnectedClassMetadataFactory();
$cmf->setEntityManager($em);

$metadatas = $cmf->getAllMetadata() // array of ClassMetadataInfo and not ClassMetadata

I think Doctrine\ORM\Tools\DisconnectedClassMetadataFactory::newClassMetadataInstance() should return a ClassMetadata instance.

@doctrinebot

Comment created by fzaninotto:

Ok, the proposed solution is wrong.

I actually need an array of ClassMetadataInfo and not of ClassMetadata, since the entities don't exist (yet). So the problem lies in the fact that getQuotedTableName() and getQuotedColumnName() are methods of Doctrine\ORM\Mapping\ClassMetadata, and not of Doctrine\ORM\Mapping\ClassMetadataInfo. Moving these two methods up the inheritance path, I managed to create a schema directly from an XML mapping. However, I had to inject the MetadataFactory inside the EM, otherwise the SchemaTool coped when dealing wit hrelationships.

So a working setup to generate SQL from an XML mapping without stepping by Entity generation is:

$config = new Configuration();
$config->setMetadataCacheImpl(new \Doctrine\Common\Cache\ArrayCache);
$driverImpl = new XmlDriver(*_DIR_* . '/fixtures');
$config->setMetadataDriverImpl($driverImpl);
$config->setProxyDir(*_DIR_* . '/Proxies');
$config->setProxyNamespace('Proxies');
$config->setClassMetadataFactoryName('Doctrine\ORM\Tools\DisconnectedClassMetadataFactory');
$connectionOptions = array(
    'driver' => 'pdo_sqlite',
    'path' => 'database.sqlite'
);
$em = \Doctrine\ORM\EntityManager::create($connectionOptions, $config);
$cmf = $em->getMetadataFactory();

$schemaTool = new SchemaTool($em);
$schemaTool->createSchema($cmf->getAllMetadata());

But again, this requires moving up the two methods from ClassMetadata to ClassMetadataInfo.

@doctrinebot

Comment created by @beberlei:

Moving the methods up is the solution i guess, however i have to think why we did this in the first place, probably because of dependencies.

@doctrinebot

Comment created by @beberlei:

This was merged into Doctrine 2.1 / master

@doctrinebot

Issue was closed with resolution "Fixed"

@beberlei beberlei was assigned by doctrinebot Dec 6, 2015
@doctrinebot doctrinebot added this to the 2.1 milestone Dec 6, 2015
@doctrinebot doctrinebot closed this Dec 6, 2015
@doctrinebot doctrinebot added the Bug label Dec 7, 2015
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment