phpstan-doctrine
phpstan-doctrine copied to clipboard
Check doctrine entities mapping is in sync with the database
We have an old legacy project where we use sql first approach to define the tables and then create the entities for them. I'm wondering if phpstan can help us by checking if the annotations in entities match those in the database.
Example:
invoiceNo is defined as nullable in the database, but in the entity it's defined as not null.
#[Column(type: Types::STRING, name: 'invoiceNo', nullable: false)]
By matching the doctrine annotations with the actual types in the database we can then auto generate our php types as well and detect any mismatch.
I realize this needs access to the database schema, which https://github.com/staabm/phpstan-dba does, is that something that can be done in this project or better done somewhere else
Hi, AFAIK you can use some built-in Doctrine command for this?
There is doctrine:schema:validate which checks the annotations and syncing, but it cannot be filtered to just a few entities. It works on the whole database.
And why would you want to filter only for select entities?
When using sql first migrations, especially in a legacy project, it is nice to check if the entity matches the database table.
What schema:validate does is it checks if schema defined in entities is the same as on the database including foreign key names, indexes etc which is usually not the case for legacy projects.
I just want a way to check if i have mapped my entity correctly to the table.
But there could still be existing errors even for those entities you want checked, right? To me it sounds like you want the baseline functionality (https://phpstan.org/user-guide/baseline) for the schema:validate command, which you need to build yourself.
Yes, i do want all entities which are mapped to be checked with their respective sql table's , but only the types not the indexes/foreign key names etc