我在控制器中更新了依赖项注入,现在看起来像这样:
class MyExtensionController extends ActionControlelr
{
/**
* @ var MyExtensionRepository
/**
protected $myRepository
..
..
..
但是在下一个函数中这样调用存储库
public function indexAction()
{
// get needed info
/** @var Category $category */
$category = $this->myRepository->findByUid($this->settings['flexForm']['categoryId']);
返回错误:
Error: Call to a member function findByUid() on null
Services.yaml看起来像这样:
services:
_defaults:
autowire: true
autoconfigure: true
public: false
REP\MyExtension\:
resource: '../Classes/*'
有什么想法吗?
谢谢
我们看不到您如何设置$this->myRepository
,但它似乎未在Controller类上设置。
在Symfony上访问Doctrine存储库类的推荐方法是通过EntityManager,如下所示:
$category = $this->getDoctrine()
->getRepository(Category::class)
->findByUid($this->settings['flexForm']['categoryId']);
不要忘记,存储库应该链接到一个实体,所以您应该在某个地方有一个Category.php
实体,并带有如下所示的注释:
/**
* @ORM\Entity(repositoryClass="App\Repository\CategoryRepository")
*/
class Category
{
...
}
似乎您正在混合TYPO3和Symphony。除了Services.yaml之外的所有内容都看起来像TYPO3。
在TYPO3控制器中,您必须注入存储库。您可以使用变量声明旁边的@inject注释来完成此操作。您还应该在那里使用完整的名称空间。然后,您必须清除所有缓存,它应该可以工作。
请修正您的整个评论部分。
/**
* @var \Full\Path\To\Repository\MyExtensionRepository
* @inject
*/
protected $myRepository