I try to ignore two entities when I execute the command line doctrine:schema:update --force
in my project who is writing like this :
/**
* @ORMEntity(readOnly=true)
* @ORMTable(name="view_tableau_de_bord")
*/
class ViewTableauDeBord
{
//...
}
In my doctrine.yaml configuration file:
doctrine:
dbal:
default_connection: default
connections:
default:
url: '%env(resolve:DATABASE_URL)%'
driver: 'pdo_pgsql'
server_version: '12'
charset: utf8
schema_filter: ~^(?!view_)~
# ...
Doctrine keeps generating all entities while my views are in the schema_filter
. Do you have an explanation about this ? It’s my first time with this option in a project.
Configuration du projet:
- Symfony 5.4.14
- PHP 7.4.26
- doctrine:orm: 2.13.3
- doctrine/annotations: 1.13.3
- doctrine/doctrine-bundle: 2.7.0
- doctrine/doctrine-migrations-bundle: 3.2.2
- symfony/doctrine-bridge: 5.4.14
- doctrine/data-fixtures: 1.5.3
2
Answers
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 answers to the question "Ignore a Doctrine2 Entity when running schema-manager update" there are 3 valid options to ignore the entity from schema update.
Full explanation :
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 where you do not want doctrine to generate a query like this each time you want to update your 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 show
database is not in sync
error.Once you put it in schema_filter, the error won’t occur anymore.
It work for
doctrine:migration:diff
anddoctrine:schema:update
But if you want to avoid an entity to be generated inside the database there is this from the link in the answer of Ernesto:
Only working since Doctrine 2.7 version.
You can find the complete example here : Ignore a Doctrine2 Entity when running schema-manager update
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
And also; Sorry for the very short answered i did before haha 🙂