简体   繁体   中英

TYPO3 extension: attaching an object to an objectstorage throws a MySQL database error

One of my TYPO3 extension classes uses a property (checklist) that is an object storage.

class Kaufmnnisch extends \TYPO3\CMS\Extbase\DomainObject\AbstractEntity
{

    public function __construct(){

        $this->checklist = new \TYPO3\CMS\Extbase\Persistence\ObjectStorage();



    }


    /**
     * checklist
     *
     * @var Tx_Extbase_Persistence_ObjectStorage
     * @lazy
     */
    protected $checklist;

 /**
     * Returns the checklist
     *
     * @return Tx_Extbase_Persistence_ObjectStorage $checklist
     */
    public function getChecklist()
    {
        return $this->checklist;
    }

    /**
     * Sets the checklist
     *
     * @param Tx_Extbase_Persistence_ObjectStorage $checklist
     * @return void
     */
    public function setChecklist($checklist)
    {
        $this->checklist = $checklist;
    }

    /**
     * Returns the boolean state of checklist
     *
     * @return bool
     */
    public function isChecklist()
    {
        return $this->checklist;
    }

To that object storage i want to be able to add objects of the class 'Checkobject' which has just two propeties: 'name' and 'checked'... Within the controller of the class i have following code in the createAction:

$Kundenauftragliegtvor = \TYPO3\CMS\Core\Utility\GeneralUtility::makeInstance('...\Kundentermine\Domain\Model\Checkobject');
        $Kundenauftragliegtvor->setName('Kundenauftrag liegt vor');
        $Kaufmnnisch->getChecklist()->attach($Kundenauftragliegtvor);

So basically when creating an object of that class i want to initially create the object $Kundenauftragliegtvor (of course later i want to add additional objects of that class, but thats another story)... BUT: When creating an object of the class Kaufmnnisch I get the following error:

Incorrect integer value: '' for column 'checklist' at row 1 

It seems that attaching the object to the objectstorage 'checklist' seems to create the error in my MySQL database. I suspect that my TCA (specifically the part for the objectstorage) isn't correct:

 'checklist' => [
            'exclude' => true,
            'label' => 'LLL:EXT:kundentermine/Resources/Private/Language/locallang_db.xlf:tx_kundentermine_domain_model_kaufmnnisch.checklist',
            'config' => [
                'type' => 'passthrough',
                'items' => [
                    '1' => [
                        '0' => 'LLL:EXT:lang/locallang_core.xlf:labels.enabled'
                    ]
                ],
                'default' => 0,
            ]
        ],

Edit: in my ext_tables.sql file the datatype for 'checklist' is:

checklist smallint(5) unsigned DEFAULT '0' NOT NULL,

I guess the error has something to do with this... But what datatype should i specify for an objectstorage?

If you have a Checkobject, you should use a CheckobjectRepository and add the Checkobject first.

Try:

$Kundenauftragliegtvor = \TYPO3\CMS\Core\Utility\GeneralUtility::makeInstance('...\Kundentermine\Domain\Model\Checkobject');
$checkobjectRepository = $this-objectManager->get('...\Kundentermine\Domain\Repository\CheckobjectRepository');
$checkobjectRepository->add($Kundenauftragliegtvor);

$Kundenauftragliegtvor->setName('Kundenauftrag liegt vor');
$Kaufmnnisch->getChecklist()->attach($Kundenauftragliegtvor);

Note: You can inject the repository inside your controller too.

**
* checkobjectRepository
*
* @var ...\Kundentermine\Domain\Repository\CheckobjectRepository
*/ 
$protected $checkobjectRepository = null;

public function injectCheckobjectRepository(...\Kundentermine\Domain\Repository\CheckobjectRepository $checkobjectRepository) {
            $this->checkobjectRepository = $checkobjectRepository;
        }

If you do this change pls don't forget to clear all caches including Optcode cache.

You TCA should be configured correctly:

  'checklist' => [
            'exclude' => true,
            'label' => 'LLL:EXT:kundentermine/Resources/Private/Language/locallang_db.xlf:tx_kundentermine_domain_model_kaufmnnisch.checklist',
            'config' => array(
                     'type' => 'select',
                     'foreign_table' => 'tx_kundentermine_domain_model_checkobject',
                     'foreign_table_where' => ' AND tx_kundentermine_domain_model_checkobject.sys_language_uid IN (-1,0) ORDER BY tx_kundentermine_domain_model_checkobject.title',
        ],

Your TCA lacks information in the items. Inside the '1' array you need to add one key/value pair for the value (you only defined the label). In case you need an integer, you need to add eg 1 => 0 to define the zero to the label "enabled".

See: https://docs.typo3.org/typo3cms/TCAReference/ColumnsConfig/Type/Select.html#items

PS: Please use english variables and declare integers as integers, not as string.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM