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

Skip to content

PropertyAccessor::writeProperty() throws misleading NoSuchPropertyException #24742

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
d42ohpaz opened this issue Oct 29, 2017 · 3 comments
Closed

Comments

@d42ohpaz
Copy link

Q A
Bug report? yes
Feature request? no
BC Break report? no
RFC? no
Symfony version master

I had (incorrectly) architected myself into a situation where I have an entity without a setter for a property, and when trying to hydrate the entity from a FormType, I would get the following exception:

Could not determine access type for property "property" in class "AppBundle\Entity\MyEntity".

I'm not always a smart man, so it wasn't immediately obvious what that message meant. It took me dumping the contents of the $access array before it clicked:

/var/www/vendor/symfony/symfony/src/Symfony/Component/PropertyAccess/PropertyAccessor.php:653:
array(3) {
[0] =>
bool(true)
[1] =>
int(4)
[2] =>
string(190) "Neither the property "property" nor one of the methods "addProperty()"/"removeProperty()", "setProperty()", "property()", "__set()" or "__call()" exist and have public access in class "AppBundle\Entity\MyEntity"."
}

Long story short: I believe the NoSuchPropertyException that is thrown should include the $access[self::ACCESS_NAME] if it exists.

Thank you!

@xabbuh
Copy link
Member

xabbuh commented Nov 2, 2017

Can you be a bit more specific of what you would have liked to have instead? I am not sure if I completely got that.

@chalasr chalasr added Feature and removed Bug labels Nov 2, 2017
@d42ohpaz
Copy link
Author

d42ohpaz commented Nov 2, 2017

@xabbuh, essentially, I envision the exception appending the value of $access[self::ACCESS_NAME] to the existing message. e.g.,

Could not determine access type for property "property" in class "AppBundle\Entity\MyEntity": Neither the property "property" nor one of the methods "addProperty()"/"removeProperty()", "setProperty()", "property()", "__set()" or "__call()" exist and have public access in class "AppBundle\Entity\MyEntity"

So, the line of code might look like:

(formatted for readability)

throw new NoSuchPropertyException(sprintf(
    'Could not determine access type for property "%s" in class "%s"%s.',
    $property,
    get_class($object),
    isset($access[self::ACCESS_NAME]) ? ': ' . $access[self::ACCESS_NAME] : ''
));

@xabbuh
Copy link
Member

xabbuh commented Nov 2, 2017

Would you like to open a pull request with your suggestion?

fabpot added a commit that referenced this issue Dec 14, 2017
…yException Message (Simperfit)

This PR was merged into the 4.1-dev branch.

Discussion
----------

[PropertyAccess] add more information to NoSuchPropertyException Message

| Q             | A
| ------------- | ---
| Branch?       | 4.1
| Bug fix?      | no
| New feature?  | yes <!-- don't forget to update src/**/CHANGELOG.md files -->
| BC breaks?    | no
| Deprecations? | no <!-- don't forget to update UPGRADE-*.md files -->
| Tests pass?   | no
| Fixed tickets | #24742
| License       | MIT
| Doc PR        |

Add more context to NoSuchPropertyException as discussed in the issue.

Commits
-------

dee00f8 [PropertyAccess] add more information to NoSuchPropertyException Message
@fabpot fabpot closed this as completed Dec 14, 2017
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

6 participants