You are getting this php error, because you instanciated your controller with makeInstance()
. If you use makeInstance to get the objectManager (\TYPO3\CMS\Extbase\Object\ObjectManager
) and use $objectManager->get('TYPO3\ExtName\Controller\SampleController')
, the dependency injection inside your controller will work (e.g. your repository).
But you can use the objectManager to get the repository right away, so you dont have to call a controller action:
Something like this:
namespace TYPO3\ExtName\Task;
use TYPO3\CMS\Core\Utility\GeneralUtility;
use TYPO3\CMS\Extbase\Object\ObjectManager;
use TYPO3\ExtName\Domain\Repository\SampleRepository;
use TYPO3\ExtName\Domain\Model\Sample;
use TYPO3\CMS\Extbase\Persistence\PersistenceManagerInterface;
class SampleTask extends \TYPO3\CMS\Scheduler\Task\AbstractTask {
public function execute() {
$objectManager = GeneralUtility::makeInstance(ObjectManager::class);
$sampleRepository= $objectManager->get(SampleRepository::class);
$new = new Sample();
$new->setName('test');
$sampleRepository->add($new);
$objectManager->get(PersistenceManagerInterface::class)->persistAll();
}
}