Symfony2 doctrine update scheme from a specific entity

If I run

php app/console doctrine:schema:update --force 

I am updating my database with all objects.

I need an update database for a custom object only, what is the solution?

One solution is to define a custom entity manager and when transferring the entity manager to

 php app/console doctrine:schema:update --force --em="custom" 

But maybe exisgts is something faster without a specific user entity manager?

+5
source share
2 answers

According to the command documentation, there is no such option. If this is what you need to do only once, I would suggest using the --dump-sql option to get the necessary SQL statements and manually launch the ones you need.

PS I do not understand what is the reason only to update the schema for the object and leave all other objects not synchronized with the database. It sounds like a recipe for getting db errors.

+9
source

You can manually use the Doctrine SchemaTool class to create SQL-diff based on only one object. You will need access to the Doctrines EntityManager - the example below assumes access to the Symfonys container.

 use Doctrine\ORM\Tools\SchemaTool; $em = $this->container->get('doctrine')->getManager(); $schemaTool = new SchemaTool($em); $metadata = $em->getClassMetadata(YourEntity::class); $sqlDiff = $schemaTool->getUpdateSchemaSql([$metadata], true); 

Now the $sqlDiff variable will contain an array of SQL statements needed to match the database schema with the entity mapping.

The second argument passed to SchemaTool::getUpdateSchemaSql() is important - without it, the default behavior would be to generate DROP TABLE statements for every other table that appears in your database.

0
source

All Articles