简体   繁体   English

TYPO3 v9 调度程序不持久化数据

[英]TYPO3 v9 Scheduler does not persist the data

I have a function that loads data from a JSON file and enters it into the TYPO3 database.我有一个 function 从 JSON 文件加载数据并将其输入 TYPO3 数据库。 If I call this function via the backend Controller ( indexAction ), then everything works fine.如果我通过后端 Controller ( indexAction )调用这个 function ,那么一切正常。 However, when I call it from a task, the data is not saved.但是,当我从任务中调用它时,不会保存数据。 By means of test output I see that the object was changed correctly, only the Update or Add is not executed correctly, because the data in the database is not changed.通过测试 output 我看到 object 更改正确,只有UpdateAdd没有正确执行,因为数据库中的数据没有更改。

Here is my controller function:这是我的 controller function:

class ImportController extends \TYPO3\CMS\Extbase\Mvc\Controller\ActionController
{
    protected $siteRepository = null;

    public function injectSiteRepository(SiteRepository $siteRepository)
    {
        $this->siteRepository = $siteRepository;
    }

public function indexAction()
{ 
    $this->dataImport();
}

public function dataImport() {
        $file = "test.json";
        $json = file_get_contents($file);
        $jsonarray = json_decode($json);

        foreach ($jsonarray->{'sites'} as $site) {
            $newValue = false;
            $dbSite = $this->siteRepository->getSiteByID($site->{'ID'});
            if (empty($dbSite->getFirst())) {
                $dbSite = \TYPO3\CMS\Core\Utility\GeneralUtility::makeInstance('test\test123\Domain\Model\Site');
                $dbSite->setID($site->{'ID'});
                $newValue = true;
            } else {
                $dbSite = $dbSite->getFirst();
            }

            //Set Data
            $dbSite->setTest($site->{'TEST'});

            //This object is correct, even in the Task
            DebuggerUtility::var_dump(
                 $dbSite
            );

            //Update or Add new Data
            if (!$newValue) {
                $this->siteRepository->update($dbSite);
            } else {
                $this->siteRepository->add($dbSite);
            }
        }
        $persistenceManager = \TYPO3\CMS\Core\Utility\GeneralUtility::makeInstance('TYPO3\CMS\Extbase\Persistence\Generic\PersistenceManager');
        $persistenceManager->persistAll();
        return true;
    }
}

Here is my task:这是我的任务:

class JsonImportTask extends AbstractTask {
    public function execute() {
        $objectManager = GeneralUtility::makeInstance(
            ObjectManager::class
        );
        $controller = $objectManager->get(ImportController::class);
        $controller->dataImport();

        return true;
    }
}

Here my repository:这是我的存储库:

public function getSiteByID($id) {
    $query = $this->createQuery();
    $query->matching(
        $query->equals("uid", $id),
    );
    return $query->execute();
}

Does anyone have an idea what this could be?有谁知道这可能是什么?

Ok I found my mistake myself.好的,我自己发现了我的错误。 Here is the solution for all who have the same problem:以下是所有有相同问题的人的解决方案:

I added setRespectStoragePage in my getSiteByID function in SiteRepository:我在setRespectStoragePagegetSiteByID function 中添加了 setRespectStoragePage:

$query->getQuerySettings()->setRespectStoragePage(false);

The error was that it was looking for the data at StoragePid 1. With this command he searches at the right place错误在于它正在查找StoragePid 1 的数据。使用此命令,他在正确的位置进行搜索

Here is my correct repository function:这是我正确的存储库 function:

public function getSiteByID($id) {
    $query = $this->createQuery();
    $query->getQuerySettings()->setRespectStoragePage(false);
    $query->matching(
        $query->equals("uid", $id),
    );
    return $query->execute();
}

I had another problem.我还有另一个问题。 You have to set the PID number for new entries.您必须为新条目设置 PID 号。

For example, my data is stored on Page ID 12.例如,我的数据存储在 Page ID 12 上。

I added this line here:我在这里添加了这一行:

if (empty($dbSite->getFirst())) {
    $dbSite = \TYPO3\CMS\Core\Utility\GeneralUtility::makeInstance('test\test123\Domain\Model\Site');
    $dbSite->setID($site->{'ID'});
    $newValue = true;
    $dbSite->setPid(12); //New Line set PID (For me to PID 12)
} else {
    $dbSite = $dbSite->getFirst();
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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