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
ThereadOnly=true
is not tracked for updates anymore but it is still possible to insert or delete rows, as explained in the documentation.##.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 is table is not used by your code in your project for is yoursis project your project for.schema_filter
Try to create a random table using raw sql and useyou can tell doctrine to ignore this table in his validation and update process.
doctrine:schema:validate
It work for. It will show
database is not in syncerror. 一旦將其放入 schema_filter 中,錯誤就不會再發生。
doctrine:migration:diff
schema_ignore_classand
doctrine:schema:update執行架構管理器更新時忽略 Doctrine2 實體
#使用學說遷移doctrine:migration:diff
then
doctrine:migration:migrateinstead of
doctrine: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 #