phpstan-doctrine icon indicating copy to clipboard operation
phpstan-doctrine copied to clipboard

Check doctrine entities mapping is in sync with the database

Open aszenz opened this issue 3 years ago • 6 comments

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

aszenz avatar Jul 06 '22 11:07 aszenz

Hi, AFAIK you can use some built-in Doctrine command for this?

ondrejmirtes avatar Jul 06 '22 11:07 ondrejmirtes

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.

aszenz avatar Jul 06 '22 13:07 aszenz

And why would you want to filter only for select entities?

ondrejmirtes avatar Sep 21 '22 11:09 ondrejmirtes

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.

aszenz avatar Sep 21 '22 13:09 aszenz

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.

ondrejmirtes avatar Sep 21 '22 13:09 ondrejmirtes

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

aszenz avatar Sep 21 '22 13:09 aszenz