-
Notifications
You must be signed in to change notification settings - Fork 2.5k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[RFC] Read nullability from PHP type #9744
Comments
The problems we face are:
It would be technically feasible to use null or not null as the default for |
Well, when someone updates from 2.0 to 3.0, BC breaks are expected, I bet that most people will have not a single line in migration, since now they are forced to declare nullability via attribute, it would mean that they have nullable property and non-nullable column in database, what would result in error when saving entity with null value, so they will have maybe few lines in migration and they check the entity properties, figure out only they are nullable, and therefore they add In conclusion, that migration will probably help them discover bugs. Another deprecating solution is to add deprecated yellow warnings when generating migrations or orm schema update, that there are entity properties that are nullable but database nullability doesn't match, so they should add I think that But on the other hand, if you make I personally wouldn't have problem with relations being non-nullable, it would bring more consistency and less bugs, since now if I set somehow column in db to be null, ORM will probably scream that there is non-nullable variable and null was passed to that variable. Another plus is that there can't be an empty value of |
Yes, but they have to be prepared. This means that there has to be some way to opt-in to the new behavior before it becomes mandatory with the major release. There are still applications out there using Zend Framework 1, Symfony 1 and/or Doctrine 1 because the upgrade path of each of those libraries to version 2 had been too difficult. The PHP ecosystem has learnt from its mistakes.
You will lose that bet. A newly created entity does not necessarily need to be ready to be persited right away. I have seen quite a few codebases in the past where Entities were created like this: $entity = (new MyEntity())
->setMandatoryFieldA('some value')
->setMandatoryFieldB('some value')
->setMandatoryFieldC('some value')
; In that case, This is probably not an approach you would take and I respect that. But that does not render this approach invalid. A special case of that are generated primary keys like this one: #[Column, Id, GeneratedValue]
private ?int $id = null; Here, the database column must not be nullable and an entity with So to conclude: even if we decide to guess nullability from the property type in 3.0, a proper deprecation layer has to be in place that notifies the developer that his current mapping configuration will trigger different behavior in 3.0. And here, the change is especially tricky because you want to change the semantics of the omission of the
That might be a nice bonus, but using Doctrine Migrations is completely optional which is why our deprecation layer must not depend on it.
Taking into account the type of codebase I mentioned earlier, adding this kind of nullability guessing to relations is not particularly heavier than for regular columns. |
That special cases with
That's sounds good, if there was option in configuration, for example |
Hi @derrabus, do you think it's reasonable to add this at least as opt-in in configuration? It would help a lot to have cleaner entity properties, that's one of the few things why do I have something in JoinColumn or Column attribute. Regarding that |
We can do that, yes. But someone would need to work on the feature and apparently nobody did that during the last three years. |
It has already been done, but it was reverted #8732, I tried to argue that we could have it at least opt in, so if it's possible, I'll just write same implementation when I'll find some free time, I'll just add configuration support and default option will be current behavior. |
Feature Request
I would like to have nullable columns according to my PHP nullable properties, when column property is nullable, column should be also nullable, if there is no nullable attribute that says otherwise, I checked my entities in multiple big projects, and I have 1:1 nullable database values with nullable PHP properties.
This code should result in column being nullable in database, if there was declared
#[Column(nullable: false)]
, column would not be nullable, but PHP variable can be, what is really bad practice, since entity can get into invalid state, and if it will be saved, application runs into an error. I thought about it, and I can't come up with one example where it's good to have nullable field in PHP, but not in database.Previous discussions
This matter was discussed multiple times, once, this feature it was shipped into the
2.9.1
version, so some programmers (#8770, #8834) (me including) have removednullable
property from Column annotation, in version2.9.2
it was reverted (#8732) and a BC break was introduced. I doubt there was many people that had different nullability in PHP.Currently it's stated that it is not in
2.x
because of backward compatibility (#8835), and main issue that caused rollback of the feature was this #8723 (and I think that only that one issue). And in that issue it was stated by @beberlei #8723 (comment) that you can look at it in ORM 3, what would be great, but then I asked about it recently, and got answer #9736 (comment)So, I'm curious based on what did you decide to not inherit nullability from PHP types?
Motivation
My vision is to have clean code, less bugs and less repeating declarations, ORM has been been going this way last couple of years.
For instance:
#[Column]
attribute.#[Column]
, not even enumType, since it's inherited from PHP type#[Embedded]
targetEntity
in ManyToOne relationships, it's inherited from PHP type.Also it enforces entity valid state, because if PHP value could be null and if there was non-null column in database, after flushing the entity there will be a SQL error. So there is that.
And if someone want nullable type to be non-nullable in database, they can simply declare
nullable: false
in#[Column]
attribute.Another thing is, that happened to me a few times, that I totally forgot to declare nullable columns in annotations additionally, and I discovered it after I wanted to save entity, that PHP values could be nullable, but database columns could not.
Here is an example of that mentioned code where I needed to add additional

nullable
attribute and how bad it looksAnd here is same code without that attribute nullability declarations

The text was updated successfully, but these errors were encountered: