Symfony 5 - Doctrine 的 schema_filter 無法正常運作
P粉002023326
P粉002023326 2023-08-26 18:16:28
0
2
448

當我在我的專案中執行命令列 doctrine:schema:update --force 時,我嘗試忽略兩個實體,如下所示:

/*** @ORM\Entity(只讀=true) * @ORM\Table(名稱=“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 中。你對此有何解釋?這是我第一次在專案中使用此選項。

專案設定:

  • Symfony 5.4.14
  • PHP 7.4.26
  • 教義:orm:2.13.3
  • 理論/註:1.13.3
  • 學說/學說包:2.7.0
  • 學說/學說遷移包:3.2.2
  • symfony/doctrine-bridge:5.4.14
  • 理論/資料裝置:1.5.3

P粉002023326
P粉002023326

全部回覆 (2)
P粉186897465

An entity marked with the flagreadOnly=trueis not tracked for updates anymore but it is still possible to insert or delete rows, as explained in thedocumentation.##.

The

doctrine:schema:updatecommand will still take the table into account to update the schema.

在問題的答案中

「忽略 Doctrine2 實體執行架構管理器更新時」有 3 個有效選項可以忽略架構更新中的實體。

    P粉455093123

    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 calledview_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_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: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:diffthendoctrine:migration:migrateinstead ofdoctrine:schema:updateto 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#

      最新下載
      更多>
      網站特效
      網站源碼
      網站素材
      前端模板
      關於我們 免責聲明 Sitemap
      PHP中文網:公益線上PHP培訓,幫助PHP學習者快速成長!