Symfony 5 - Doctrine 的 schema_filter 無法正常運作
P粉002023326
P粉002023326 2023-08-26 18:16:28
0
2
623
<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>
P粉002023326
P粉002023326

全部回覆(2)
P粉186897465

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 個有效選項可以忽略架構更新中的實體。

P粉455093123

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 is table is not used by your code in your project for is yoursis project your project for.

這是一個典型的範例,您不希望每次更新架構時都會產生這樣的查詢。

DROP TABLE view_booking_by_customer_per_year; // NO I DONT WANT THIS
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 show database is not in sync error. 一旦將其放入 schema_filter 中,錯誤就不會再發生。

It work for

doctrine:migration:diff and doctrine:schema:update

schema_ignore_class

但是,如果您想避免在資料庫內產生實體,則可以從 Ernesto 的答案中的連結中找到:

schema_ignore_classes:
   - Reference\To\My\Class
   - Reference\To\My\OtherClass
僅從 Doctrine 2.7 版本開始工作。 您可以在這裡找到完整的範例:

執行架構管理器更新時忽略 Doctrine2 實體

#使用學說遷移

I strongly advise you to use

doctrine:migration:diff then doctrine:migration:migrate instead of doctrine:schema:update to performbase . It's ok for local dev, but when in production it is a very bad practice.

https://symfony.com/bundles/DoctrineMigrationsBundle/current/index.html #

熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板