phptypo3typo3-extensionstypo3-12.x

Using own Pages model as reference within another model does not work


Hello dear Stackoverflow community,

i've meet following Scenario which appears to not work properly.

I have an Model called Principal

Inside the Model, i have the following property

protected Pages|null $storage = null;

And the following functions:

public function getStorage(): Pages|null
{
    return $this->storage;
}

public function setStorage(Pages|null $storage): void
{
    $this->storage = $storage;
}

With the following TCA Entry for that Database column:

'storage' => [
    'label'  => 'Mandanten Ordner',
    'config' => [
        'type'          => 'group',
        'internal_type' => 'db',
        'allowed'       => 'pages',
        'foreign_table' => 'pages',
        'foreign_field' => 'uid',
        'minitems'      => 1,
        'suggestOptions' => [
            'default' => [
                'minimumCharacters' => 1,
                'searchWholePhrase' => true
            ],
        ],
        'fieldControl'  => [
            'addRecord' => [
                'options'  => [
                    'title' => 'Neu anlegen',
                ],
            ]
        ]
    ],
],

and db column:

storage int(11) DEFAULT '0' NOT NULL,

i created a Pages Model, for which i have Configured a Classes.php under Configuration->Extbase->Persistence. The Configuration was the following:

\MyVendor\MyExtensionKey\Domain\Model\Pages::class => [
    'tableName' => 'pages',
    'recordType' => '0'
],

The Pages Model:

<?php

namespace MyVendor\MyExtensionKey\Domain\Model;

class Pages extends \TYPO3\CMS\Extbase\DomainObject\AbstractEntity
{
    public function getPid(): int|null
    {
        return $this->pid;
    }

    public function setPid(int $pid): void
    {
        $this->pid = $pid;
    }
}

but when i try to call the getStorage() function from my Principal Model, i always only get null returned even if the uid of the page is already saved to my Principal Model.

i am confused as i already did associate an Model of mine to another Table, it worked when i for example used the fe_user table and so on, but pages does not seems to work.

Is there another solution to my specific case or is this one of the limits that can't be fully implemented within a TYPO3 Extension?


Solution

  • Thanks to Thomas Löffler and Mogens, i came across this solution:

    1. In Configuration\Extbase\Persistence\Classes.php

      1. For the Pages Mapping, the recordType should be 254 (In Case you wanna use Folders) or 1 (In Case you wanna use a Standard Site) and so on -> See https://docs.typo3.org/m/typo3/reference-coreapi/main/en-us/ApiOverview/PageTypes/Index.html

        \MyVendor\MyExtensionKey\Domain\Model\Pages::class => [
            'tableName' => 'pages',
            'recordType' => '254'
        ],
        
    2. Configuration\TCA\tx_yourextensionkey_domain_model_principal.php

      1. In my TCA, the type for the storage field was set to group, but group do not have an internal_type or foreign_field, that's why even with the fact that the right value was saved to the Database, it was somehow bugged when i called the getStorage method from my principal Model.

      2. For the reference what array_keys can be used for the group type, see https://docs.typo3.org/m/typo3/reference-tca/12.4/en-us/ColumnsConfig/Type/Group/Index.html#properties-of-the-tca-column-type-group, which was suggested by Mogens comment

        'storage' => [
            'label'  => 'Mandanten Ordner',
            'config' => [
                'type'          => 'group',
                'allowed'       => 'pages',
                'foreign_table' => 'pages',
                'minitems'      => 1,
                'suggestOptions' => [
                    'default' => [
                        'minimumCharacters' => 1,
                        'searchWholePhrase' => true
                    ],
                ],
                'fieldControl'  => [
                    'addRecord' => [
                        'options'  => [
                            'title' => 'Neu anlegen',
                        ],
                    ]
                ]
            ],
        ],
        

        These are the corrected code parts, the first one is the Configuration\Extbase\Persistence\Classes.php and the second one is the Configuration\TCA\tx_yourextensionkey_domain_model_principal.php code.