当我在我的项目中执行命令行 doctrine:schema:update --force 时,我尝试忽略两个实体,如下所示:
/**
* @ORM\Entity(readOnly=true)
* @ORM\Table(name="view_tableau_de_bord")
*/
class ViewTableauDeBord
{
//...
}
在我的doctrine.yaml配置文件中:
doctrine:
dbal:
default_connection: default
connections:
default:
url: '%env(resolve:DATABASE_URL)%'
driver: 'pdo_pgsql'
server_version: '12'
charset: utf8
schema_filter: ~^(?!view_)~
# ...
Doctrine 不断生成所有实体,而我的视图位于 schema_filter 中。你对此有何解释?这是我第一次在项目中使用此选项。
项目设置:
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号
An entity marked with the flag
readOnly=trueis not tracked for updates anymore but it is still possible to insert or delete rows, as explained in the documentation.The
doctrine:schema:updatecommand will still take the table into account to update the schema.在问题的答案中“忽略 Doctrine2 实体运行架构管理器更新时” 有 3 个有效选项可以忽略架构更新中的实体。
schema_filter
schema_filteris not made to "filter" entity but to filter db table from doctrine awareness.这是一个示例:
Assuming you manually create a table that is updated from a custom raw php cronjob called
view_booking_by_customer_per_year, this table is not used by your code in your project but is used for data analysis.这是一个典型的示例,您不希望每次更新架构时都生成这样的查询。
So using
schema_filteryou can tell doctrine to ignore this table in his validation and update process.Try to create a random table using raw sql and use
doctrine:schema:validate. It will showdatabase is not in syncerror. 一旦将其放入 schema_filter 中,错误就不会再发生。It work for
doctrine:migration:diffanddoctrine:schema:updateschema_ignore_class
但是,如果您想避免在数据库内生成实体,则可以从 Ernesto 的答案中的链接中找到:
仅从 Doctrine 2.7 版本开始工作。 您可以在这里找到完整的示例: 运行架构管理器更新时忽略 Doctrine2 实体
使用学说迁移
I strongly advise you to use
doctrine:migration:diffthendoctrine:migration:migrateinstead ofdoctrine:schema:updateto perform change on database. It's ok for local dev, but when in production it is a very bad practice.https://symfony.com/bundles/DoctrineMigrationsBundle/current/index.html