Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
389 views
in Technique[技术] by (71.8m points)

task - 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. If I call this function via the backend Controller (indexAction), then everything works fine. 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.

Here is my controller function:

class ImportController extends TYPO3CMSExtbaseMvcControllerActionController
{
    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 = TYPO3CMSCoreUtilityGeneralUtility::makeInstance('testest123DomainModelSite');
                $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 = TYPO3CMSCoreUtilityGeneralUtility::makeInstance('TYPO3CMSExtbasePersistenceGenericPersistenceManager');
        $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?

question from:https://stackoverflow.com/questions/65880684/typo3-v9-scheduler-does-not-persist-the-data

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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:

$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

Here is my correct repository 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.

For example, my data is stored on Page ID 12.

I added this line here:

if (empty($dbSite->getFirst())) {
    $dbSite = TYPO3CMSCoreUtilityGeneralUtility::makeInstance('testest123DomainModelSite');
    $dbSite->setID($site->{'ID'});
    $newValue = true;
    $dbSite->setPid(12); //New Line set PID (For me to PID 12)
} else {
    $dbSite = $dbSite->getFirst();
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...