TYPO3 - Call another repository

前端 未结 1 1802
执念已碎
执念已碎 2021-01-03 06:00

Is it possible to call a function in different controllers? I need to call FindByCategoryGrouped($catId) in designRepository.php and getCategories($catId)

1条回答
  •  失恋的感觉
    2021-01-03 06:49

    You can inject every repository of every installed extbase extension. Just add the dependency injection code to your controller. Depending on your TYPO3 version ist either:

    TYPO3 >= 6.0:

    /**
     * @var \Vendor\Extension\Domain\Repository\SomeRepository
     * @inject
     */
    protected $someRepository;
    

    Note that the @inject Annotation does not perform very well in comparison to a dedicated inject method. So if you need to tweek the performance of your application and have many injections in yout controller you should consider switching to inject methods:

    /**
     * @var \Vendor\Extension\Domain\Repository\SomeRepository
     */
    protected $someRepository;
    
    /**
     * @param \Vendor\Extension\Domain\Repository\SomeRepository
     */
    public function injectSomeRepository(\Vendor\Extension\Domain\Repository\SomeRepository $someRepository) {
      $this->someRepository = $someRepository;
    }
    

    TYPO3 = 4.7:

    /**
     * @var Tx_MyExtension_Domain_Repository_SomeRepository
     * @inject
     */
     protected $someRepository;
    

    TYPO3 < 4.7

    /**
     * @var Tx_MyExtension_Domain_Repository_SomeRepository
     */
     protected $someRepository;
    
    /**
     * Inject SomeRepository
     * @param Tx_MyExtension_Domain_Repository_SomeRepository $someRepository
     * @return void
     */
    public function injectSomeRepository(Tx_MyExtension_Domain_Repository_SomeRepository $someRepository) {
      $this->someRepository = $someRepository;
    }
    

    In any case you can use $this->someRepository with all its methods in the controller you injected the repository into.

    Edit: fixed typo.

    Edit: After adding a Dependency Injection, you have to clear the cache!

    0 讨论(0)
提交回复
热议问题