attribute() not looking for camelCase method
Hi, I think it's a sort of bug, but it may be classified as a whish.
I'm using Symfony. I've added the non public attribute $event_login to an entity class. I meet a twig error when using {{ attribute(item, field) }} : Neither the property "event_login" nor one of the methods "event_login()", "getevent_login()"/"isevent_login()"/"hasevent_login()" or "__call()" exist and have public access in class "App\Entity\Event". So I add the getevent_login() getter ... Then I meet Symfony\Component\PropertyAccess\Exception\NoSuchPropertyException : Can't get a way to read the property "event_login" in class "App\Entity\Event". So I add the getEventLogin() getter ...
Why cannot they use the same getter ?
After a few exchanges, man states that a method should always been camelCase() (see https://www.php-fig.org/psr/psr-1/ ). Note that using underscores in attributes is allowed. That's why PropertyAccess use getEventLogin() ...
So, why does the twig attribute() function use getevent_login() instead of getEventLogin() ?
Should not it at least also check for getEventLogin() existence ?
Regards
The error you get is from the PropertyAccess component, not from Twig.
and Twig supports using either of them for the getter
Just to be sure : you're saying the error Neither the property event_login" nor one of the methods "event_login()", "getevent_login()"/"isevent_login()"/"hasevent_login()" or "__call()" exist and have public access in class "App\Entity\Event". is not a twig error ? It's comming from vendor/twig/twig/src/Extension/CoreExtension.php ...
ah no indeed. Your post contains 2 error messages, not 1. And I was wrong when thinking that Twig converts snake case names into camel case ones for getters.
Should not you reopen ?
It looks as if the attribute function is indeed broken here (tested on 3.4.1). I do have an entity "Inhabitant" which has a "getPerson()" method which does have a "getName()" method. Sample code:
{% set check = 'person.name' %}
{{ attribute(targetEntity, check) }}
{{ targetEntity.person.name }}
Running second line will give me the following error:
Neither the property "person.name" nor one of the methods "person.name()", "getperson.name()" "isperson.name()" "hasperson.name()" or "__call()" exist and have public access in class "App\Entity\Inhabitant".
But if I comment out the second line, the third line will properly give me a name.
@Cruiser13 you are trying to access the person.name attribute here, which is not something like getPerson will return. To access nested objects, you need multiple attribute calls.
@stof actually the attribute() function should work the same as the dot notation, right? Atleast it says that in the docs note. And the dot notation (in the third line) does properly return the name. Using symfony I can also use $propertyAccessor->getValue(targetEntity.person.name) and this will work fine.
The attribute function works the same than the . operator. But the dot operator does not deal with nested properties either (for that, you use several dot operators, just like you need several attribute calls).
And the attribute function is not the same thing that the Symfony PropertyAccess component.
This is happens to me as well, is there any way to make twig look for the camelCase getter instead?