-
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
Support readonly properties for read operations #9316
Support readonly properties for read operations #9316
Conversation
CodeSniffer failure should be fixed by #9317 |
The RFC states about Reflection and
The problem here I believe is that "simple IDs" are already populated in proxies, and then when loading the entity the ID is set again, triggering this error from The workaorund could be for now to avoid using |
Why's that? Could we just not set the ID again when resolving the proxy? It should not change anyway, should it?
The test assumes that the entity model is used for read operations only. And in that case, |
13244f6
to
f031be5
Compare
I've pushed a commit that fixes the issue. Can you have a look? |
f031be5
to
2ef01eb
Compare
This is the hot code path, this change is probably too expensive since it affects every property. I would imagine again we need a special reflection instance here |
2ef01eb
to
e800bf5
Compare
Do you already have an idea how to implement that "special reflection instance"? I'm a bit lost here, I'm afraid. |
@derrabus from what I understood it would look like so: <?php
declare(strict_types=1);
namespace Doctrine\ORM\Mapping;
use LogicException;
use ReflectionProperty;
use function sprintf;
class ReflectionReadOnlyProperty extends ReflectionProperty
{
/** @var ReflectionProperty */
private $originalReflectionProperty;
public function __construct(ReflectionProperty $originalReflectionProperty)
{
$this->originalReflectionProperty = $originalReflectionProperty;
}
/**
* @param object $object
* @param mixed $value
*/
public function setValue($object, $value = null): void
{
if ($this->originalReflectionProperty->getValue($object) === $value) {
return;
}
throw new LogicException(sprintf(
'Attempting to change readonly field %s.',
$this->originalReflectionProperty->getName()
));
}
} Then, in if ($this->reflFields[$field]->isReadOnly()) {
$this->reflField[$field] = new ReflectionReadOnlyProperty($this->reflFields[$field])];
} where appropriate |
7d5f432
to
5923e57
Compare
I think I got it right now. 😓 |
5923e57
to
9c4c171
Compare
final class ReflectionReadonlyProperty extends ReflectionProperty | ||
{ | ||
public function __construct( | ||
private ReflectionProperty $wrappedProperty |
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.
Oh yeah 😎
psalm.xml
Outdated
@@ -75,6 +75,12 @@ | |||
<referencedClass name="Doctrine\DBAL\Platforms\PostgreSQLPlatform" /> | |||
</errorLevel> | |||
</InvalidClass> | |||
<MethodSignatureMismatch> | |||
<errorLevel type="suppress"> | |||
<!-- Psalm is wrong about ReflectionProperty signatures. --> |
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 there a github issue to track this?
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.
I haven't researched that 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.
I just filed vimeo/psalm#7357
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.
Great! I also observed this locally: https://psalm.dev/r/5c7d43694a , but maybe that's because I'm using 8.0 🤔
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.
Yes, that error goes away if you run Psalm with PHP 8.1.
9c4c171
to
5816424
Compare
5816424
to
9696bd3
Compare
* 2.11.x: Leverage generic ObjectManagerDecorator (doctrine#9312) Fix WhereInWalker description to better describe the behaviour of this class (doctrine#9268) Regenerate Psalm baseline Update Psalm baseline for Persistence 2.3 (doctrine#9349) Support readonly properties for read operations (doctrine#9316)
This PR provides some testcases to check if the ORM handles
readonly
properties well for read operations. So far, it looks good except for lazy-loadedManyToOne
relations. Here, the proxy tries to unsetreadonly
properties and fails.