Skip to content

[make:entity] Generate a strict setter for non-nullable unidirectional ManyToOne relations#1797

Open
Amoifr wants to merge 1 commit into
symfony:1.xfrom
Amoifr:feature/198-strict-manytoone-setter
Open

[make:entity] Generate a strict setter for non-nullable unidirectional ManyToOne relations#1797
Amoifr wants to merge 1 commit into
symfony:1.xfrom
Amoifr:feature/198-strict-manytoone-setter

Conversation

@Amoifr

@Amoifr Amoifr commented Jun 18, 2026

Copy link
Copy Markdown
Contributor

Summary

Concrete design proposal for #198, which reports that generated ManyToOne setters accept null even when the relation is non-nullable (setCategory(?Category $category) for a nullable: false association).

The discussion stalled on a real constraint: the setter must stay nullable for bidirectional relations, because the collection remove*() generated on the inverse side sets the owning side back to null ($item->setOwner(null), orphanRemoval) — making every ManyToOne setter strict would break that generated code.

This PR tightens only the provably-safe subset: a non-nullable, unidirectional ManyToOne, which has no such remove*() null path:

public function setCategory(Category $category): static  // was: ?Category

Bidirectional and explicitly-nullable relations are unchanged. This mirrors what already happens for non-nullable OneToOne setters ($relation->isNullable()), just extended to the ManyToOne case that is provably safe.

Scope / open questions

  • Deliberately setter-only (the original report). The backing property stays ?Category $category = null — consistent with the current OneToOne behavior, and it avoids uninitialized-typed-property pitfalls with Forms.
  • Also making the property non-nullable (raised again recently re: phpstan-doctrine doctrine.associationType) is a bigger discussion — it reopens @javiereguiluz's Forms concern about uninitialized properties — so it's left out here. Happy to follow up if you'd like to go there.

No existing fixtures change (no current test case is non-nullable + unidirectional); a new many_to_one_not_nullable_no_inverse case + fixture covers it.

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.

1 participant