Symfony 5 - Doctrine 的 schema_filter 无法正常工作
P粉002023326
2023-08-26 18:16:28
<p>当我在我的项目中执行命令行 <code>doctrine:schema:update --force</code> 时,我尝试忽略两个实体,如下所示:</p>
<pre class="brush:php;toolbar:false;">/*** @ORM\Entity(只读=true)
* @ORM\Table(名称=“view_tableau_de_bord”)*/
class ViewTableauDeBord
{
//...
}</pre>
<p>在我的doctrine.yaml配置文件中:</p>
<pre class="brush:php;toolbar:false;">doctrine:
dbal:
default_connection: default
connections:
default:
url: '%env(resolve:DATABASE_URL)%'
driver: 'pdo_pgsql'
server_version: '12'
charset: utf8
schema_filter: ~^(?!view_)~
# ...</pre>
<p>Doctrine 不断生成所有实体,而我的视图位于 <code>schema_filter</code> 中。你对此有何解释?这是我第一次在项目中使用此选项。</p>
<p>项目设置:</p>
<ul>
<li>Symfony 5.4.14</li>
<li>PHP 7.4.26</li>
<li>教义:orm:2.13.3</li>
<li>理论/注释:1.13.3</li>
<li>学说/学说包:2.7.0</li>
<li>学说/学说迁移包:3.2.2</li>
<li>symfony/doctrine-bridge:5.4.14</li>
<li>理论/数据装置:1.5.3</li>
</ul></p>
An entity marked with the flag
readOnly=true
is not tracked for updates anymore but it is still possible to insert or delete rows, as explained in the documentation.The
doctrine:schema:update
command will still take the table into account to update the schema.在问题的答案中“忽略 Doctrine2 实体运行架构管理器更新时” 有 3 个有效选项可以忽略架构更新中的实体。
schema_filter
schema_filter
is 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_filter
you 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 sync
error. 一旦将其放入 schema_filter 中,错误就不会再发生。It work for
doctrine:migration:diff
anddoctrine:schema:update
schema_ignore_class
但是,如果您想避免在数据库内生成实体,则可以从 Ernesto 的答案中的链接中找到:
仅从 Doctrine 2.7 版本开始工作。 您可以在这里找到完整的示例: 运行架构管理器更新时忽略 Doctrine2 实体
使用学说迁移
I strongly advise you to use
doctrine:migration:diff
thendoctrine:migration:migrate
instead ofdoctrine:schema:update
to 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