Twig icon indicating copy to clipboard operation
Twig copied to clipboard

attribute() not looking for camelCase method

Open linuxprocess opened this issue 4 years ago • 10 comments

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

linuxprocess avatar Dec 13 '21 16:12 linuxprocess

The error you get is from the PropertyAccess component, not from Twig.

stof avatar Dec 13 '21 16:12 stof

and Twig supports using either of them for the getter

stof avatar Dec 13 '21 16:12 stof

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 ...

linuxprocess avatar Dec 13 '21 17:12 linuxprocess

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.

stof avatar Dec 13 '21 17:12 stof

Should not you reopen ?

linuxprocess avatar Dec 13 '21 17:12 linuxprocess

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 avatar Jun 18 '22 09:06 Cruiser13

@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 avatar Jun 19 '22 12:06 stof

@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.

Cruiser13 avatar Jun 20 '22 07:06 Cruiser13

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.

stof avatar Jun 20 '22 07:06 stof

This is happens to me as well, is there any way to make twig look for the camelCase getter instead?

nsetyo avatar Jun 14 '23 09:06 nsetyo