doctrine/orm · error · InvalidArgumentException
Given property is not readonly.
Error message
Given property is not readonly.
What it means
ReflectionReadonlyProperty extends ReflectionProperty to allow exactly one initialization write to a readonly property (setValue passes through while uninitialized; a later differing write throws). Its constructor asserts the wrapped ReflectionProperty is readonly; LegacyReflectionFields only wraps properties where isReadOnly() already returned true, so this InvalidArgumentException signals direct misuse of the class.
Source
Thrown at src/Mapping/ReflectionReadonlyProperty.php:24
use InvalidArgumentException;
use LogicException;
use ReflectionProperty;
use function assert;
use function func_get_args;
use function func_num_args;
use function is_object;
use function sprintf;
/** @internal */
final class ReflectionReadonlyProperty extends ReflectionProperty
{
public function __construct(
private readonly ReflectionProperty $wrappedProperty,
) {
if (! $wrappedProperty->isReadOnly()) {
throw new InvalidArgumentException('Given property is not readonly.');
}
parent::__construct($wrappedProperty->class, $wrappedProperty->name);
}
public function getValue(object|null $object = null): mixed
{
return $this->wrappedProperty->getValue(...func_get_args());
}
public function setValue(mixed $objectOrValue, mixed $value = null): void
{
if (func_num_args() < 2 || $objectOrValue === null || ! $this->isInitialized($objectOrValue)) {
$this->wrappedProperty->setValue(...func_get_args());
return;
}
View on GitHub (pinned to d9b9ff7301)
Solutions
- Guard the wrapping: only construct ReflectionReadonlyProperty when $reflectionProperty->isReadOnly() is true.
- Reuse the library path (LegacyReflectionFields / getClassMetadata()->getReflectionProperty()) instead of constructing the wrapper yourself.
- If the property is intentionally mutable, use the plain ReflectionProperty.
Example fix
// before
$refl = new ReflectionReadonlyProperty($plainRefl); // $plainRefl not readonly -> InvalidArgumentException
// after
$refl = $plainRefl->isReadOnly()
? new ReflectionReadonlyProperty($plainRefl)
: $plainRefl; Defensive patterns
Strategy: validation
Validate before calling
$refl = $reflectionProperty->isReadOnly()
? new ReflectionReadonlyProperty($reflectionProperty)
: $reflectionProperty; Prevention
- Only wrap properties whose isReadOnly() is true.
- Use getClassMetadata()->getReflectionProperty() instead of constructing wrappers yourself.
- When removing readonly from a property, search for any saved/custom wrappers that reference it.
When it happens
Trigger: Directly calling new ReflectionReadonlyProperty($reflectionProperty) with a property that lacks the readonly modifier; keeping a previously-created wrapper after removing readonly from the property; custom reflection-service implementations wrapping properties unconditionally.
Common situations: Custom tooling copying LegacyReflectionFields::getAccessibleProperty() logic without the isReadOnly() branch; refactoring a property from readonly to mutable while cached/custom reflection wrappers persist.
Related errors
- %s::$%s must be readonly property
- The attribute "%s" is repeatable. Call getPropertyAttributeC
- The attribute "%s" is not repeatable. Call getPropertyAttrib
- Unknown field: {name} ::${field}
- Attempting to change readonly property %s::$%s.
AI-assisted analysis of doctrine/orm@d9b9ff7301 (2026-08-21).
Data as JSON: /api/errors/bb5b461b65ea645c.
Report an issue: GitHub.