Skip to content
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

[GH-8723] Remove use of nullability to automatically detect nullable status #8732

Merged
merged 2 commits into from
May 31, 2021

Conversation

beberlei
Copy link
Member

The new typed properties support has a flaw around the the concept of nullable. Setting a type in the entity to be or not be nullable does not necessarily mean the databsae column has to be the same. They can differ from each other.

As such it is better to remove the the use of allowsNull() as the default value for a column's or joincolumn's nullable flag.

This supersedes #8726

Fixes #8723

@@ -1527,14 +1523,6 @@ private function validateAndCompleteTypedAssociationMapping(array $mapping): arr
$mapping['targetEntity'] = $type->getName();
}

if (isset($mapping['joinColumns'])) {
foreach ($mapping['joinColumns'] as &$joinColumn) {
if ($type->allowsNull() === false) {
Copy link

@zolex zolex May 31, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

instead of removing it, do we maybe want the same logic as for $mapping['nullable']?
e.g. if (! isset($joinColumn['nullable'])) $joinColumn['nullable'] = $type->allowsNull();

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No, tthe column nullable behavior is also flawed, as there are legit cases where you want nullable behavior to be different between entity and database.

Copy link

@zolex zolex May 31, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ah, at least for me the bug was only that I could not set the value at all in the JoinColumn annotation. it was always false even when providing it explicitly true.

but alright, I see setting the default based on the php type may be a bc where it is not defined in the annotation. so maybe this is really something for orm 3?

@beberlei beberlei merged commit d9e59d6 into doctrine:2.9.x May 31, 2021
@beberlei beberlei deleted the GH-8723-RemoveNullableDetection branch May 31, 2021 08:19
@beberlei beberlei added this to the 2.9.2 milestone May 31, 2021
beberlei added a commit that referenced this pull request May 31, 2021
* Mark 2.8.x as unmaintained, and 2.9.x as current

* Fix ClassMetadataInfo template inference

* Fix metadata cache compatibility layer

* Bump doctrine/cache patch dependency to fix build with lowest deps

* Add generics to parameters

* Add note about performance and inheritance mapping (#8704)

Co-authored-by: Claudio Zizza <[email protected]>

* Adapt flush($argument) in documentation as it's deprecated. (#8728)

* [GH-8723] Remove use of nullability to automatically detect nullable status (#8732)

* [GH-8723] Remove use of nullability to automatically detect nullable status.

* [GH-8723] Make Column::$nullable default to false again, fix tests.

* Add automatic type detection for Embedded. (#8724)

* Add automatic type detection for Embedded.

* Inline statement.

Co-authored-by: Benjamin Eberlei <[email protected]>

Co-authored-by: Grégoire Paris <[email protected]>
Co-authored-by: Vincent Langlet <[email protected]>
Co-authored-by: Andreas Braun <[email protected]>
Co-authored-by: Fran Moreno <[email protected]>
Co-authored-by: Juan Iglesias <[email protected]>
Co-authored-by: Claudio Zizza <[email protected]>
Co-authored-by: Yup <[email protected]>
Co-authored-by: Benjamin Eberlei <[email protected]>
@BusterNeece
Copy link

A question as a user: why is it preferable to remove this as a default instead of simply allowing the user to override the nullable status whenever it misaligns with the property type?

I considered the alignment with property type to be a hugely positive addition alongside the support for attributes, since it made it possible for, in a majority of cases, a simple variable declaration and single attribute to be all that is required to have a database column that corresponds intuitively to that property. That included the nullability of that property.

The changelog suggests that this fixes a bug with joinColumn nullability; rather than simply not depending on the nullable status at all, is there another way to resolve this problem?

@zolex
Copy link

zolex commented May 31, 2021

A question as a user: why is it preferable to remove this as a default instead of simply allowing the user to override the nullable status whenever it misaligns with the property type?

because in an existing application that has an entity with a nullable typed php property and nullable not defined in the annotation/attribute, it changes the behavior, which is a breaking change. according to semantic versioning a bc is only allowed in a major version increment, so handling it strictly, it can not be in orm 2.x versions.

let's hope we get this in 3.0, basically it's a good idea :)

@Slamdunk
Copy link
Contributor

Slamdunk commented Jun 1, 2021

@zolex sounds right, but god it feels so weird to revert everything I did alongside updating to 2.9.0, it helped me to uncover a lot of unaligned types

@BusterNeece
Copy link

Same; in upgrading to 2.9.0 I took all the nullable specifications out in favor of the native PHP types, which helped me find numerous instances (particularly on ManyToOne joins) where nullability didn't match my expectations and I had to migrate the database to make the two match.

It's definitely a step back in the name of compatibility, but I would love to see it be something that's configurable to use now.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Upgrade from 2.8.x to 2.9.1: possible BC break for nullable behaviour
4 participants