Thanks to visit codestin.com
Credit goes to github.com

Skip to content

[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

Closed
tobske opened this issue Feb 23, 2016 · 8 comments
Closed

[PropertyAccess] Issue with findAdderAndRemover() #17907

tobske opened this issue Feb 23, 2016 · 8 comments

Comments

@tobske
Copy link

tobske commented Feb 23, 2016

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 contain removeB() the PropertyAccessor doesn't return any method in findAdderAndRemover() and then throws a NoSuchPropertyException in writeProperty().

@javiereguiluz javiereguiluz changed the title [PropertyAccess] [PropertyAccess] Issue with findAdderAndRemover() Feb 23, 2016
@xabbuh
Copy link
Member

xabbuh commented Feb 23, 2016

Can you provide a concrete example please? Not sure I understand what you mean.

@paradajozsef
Copy link
Contributor

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:
http://symfony.com/doc/current/components/property_access/introduction.html

@Simperfit
Copy link
Contributor

@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 insetValue(), it does Throw an exception in getWriteAccessInfo.

It is specific to a Symfony version ?

ping @xabbuh.

@xabbuh
Copy link
Member

xabbuh commented Apr 3, 2016

@Simperfit In your example, is the property accessible through some other method (a setter) or is it public?

@xabbuh
Copy link
Member

xabbuh commented Apr 3, 2016

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)?

@Simperfit
Copy link
Contributor

@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

@xabbuh
Copy link
Member

xabbuh commented Apr 3, 2016

@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 2.3 and master.

@Simperfit
Copy link
Contributor

Oh, sorry @xabbuh didn't understand that like that.

Now that it is private I've the exception.
Neither the property "test" nor one of the methods "addTest()"/"removeTest()", "setTest()", "test()", "__set()" or "__call()" exist and have public access in class "Symfony\Component\PropertyAccess\Tests\Fixtures\TestClassWithAddWithoutRemove".

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
https://github.com/symfony/symfony/blob/master/src/Symfony/Component/PropertyAccess/PropertyAccessor.php#L790

The Exception is thrown by https://github.com/symfony/symfony/blob/master/src/Symfony/Component/PropertyAccess/PropertyAccessor.php#L717

@fabpot fabpot closed this as completed Jul 15, 2019
fabpot added a commit that referenced this issue Jul 15, 2019
…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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

7 participants