TYPO3领域模型能否使用下划线?属性命名报错问题咨询
Hey, let's break down your problem with underscore-named properties in Extbase domain models and figure out the best fix for you.
What's Going Wrong?
You tried using an underscore in your property name ($operating_abc) and hit this error:
Uncaught TYPO3 Exception Cannot access protected property Vendor\Mymodel\Domain\Model\Mymodel::$operating_abc
But when you switched to camelCase ($operatingAbc), everything worked fine. You want to keep the database column as operating_abc and also see that exact key name in your output—totally understandable.
Why This Happens
Extbase’s property handling is tied strictly to PHP’s camelCase conventions. When you use an underscore in a property name, Extbase’s reflection system tries to map it using its default rules, but your getters/setters (getOperatingAbc()) are written for a camelCase property. This mismatch means Extbase tries to directly access the protected $operating_abc property without a valid getter/setter, hence the permission error.
Also, here’s a key point: Extbase automatically maps camelCase properties to snake_case database columns. So $operatingAbc will already link to your operating_abc column out of the box—you don’t need to do extra configuration for that part.
Solutions to Try
Option 1: Stick with CamelCase (Recommended)
Since Extbase already handles the database column mapping for you, the simplest fix is to keep using $operatingAbc in your model, and adjust your output to use the snake_case key when needed. For example, in your controller or when preparing data for display:
$model = $this->mymodelRepository->findByUid(2); $displayData = [ 'operating_abc' => $model->getOperatingAbc(), // Add other properties here ]; // Output $displayData will show 'operating_abc' as the key
This follows Extbase’s best practices and avoids any weird edge cases with property mapping.
Option 2: Force Underscore Property (Not Recommended)
If you really need to use $operating_abc in your model, you’ll need to align your getters/setters and explicitly configure the property-to-column mapping:
First, update your model to match the underscore naming in getters/setters:
/** * Mymodel */ class Mymodel extends \TYPO3\CMS\Extbase\DomainObject\AbstractEntity { /** * operating_abc * * @var string */ protected $operating_abc = ''; /** * Returns the operating_abc * * @return string $operating_abc */ public function getOperating_abc() { return $this->operating_abc; } /** * Sets the operating_abc * * @param string $operating_abc * @return void */ public function setOperating_abc($operating_abc) { $this->operating_abc = $operating_abc; } }
Then, add explicit mapping in your extension’s Configuration/Extbase/Persistence/Classes.php:
return [ Vendor\Mymodel\Domain\Model\Mymodel::class => [ 'properties' => [ 'operating_abc' => [ 'fieldName' => 'operating_abc' ] ] ] ];
Again, this goes against Extbase’s conventions, so you might run into unexpected issues down the line (like problems with form binding, serialization, etc.).
Final Takeaway
Sticking with camelCase properties is the way to go here—Extbase is designed to work that way, and it already handles the database column conversion for you. Just adjust your output layer to use snake_case keys if needed, and you’ll have a clean, maintainable solution.
内容的提问来源于stack exchange,提问作者Philipp M

