5

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.

Bonswouar
  • 1,521
  • 2
  • 17
  • 37
  • what would you consider a common use case for this? If you modify 1 entity that has relations to another then in order for your database to stay in sync you would need to update both entities. Can you provide why you are trying to isolate the sql for a given entity? – Chase Mar 31 '14 at 21:46
  • It's because I have hundreds of entities (previously generated by reverse engineering the Database with `doctrine:mapping:import`). And as I said in the "P.S", because of the weirdness of those tables it generates some errors if I `doctrine:schema:update` everything, and if I correct those errors it generates lots of useless and/or wrong SQL code. – Bonswouar Apr 01 '14 at 10:05

3 Answers3

5

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);
    }
}
Lulhum
  • 774
  • 11
  • 16
2

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.

Alberto Fernández
  • 1,568
  • 13
  • 22
  • So this is kind of a hack, isn't it ? I would create this entity manager only for generating SQL and then remove it ? I wonder why there isn't a cleaner solution.. Like just a command line option. – Bonswouar Apr 01 '14 at 10:00
  • This helped me. Thanks – kieste Apr 05 '16 at 21:19
1

You can use grep:

 php bin/console doctrine:schema:create --dump-sql|grep -w 'CREATE TABLE entity_name'
ZxDx
  • 51
  • 1
  • 4