8

Currently I have an scheduler task, but I want to use function from my extbase repository (in the same extension).

I keep getting "PHP Fatal error: Call to a member function add() on a non-object", no matter how I try to include my repo or controller from extbase.

My SampleTask.php:

namespace TYPO3\ExtName\Task;

class SampleTask extends \TYPO3\CMS\Scheduler\Task\AbstractTask {

    public function execute() {
        $controller = \TYPO3\CMS\Core\Utility\GeneralUtility::makeInstance('\TYPO3\ExtName\Controller\SampleController');
        $new = new \TYPO3\ExtName\Domain\Model\Sample;
        $new->setName('test');
        $controller->createAction($new);
    }
}

And correctly defined in my ext_localconf.php

Can someone explain me how I can access my Repository (or controller) -extbase- from my SampleTask.php.

Using TYPO3 6.2.

Thank you.

Daan
  • 130
  • 2
  • 6

1 Answers1

13

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();
    }
}
Daniel
  • 6,916
  • 2
  • 36
  • 47
  • Hi Daniel, thank you for you for your answer! My scheduler task doesn't throw an error anymore, but I still cannot add anything to my repository. I think the Pid is the problem, tried to set it with the configurationManager which has no effect. I can add items to my repository in my controller without problems. Also, when I add the plugin to a content element, I also have to set the PID in 'Record Storage Page'. Pid is set in typoscript configuration.. – Daan Jan 18 '14 at 17:01
  • most likely you have to call the persitenceManager manually to persist your added object. I added it to my answer. – Daniel Jan 18 '14 at 17:07
  • Adding the persitenceManager worked! The only problem i'm running into now is that the pid is set to 2 (?) in the database. – Daan Jan 18 '14 at 17:51
  • 1
    In your case I would recommend to set a pid via $new->setPid(1234); – Daniel Jan 18 '14 at 18:06