Philipp M
Philipp M

Reputation: 3488

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.

Upvotes: 1

Views: 1127

Answers (3)

Lasse
Lasse

Reputation: 585

The question is not if it is possible but if it is allowed and according to the coding guidelines it´s NOT allowed.

All identifiers must use camelCase and start with a lowercase letter. Underscore characters are not allowed...

https://docs.typo3.org/typo3cms/CoreApiReference/latest/CodingGuidelines/PhpFileFormatting/PhpSyntaxFormatting.html#identifiers

It´s "just" guidelines but the entire TYPO3 core is designed or being refactored with this in mind and you will help youself a lot if you follow the guidelines. A lot of automation is happening in the background like the mapping from DB field to property name and you would safe yourself a lot of time just following the recommendations.

Bottom line: The question is not if you can, but if you should do it!

Upvotes: 3

Mathias Brodala
Mathias Brodala

Reputation: 6460

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.

Upvotes: 2

andreas
andreas

Reputation: 21

As far as i know, this is no problem "The column name is: operating_abc". Anyway you can user camelcase for set/get. I have several, where the column name ist called "fax_number" and then "setFaxNumber(), getFaxNumber()". That works.

Why to use the set/get with underscore? It is not consistent and will make a mess.

Upvotes: 0

Related Questions