Creating/updating schema from Doctrine entities

2019-06-07 23:21发布

问题:

I'm using Doctrine with Zend Framework. I have to create both the DB schema and the Doctrine entities with annotations.

Since the annotations already contain the information, it should be possible to create/update the schema based on them. I don't want to reinvent the wheel, so I was wondering whether that kind of logic already existed?

回答1:

You can use the doctrine CLI, for a lot of doctrine related tasks. I'm not sure how doctrine is integrated into Zend, but look for the doctrine.php, and invoke it like this:

php doctrine.php orm:schema-tool:update --force

This will update your db to match your schema definitions. You can also use

php doctrine.php orm:schema-tool:update --dump-sql

To see what SQL commands would doctrine run. See the Tools section in the doctrine docs for more information.



回答2:

There's the Doctrine SchemaTool (ORM/Tools/SchemaTool).

$meta = array(
    $this->_em->getClassMetadata('Customer')
);

$tool = new \Doctrine\ORM\Tools\SchemaTool($this->_em);
$tool->updateSchema($meta);