首页 > 解决方案 > Typo3:PersistenceManager persistAll() 在将对象添加到存储库后创建空数据库记录

问题描述

我正在尝试保护数据库中的对象,因此我正在使用 PersistenceManager 类。

在调用之前,persistAll()我正在向我的存储库添加一个对象。

    $newChecklist = \TYPO3\CMS\Core\Utility\GeneralUtility::makeInstance('foo\\bar\\Domain\\Model\\Checklist');
    $newChecklist->setNameDE('de');
    $newChecklist->setNameEN('en');
    $newChecklist->setAuthor('someGuy');
    //DebuggerUtility::var_dump($newChecklist); //returns object with correct attribute values
    $this->checklistRepository->add($newChecklist);

之后,我像这样使用 PersistenceManager:

$persistenceManager = \TYPO3\CMS\Core\Utility\GeneralUtility::makeInstance('TYPO3\\CMS\\Extbase\\Persistence\\Generic\\PersistenceManager');
$persistenceManager->persistAll();

但是这段代码只产生一个空的数据库记录。

我在语法中找不到任何错误。我的文件名都设置正确,Model=>Checklist, Repository=>ChecklistRepository, Controller=>ChecklistController

有谁知道如何解决这个问题?

提前致谢

标签: phptypo3-8.x

解决方案


此代码适用于我的 BE 模块:

class MyController extends \TYPO3\CMS\Extbase\Mvc\Controller\ActionController
{
    /**
     * myRepository
     *
     * @var \VENDOR\Extension\Domain\Repository\AddressRepository
     * @inject
     */
    protected $addressRepository = null;

    function import() {
        $persistenceManager = $this->objectManager->get("TYPO3\\CMS\\Extbase\\Persistence\\Generic\\PersistenceManager");
        ...
        $record = GeneralUtility::makeInstance('VENDOR\\Extension\\Domain\\Model\\Address');
        $record->setTitle('The Title');
        $record->setPid(123);
        ...

        $this->myRepository->add($record);
        $persistenceManager->persistAll();
        ...
    }
}

推荐阅读