-
-
Notifications
You must be signed in to change notification settings - Fork 1.3k
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
Setting the parent object when creating a new instance gives error with uninitialised property (PHP >=7.4) #7703
Comments
With your code, if I do Does Sonata is supposed to handle non valid code ? It's debatable... Having the stack trace could be interesting to know which call is failing.
Accessing the value is currently our way to know if the value is a OtO or OtM.
Here, calling |
Yes it is in the code of the method $value = $propertyAccessor->getValue($object, $parentAssociationMapping);
if (\is_array($value) || $value instanceof \ArrayAccess) {
$value[] = $parentObject;
$propertyAccessor->setValue($object, $parentAssociationMapping, $value);
} else {
$propertyAccessor->setValue($object, $parentAssociationMapping, $parentObject);
}
I think it is unfair to say my code is not valid. I think it is a perfectly fine use case.
Correct 😄 Except that the current value is not
True, but that is not the case I'm trying to cover. That's a different case. If you would write that as below, it also would still go wrong because class Child {
private ?Collection $parents = null;
public function getParents(): ?Collection { return $this->parents; }
public function setParents(Collection $parents): void { $this->parents = $parents; }
} The goal of the method is to set the parent object and within it is needs to determine whether or not it needs to set it or append it and it does so by getting the current value. The setting of the parent can go wrong in many ways because of "non valid code". I think within the means of the method to determine something it should prevent errors and/or cover possible use cases. In my code I use this on the other side of the relation btw. so in that case it still isn't nullable, but it is initialised with an empty collection:class ParentEntity {
/**
* @var Collection<int, Child>
* @ORM\OneToMany(targetEntity="App\Entity\Child", mappedBy="parent", cascade={"persist", "remove"}, orphanRemoval=true)
*/
public Collection $children;
public function __construct()
{
$this->children = new ArrayCollection();
}
/* public functions getChildren, setChildren, addChild, removeChild etc. */
} |
We're suppose to support every case, not just yours. What I meant is that, I'd like to avoid adding a fallback `If we cannot know if the data is a OtO or a OtM, we consider it's a OtO. But seems like it's already the behavior for
so we it's seems reasonable to keep this way. Feel free to open a PR to catch |
I've created pull request #7705 to fix this issue. |
Environment
PHP 7.4
Symfony 4.4
Sonata Admin 3
Sonata packages
show
Symfony packages
show
PHP version
Subject
When Sonata Admin is setting a parent object on a child object when creating a new instance of the child object, Symfony Property Access gives an
\Symfony\Component\PropertyAccess\Exception\AccessException
because of an uninitialised property when having a typed non-nullable uninitialised property.Steps to reproduce
I have a parent-child 1:n relation between two entities and also a parent and child admin configured. I recently updated my code to call
addChild()
on the parent admin with the second parameter set (to remove deprecations).The child entity needs to have a parent entity set, so I have written my PHP code as such:
Parent could be something like:
In admin services definition for the parent entity admin:
Actual results
Now when Sonata Admin is setting the parent object when creating a new instance, Symfony Property Access gives an error because of an uninitialised property:
Expected results
I do not want to make my properties nullable and initialise them with a
null
value. Neither do I want to initialise the property in the constructor with$this->parent = new ParentEntity();
.Sonata Admin should not need to get the property to set it or a least should not error out on this.
Possible solution
\Symfony\Component\PropertyAccess\Exception\AccessException
is only being thrown when the property cannot be read because it has not been initialised. We could wrap the specific line that is responsible for this error with atry { } catch(AccessException $e)
:The text was updated successfully, but these errors were encountered: