Symfony 5 – Der schema_filter von Doctrine funktioniert nicht richtig
P粉002023326
2023-08-26 18:16:28
<p>Wenn ich in meinem Projekt die Befehlszeile <code>doctrine:schema:update --force</code> ausführe, versuche ich, zwei Entitäten wie diese zu ignorieren: </p>
<pre class="brush:php;toolbar:false;">/*** @ORMEtity(readOnly=true)
* @ORMTable(name="view_tableau_de_bord")*/
Klasse ViewTableauDeBord
{
//......
}</pre>
<p>In meiner doctrine.yaml-Konfigurationsdatei: </p>
<pre class="brush:php;toolbar:false;">doktrin:
dbal:
default_connection: Standard
Verbindungen:
Standard:
URL: '%env(resolve:DATABASE_URL)%'
Treiber: 'pdo_pgsql'
server_version: '12'
Zeichensatz:utf8
schema_filter: ~^(?!view_)~
# ...</pre>
<p>Doctrine generiert weiterhin alle Entitäten und meine Ansicht befindet sich in <code>schema_filter</code>. Was ist Ihre Erklärung dafür? Dies ist das erste Mal, dass ich diese Option in einem Projekt verwende. </p>
<p>Projekteinstellungen:</p>
<ul>
<li>Symfony 5.4.14</li>
<li>PHP 7.4.26</li>
<li>Lehre: orm: 2.13.3</li>
<li>Theorie/Anmerkungen: 1.13.3</li>
<li>Doktrin/Doktrinenpaket: 2.7.0</li>
<li>Doktrin/Doktrin-Migrationspaket: 3.2.2</li>
<li>symfony/doctrine-bridge:5.4.14</li>
<li>Theorie/Datengerät: 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