Doctrine ORM:自动 generate/update 与关系

Doctrine ORM: Auto generate/update with relations

自动生成学说模型很容易。

php vendor/bin/doctrine orm:generate-entities

或者从现有数据库导入 php app/console doctrine:mapping:import --force AcmeBlogBu​​ndle xml

但这些命令只生成我的映射。它也有机会生成 类 与关系。

使模型具有类似的方法。

/** @OneToMany(targetEntity="Comment", mappedBy="article") */
private $comments;

public function __construct()
{
    $this->comments = new ArrayCollection();
}

使用更新实体或重新生成实体等选项?

php doctrine orm:generate-entities --update-entities
php doctrine orm:generate-entities --regenerate-entities

有这样的工具吗?我的用例是一个 cms,用户可以在其中轻松地从配置文件中生成自己的数据模型。他不应该被迫写自己的 类 与关系 (内容管理员的错误修剪)。

Documentation of Doctrine 提到 doctrine 只能生成 70-80% 的映射信息。这意味着它无法检测反向关联、继承类型、具有外键的实体。您需要为此做一些额外的工作。但我同意,在未来的任何学说版本中,这将是一个不错的功能。

我的建议是不要总是依赖学说的进口。

如果您使用的是 Symfony 2,您可以尝试使用命令

php app/console doctrine:generate:entities

它的文档说

Generates entity classes and method stubs from your mapping information

我没用过所以不知道能不能检测和生成关系