Search code examples
typo3extbasetypo3-8.xtypo3-extensions

TYPO3 - Using underscore in domain model


Is it allowed to use 'underscore' '_' in domain model? This does not seem to work?

/**
 * 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 getOperatingAbc() {
        return $this->operating_abc;
    }

    /**
     * Sets the operating_abc
     *
     * @param string $operating_abc
     * @return void
     */
    public function setOperatingAbc($operating_abc) {
        $this->operating_abc = $operating_abc;
    }   

}

I get this Error:

Uncaught TYPO3 Exception Cannot access protected property Vendor\Mymodel\Domain\Model\Mymodel::$operating_abc

This is working though:

/**
 * Mymodel
 */
class Mymodel extends \TYPO3\CMS\Extbase\DomainObject\AbstractEntity {

    /**
     * operatingAbc
     *
     * @var string
     */
    protected $operatingAbc = '';           

    /**
     * Returns the operatingAbc
     *
     * @return string $operatingAbc
     */
    public function getOperatingAbc() {
        return $this->operatingAbc;
    }

    /**
     * Sets the operatingAbc
     *
     * @param string $operatingAbc
     * @return void
     */
    public function setOperatingAbc($operatingAbc) {
        $this->operatingAbc = $operatingAbc;
    }   

}

Output:

Vendor\Mymodel\Domain\Model\Mymodel prototypepersistent entity (uid=2, pid=0)
   operatingAbc => protected 'TWRXT' (4 chars)
   uid => protected 2 (integer)
   _localizedUid => protected 2 (integer)modified
   _languageUid => protected 0 (integer)modified
   _versionedUid => protected 2 (integer)modified
   pid => protected 0 (integer)

The column name is: operating_abc

And in the output I would like to get 'operating_abc' too.


Solution

  • It is in fact possible but you must ensure that at least your setter follows the name. Thus it must be named setOperating_abc.

    In general you should use the usual lowerCamelCase convention, especially since you get automatic mapping to DB fields in snail_case for free. If you don't do this, you must add a mapping from DB to property like this:

    plugin.tx_myextension {
        persistence {
            classes {
                MyVendor\MyExtension\Domain\Model\MyModel {
                    mapping {
                        columns {
                            operating_abc.mapOnProperty = operating_abc
                        }
                    }
                }
            }
        }
    }
    

    Otherwise Extbase will convert operating_abc to operatingAbc internally and look for a property named like this in the model.