My Typo3 Repository findByUid returns always NULL in a Scheduler Task

177 Views Asked by At

I am creating a extbase typo3 11.5 extension, that updates jobs in a database with a Scheduler Task ImportJobsTask.

namespace MyCompany\MyExtension\Task;

class ImportJobsTask extends AbstractTask
{
    public function __construct()
    {
        parent::__construct();
    }

    public function execute()
    {
        $this->jobOfferRepository = GeneralUtility::makeInstance(JobOfferRepository::class);
        $job = ['id' => 253, 'title' => 'Lagermitarbeiter'];
        $job = $this->jobOfferRepository->findByJob($job);
        DebuggerUtility::var_dump($job);
        return true;
    }
}

For that i use a JobsOfferRepository.

declare(strict_types=1);

namespace MyCompany\MyExtension\Domain\Repository;

class JobOfferRepository extends \TYPO3\CMS\Extbase\Persistence\Repository
{
    public function findByJob(array $job)
    {
        $query = $this->createQuery();

        $constraints = [
            $query->equals('job_id', $job["id"]),
            $query->equals('job_title', $job["title"]),
        ];
        $query->matching($query->logicalAnd($constraints));
        return $query->execute()->getFirst();
    }
}

In this Repository i call from the Model JobOffers the functions updateJob($job_id) and createJob($job) for updating and creating such Jobs, these works fine.

declare(strict_types=1);

namespace MyCompany\MyExtension\Domain\Model;

class JobOffer extends \TYPO3\CMS\Extbase\DomainObject\AbstractEntity
{
    public function createJob(array $job, int $pid): JobOffer
    {
        $newJob = new self();
        $newJob->setPid($pid);
        $newJob->setJobId($job["id"]);
        $newJob->setJobTitle($job["title"]);
        # some code...
        return $newJob;
}

But in my Task i just can't get any results from findByJob($job), it always returns NULL. Everything i try to get some jobs from inside of my Repository always returns NULL. On the other hand i can store new Jobs from the Repository, if i call $jobOffer->createJob($job) from there.

I implemented a setPid() function in the Repository to make sure the storagePid is correct, but the result didn't change.

Somebody has an idea, what i might be missing? Some configuration? Messed up Namespace?

2

There are 2 best solutions below

0
cari On BEST ANSWER

Ok, i found my answer.

i implemented a function setPID($pids) in my Repository, than everything worked. The other problems were from bad testing.

in my Task's execute():

...
$this->storagePid = $typoscript['module.']['tx_myextension_web_myextensionjobadmin.']['persistence.']['storagePid'];

$jobOfferRepository = GeneralUtility::makeInstance(JobOfferRepository::class);
$jobOfferRepository->setPID(array($this->storagePid));
...

In my Repository:

    public function setPID($pids)
    {
        $querySettings = GeneralUtility::makeInstance(Typo3QuerySettings::class);
        $querySettings->setStoragePageIds($pids);
        $this->setDefaultQuerySettings($querySettings);
    }

That was all i needed. I guess the debugging part would have worked with Garvin's answer, too. But maybe my testing was so off that i didnt realize. i tried with var_dump instead of DebuggerUtility::var_dump($object) and got out of memory, which i misinterpreted. yes, i am a typo3 beginner :-)

5
Garvin Hicking On

Did you add defaultQuerySettings in your Repository to set/remove specific Restraints?

Or try a Debug on the created query to see if maybe some conditions mismatch?

An example repository setup could be:

<?php
namespace Faktore\Dummy\Domain\Repository;

use TYPO3\CMS\Extbase\Persistence\Generic\QuerySettingsInterface;
use TYPO3\CMS\Core\Utility\GeneralUtility;
use TYPO3\CMS\Extbase\Persistence\Repository;
use TYPO3\CMS\Extbase\Persistence\Generic\Typo3QuerySettings;
class DummyRepository extends Repository
{
    /**
     * initialize repository settings
     */
    public function initializeObject() {
        /** @var QuerySettingsInterface $defaultQuerySettings */
        $defaultQuerySettings = GeneralUtility::makeInstance(Typo3QuerySettings::class);
        // remove the pid constraint
        $defaultQuerySettings->setRespectStoragePage(FALSE);
        $this->setDefaultQuerySettings($defaultQuerySettings);
    }

}