Search code examples
typo3typo3-10.xdatahandler

MM relation which is added with command controller not shown in backend field


I've experienced some issues with adding mm relations via a command controller in TYPO3.

I've tried to migrate the author field of pages (which is normally a varchar field in order to fill a string) to a mm relation of fe_users. In my database the mm relations are added correctly, but in the backend the selected fe_users are not shown in the author field.

I've tried to add them via a datahandler script in a command controller (I've also tried to add them with a query builder before, but same problem):

/**
 * @var array
 */
protected $mappingUsers = [
    'Max Mustermann' => 2,
    'Miri Musterfrau' => 5
];

public function execute(InputInterface $input, OutputInterface $output)
{
    /** @var ConnectionPool $connectionPool */
    $connectionPool = GeneralUtility::makeInstance(ConnectionPool::class);

    /** @var QueryBuilder $queryBuilderPages */
    $queryBuilderPages = $connectionPool->getQueryBuilderForTable('pages');

    /** @var QueryBuilder $queryBuilderAuthor */
    $queryBuilderAuthor = $connectionPool->getQueryBuilderForTable('tx_project_pages_author_mm');

    $pages = $queryBuilderPages
        ->from('pages')
        ->select('*')
        ->execute()
        ->fetchAll();

    /** @var DataHandler $dataHandler */
    $dataHandler = \TYPO3\CMS\Core\Utility\GeneralUtility::makeInstance(\TYPO3\CMS\Core\DataHandling\DataHandler::class);
    $data = [];

    foreach ($pages as $page) {
        if ($page['author']) {
            foreach ($this->mappingUsers as $key => $value) {
                if ($page['author'] === $key) {
                    $data = [
                        'pages' => [
                            $page['uid'] => [
                                'author' => $value,
                                'tstamp' => time(),
                            ]
                        ],
                        'tx_project_pages_author_mm' => [
                            str_replace(' ', '', 'NEW' . microtime() . rand()) => [
                                'uid_local' => $page['uid'],
                                'uid_foreign' => $value
                            ]
                        ]
                    ];

                    $dataHandler->start($data, []);
                    $dataHandler->process_datamap();

                    unset($data);
                }
            }
        }
    }

    $queryBuilderPages
        ->update('pages')
        ->where(
            $queryBuilderPages->expr()->eq('author', "''"),
        )
        ->set('author', 0)
        ->execute();

    return 0;
}

Is there anything else to do after adding mm relations via a command controller?


Solution

  • In general the script above works. There was just an error in the database structure.

    However it is recommended to implement the backend user authentication in order to be able to delete the cache after running the command e.g.:

        public function execute(InputInterface $input, OutputInterface $output)
        {
            Bootstrap::initializeBackendAuthentication();