-
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
[GH-8471] Undeprecate PARTIAL for objects in DQL #11653
Merged
greg0ire
merged 12 commits into
doctrine:3.3.x
from
beberlei:GH-8471-RevertPartialObjects2
Oct 12, 2024
Merged
Changes from all commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
4a3c7f0
Revert "Remove unused exception"
beberlei f717255
Revert undprecate PARTIAL for objects in DQL.
beberlei 7ef1f0a
Phpcs fixes
beberlei 5f4ecfd
Fix imports
beberlei da7854f
Fix imports
beberlei f5fb400
Address review comments.
beberlei e2b971d
Merge branch '3.3.x' into GH-8471-RevertPartialObjects2
beberlei 516b593
Fix phpcs
beberlei c19afa1
Merge 3.3.x
beberlei c7e5605
Fix test
beberlei 7c9b742
fix phpbench tests.
beberlei cf8f5f9
Apply suggestions from code review
beberlei File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,88 @@ | ||
Partial Objects | ||
=============== | ||
|
||
A partial object is an object whose state is not fully initialized | ||
after being reconstituted from the database and that is | ||
disconnected from the rest of its data. The following section will | ||
describe why partial objects are problematic and what the approach | ||
of Doctrine to this problem is. | ||
|
||
.. note:: | ||
|
||
The partial object problem in general does not apply to | ||
methods or queries where you do not retrieve the query result as | ||
objects. Examples are: ``Query#getArrayResult()``, | ||
``Query#getScalarResult()``, ``Query#getSingleScalarResult()``, | ||
etc. | ||
|
||
.. warning:: | ||
|
||
Use of partial objects is tricky. Fields that are not retrieved | ||
from the database will not be updated by the UnitOfWork even if they | ||
get changed in your objects. You can only promote a partial object | ||
to a fully-loaded object by calling ``EntityManager#refresh()`` | ||
or a DQL query with the refresh flag. | ||
|
||
|
||
What is the problem? | ||
-------------------- | ||
|
||
In short, partial objects are problematic because they are usually | ||
objects with broken invariants. As such, code that uses these | ||
partial objects tends to be very fragile and either needs to "know" | ||
which fields or methods can be safely accessed or add checks around | ||
every field access or method invocation. The same holds true for | ||
the internals, i.e. the method implementations, of such objects. | ||
You usually simply assume the state you need in the method is | ||
available, after all you properly constructed this object before | ||
you pushed it into the database, right? These blind assumptions can | ||
quickly lead to null reference errors when working with such | ||
partial objects. | ||
|
||
It gets worse with the scenario of an optional association (0..1 to | ||
1). When the associated field is NULL, you don't know whether this | ||
object does not have an associated object or whether it was simply | ||
not loaded when the owning object was loaded from the database. | ||
|
||
These are reasons why many ORMs do not allow partial objects at all | ||
and instead you always have to load an object with all its fields | ||
(associations being proxied). One secure way to allow partial | ||
objects is if the programming language/platform allows the ORM tool | ||
to hook deeply into the object and instrument it in such a way that | ||
individual fields (not only associations) can be loaded lazily on | ||
first access. This is possible in Java, for example, through | ||
bytecode instrumentation. In PHP though this is not possible, so | ||
there is no way to have "secure" partial objects in an ORM with | ||
transparent persistence. | ||
|
||
Doctrine, by default, does not allow partial objects. That means, | ||
any query that only selects partial object data and wants to | ||
retrieve the result as objects (i.e. ``Query#getResult()``) will | ||
raise an exception telling you that partial objects are dangerous. | ||
If you want to force a query to return you partial objects, | ||
possibly as a performance tweak, you can use the ``partial`` | ||
keyword as follows: | ||
|
||
.. code-block:: php | ||
|
||
<?php | ||
$q = $em->createQuery("select partial u.{id,name} from MyApp\Domain\User u"); | ||
|
||
You can also get a partial reference instead of a proxy reference by | ||
calling: | ||
|
||
.. code-block:: php | ||
|
||
<?php | ||
$reference = $em->getPartialReference('MyApp\Domain\User', 1); | ||
|
||
Partial references are objects with only the identifiers set as they | ||
are passed to the second argument of the ``getPartialReference()`` method. | ||
All other fields are null. | ||
|
||
When should I force partial objects? | ||
------------------------------------ | ||
|
||
Mainly for optimization purposes, but be careful of premature | ||
optimization as partial objects lead to potentially more fragile | ||
code. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is this sentence still true? Or should we say it's not implemented yet?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Its still true until we get lazy objects.