-
-
Notifications
You must be signed in to change notification settings - Fork 9.6k
[PropertyAccess] Issue with findAdderAndRemover() #17907
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
Comments
Can you provide a concrete example please? Not sure I understand what you mean. |
You can reproduce it with a single entity. If it has addX() method, but doesn't have removeX(), then you will get this exception when trying setValue() with an array. It's because finAdderAndRemover() searches for both addX and removeX. You have to ipmlement both of them. If the decision is that this is not a bug, then at least we should mention this in the propertyaccessor documentation. Actually I can't find anything about adders and removers in the docs: |
@paradajozsef I was trying to look into this in order to fix it. It does not return an exception in master, I've tried with a Test class, it does return NULL when I have addX but not removeX. If the property of the class does not exist or is not the same as what you pass in It is specific to a Symfony version ? ping @xabbuh. |
@Simperfit In your example, is the property accessible through some other method (a setter) or is it public? |
ping @webmozart What do you think about this? Should we treat it as a documentation issue and improve the docs or is this to be considered a real issue (looking at the comments here I tend to treat the current behaviour as intended)? |
@xabbuh It was public, i've been trying to add a phpunit test for thatn but it passed. Then I've done it with 2 dummy entity and I couldn't reproduce it. I've tested in master, not in 2.3 |
@Simperfit If the property is public, the property accessor will always fall back to access it directly. Looking at the code it doesn't seem that it changed between |
Oh, sorry @xabbuh didn't understand that like that. Now that it is private I've the exception. In Master the comments you've mentioned does not exist any more and this part neither. https://github.com/symfony/symfony/blob/2.3/src/Symfony/Component/PropertyAccess/PropertyAccessor.php#L674 The Exception is thrown by https://github.com/symfony/symfony/blob/master/src/Symfony/Component/PropertyAccess/PropertyAccessor.php#L717 |
…writable property (pierredup) This PR was submitted for the master branch but it was merged into the 4.4 branch instead (closes #31194). Discussion ---------- [PropertyAccess] Improve errors when trying to find a writable property | Q | A | ------------- | --- | Branch? | master | Bug fix? | no | New feature? | yes | BC breaks? | no | Deprecations? | no | Tests pass? | yes | Fixed tickets | #17907 | License | MIT | Doc PR | N/A When setting a property using an adder/remove, the error message is very generic if the methods don't fit the exact requirements (both the adder and remover need to be defined and accept at least one argument). This can be confusing when you already have the methods `addFoo()` and `removeFoo()` defined (but without any parameters in the signature), but the error message states that the method doesn't exist or don't have public access. So this PR tries to improve the error message if a property isn't writable by doing the following: * If only one of the add/remove methods is implemented, indicate that the other method is needed as well. * If any of the adder/remover, setter or magic methods (`__call` or `__set`) don't have the required number of parameters, make it clear that the methods need to define the correct number of parameter. * The any of the access methods were found, but don't have public access, make it clear that the method needs to be defined as public, ```php class Foo { public function addBar($value) { } public function removeBar() { } } ``` **Before:** ``` Neither the property "bar" nor one of the methods "addBar()/removeBar()", "setBar()", "bar()", "__set()" or "__call()" exist and have public access in class "Foo". ``` **After:** ``` The method "removeBar" requires at least "1" parameters, "0" found. ``` Commits ------- f90a9fd Improve errors when trying to find a writable property
If you have an entity A with a one-to-many collection to entity B and you use a form for entity A with the option
allow_add
for the collection you can have the following issue:If entity A only contains a methods
addB()
but doesn't containremoveB()
thePropertyAccessor
doesn't return any method infindAdderAndRemover()
and then throws aNoSuchPropertyException
inwriteProperty()
.The text was updated successfully, but these errors were encountered: