Reputation: 1590
I'd like to generate my SQL from my entities.
I know the commands php app/console doctrine:schema:create --dump-sql
and doctrine:schema:update
, but apparently there is no way to filter on Entities, not even to filter on Bundle, but only on EntityManager ?!
Did I miss something ? I thought that was a pretty common need, and pretty easy to develop..
P.S. I need it because I have an old weird Database shared with others softwares that isn't exactly how Doctrine would like to, so if I don't filter I'd have some errors, or in the best case lots of useless/wrong modifications.
Upvotes: 5
Views: 7851
Reputation: 59
You can use grep:
php bin/console doctrine:schema:create --dump-sql|grep -w 'CREATE TABLE entity_name'
Upvotes: 0
Reputation: 824
Since the only proposed answer here did not suit me, and this topic appears to be the only one I found referencing this issue, there is my solution (note that I use this in a SF2 ContainerAwareCommand) :
namespace AppBundle\Command;
use Doctrine\DBAL\Schema\Comparator;
use Doctrine\ORM\Tools\SchemaTool;
use Symfony\Bundle\FrameworkBundle\Command\ContainerAwareCommand;
/* ... */
class MyCommand extends ContainerAwareCommand
{
/* ... */
protected function execute(InputInterface $input, OutputInterface $output)
{
$em = $this->getContainer()->get('doctrine.orm.entity_manager');
/* Doctrine tool class for create/drop/update database schemas
based on metadata class descriptors */
$tool = new SchemaTool($em);
/* Doctrine tool class for comparing differences between database
schemas */
$comparator = new Comparator();
/* Create an empty schema */
$fromSchema = $tool->getSchemaFromMetadata(array());
/* Create the schema for our class */
$toSchema = $tool->getSchemaFromMetadata(
array($em->getClassMetadata('AppBundle:MyEntity'))
);
/* Compare schemas, and write result as SQL */
$schemaDiff = $comparator->compare($fromSchema, $toSchema);
$sql = $schemaDiff->toSql(
$em->getConnection()->getDatabasePlatform()
);
$output->writeln($sql);
}
}
Upvotes: 7
Reputation: 1598
You can filter on entity managers, but you need to register them manually in your config:
orm:
auto_generate_proxy_classes: %kernel.debug%
entity_managers:
default:
mappings:
OneBundle:
type: annotation
AnotherBundle:
type: annotation
another_entity_manager:
mappings:
SomeOtherBundle:
type: annotation
This way you can use, for example:
php app/console doctrine:schema:update --dump-sql --em=another_entity_manager
This should only update the schema for the entities in SomeOtherBundle.
Upvotes: 2