在调度程序任务中执行存储库功能 [英] Execute repository functions in scheduler task
问题描述
当前我有一个调度程序任务,但是我想使用extbase存储库中的功能(在同一扩展名中).
Currently I have an scheduler task, but I want to use function from my extbase repository (in the same extension).
无论如何尝试从extbase包含我的存储库或控制器,我都不断收到"PHP致命错误:在非对象上调用成员函数add()".
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.
我的SampleTask.php:
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);
}
}
并在我的ext_localconf.php中正确定义
And correctly defined in my ext_localconf.php
有人可以解释一下我如何从我的SampleTask.php访问我的存储库(或控制器)-extbase-.
Can someone explain me how I can access my Repository (or controller) -extbase- from my SampleTask.php.
使用TYPO3 6.2.
Using TYPO3 6.2.
谢谢.
推荐答案
您收到此php错误,因为您使用makeInstance()
实例化了控制器.如果使用makeInstance获取objectManager(\TYPO3\CMS\Extbase\Object\ObjectManager
)并使用$objectManager->get('TYPO3\ExtName\Controller\SampleController')
,则控制器内部的依赖项注入将起作用(例如,存储库).
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).
但是您可以使用objectManager立即获取存储库,因此您不必调用控制器操作:
But you can use the objectManager to get the repository right away, so you dont have to call a controller action:
类似这样的东西:
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();
}
}
这篇关于在调度程序任务中执行存储库功能的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!