Francisc avatar Francisc 2 months ago

why is it that the entity class allows for null values (at the php level) when during the make:entity process the columns were specified as not null?

| Reply |

Hey @Francisc,

This is a great question. The answer is validation. The current suggested way to add validation to entities is to define your constraints directly on your entity's properties. For the validation component to work correctly, these properties need to be nullable.

I admit, this is unfortunate. I dislike the possibility of my entities being in an "invalid state" (a state that, when persisted, gives a database error). The current workaround is to use a separate DTO object that contains your validation constraints. Once that is valid, map this to your entity. Then, you likely need a way to map your entity back to the DTO for doing entity edits. It gets cumbersome...

The good news is there's some efforts into making this easier. Symfony 7.3 added an Object Mapper component that helps with the DTO <-> Entity conversion.

I'm hoping sometime in the future, we can change the current recommendation.

--Kevin

1 | Reply |

why is there a disconnect between the database configs for column and their corresponding class properties?

| Reply |