DataObject getters and setters


#1

Is there a configuration setting to control camelCase / underscore when accessing DataObjects?

When I try to create a Symfony form bound to a Pimcore DataObject I get the following error:

Neither the property "foo_bar" nor one of the methods "getFooBar()", "fooBar()", "isFooBar()", "hasFooBar()", "__get()" exist and have public access in class "Pimcore\Model\DataObject\MyPimcoreDataObject".

In the generated DataObject class the getter is getFoo_bar() (not getFooBar())

It used to work but it looks like the variable declarations in generated DataObjects have changed from public to protected recently and this prevents Symfony’s PropertyAccessor finding them.

Is there any way to fix this other than removing all spaces from variable names?


#2

whats your property name? ‘foo_bar’? Then your form field should be named the same.


#3

The form field is also named foo_bar.

The problem is that Symfony’s property accessor (used in binding the DataObject and form) can only access the DataObject property if there is a getFooBar() method or the property has public access.

From Symfony docs:

The getValue() method also supports reading using getters. The method will be created using common naming conventions for getters. It camelizes the property name ( first_name becomes FirstName ) and prefixes it with get . So the actual method becomes getFirstName()

As Pimcore doesn’t camelize the property name in generated classes (it would be getFirst_name()) and the variable declaration is now protected the accessor can’t find it using either the getter or by accessing the variable directly.


#4

well, why don’t you just name your properties then fooBar?


#5

Renaming the properties works but effectively means underscores cannot be used in attribute names and breaks backwards compatibility with a lot of existing objects, which I was hoping to avoid.

I was hoping there was a config setting along the lines of doctrine’s

naming_strategy: doctrine.orm.naming_strategy.underscore.

I think it might be worth adding something to the documentation to say that using underscores in object property names will render them inaccessible to Symfony’s property accessor.

Other than renaming all I can think of is to create a parent class that includes magic __get() and __set() methods but I only use magic methods as a last resort.


#6

There is a CoreShop Class Definition Renamer Class that could help you. But be careful, that is still experimental.


#7

You can just include that by requiring the Pimcore Component

composer req coreshop/pimcore:^2.0

#8

Thanks - will have a look at that