Symfony 5 - Doctrine's schema_filter not working properly
P粉002023326
2023-08-26 18:16:28
<p>When I execute the command line <code>doctrine:schema:update --force</code> in my project, I try to ignore two entities like this: </p>
<pre class="brush:php;toolbar:false;">/*** @ORM\Entity(readOnly=true)
* @ORM\Table(name="view_tableau_de_bord")*/
class ViewTableauDeBord
{
//...
}</pre>
<p>In my doctrine.yaml configuration file: </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 keeps generating all entities and my view is in <code>schema_filter</code>. What is your explanation for this? This is the first time I've used this option in a project. </p>
<p>Project settings: </p>
<ul>
<li>Symfony 5.4.14</li>
<li>PHP 7.4.26</li>
<li>Doctrine: orm: 2.13.3</li>
<li>Theory/Notes: 1.13.3</li>
<li>doctrine/doctrine package: 2.7.0</li>
<li>doctrine/doctrine migration package: 3.2.2</li>
<li>symfony/doctrine-bridge:5.4.14</li>
<li>Theory/Data Device: 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.In the answer to the question "Ignore Doctrine2 entities when running Schema Manager update" There are 3 valid options to ignore entities in schema updates.
schema_filter
schema_filter
is not made to "filter" entity but to filter db table from doctrine awareness.Here is an example:
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.This is a typical example, you don't want to generate such a query every time you update the schema.
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. Once I put it in schema_filter the error no longer occurs.It work for
doctrine:migration:diff
anddoctrine:schema:update
schema_ignore_class
However, if you want to avoid generating entities within the database, you can find that from the link in Ernesto's answer:
Only works from Doctrine 2.7 version. You can find the complete example here: Ignore Doctrine2 entities when running Schema Manager update
Use Doctrine Migration
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