5

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

public function listAction() {
    $this->settings['flexform']['showCategory'] ? $catId = $this->settings['flexform']['showCategory']:$catId = $this->settings['listView']['showCategory'];
    // print $catId;
    if (!$catId || $this->settings['flexform']['showCategory'] == '-1') {
        $designs = $this->designRepository->findAll(); 
    } else {
        // $designs = $this->designRepository->findByCategory($catId);
        $designs = $this->designRepository->findByCategoryGrouped($catId);  // THIS
        $categories = $this->categoryRepository->getCategories($catId); // THIS
    }
    // indhold forsvinder hvis næste linje slettes
    $this->view->assign('designs', $designs, "L", $GLOBALS['TSFE']->sys_language_uid);
    $this->view->assign('catId', $catId);
    $this->view->assign('categories', $categories);
}
Daniel
  • 6,916
  • 2
  • 36
  • 47
Jeppe Donslund
  • 469
  • 9
  • 25

1 Answers1

23

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!

Daniel
  • 6,916
  • 2
  • 36
  • 47
  • My listAction in the code above, is from designController.php. Is in that file I should add /** * (at)var \Vendor\Extension\Domain\Repository\SomeRepository * (at)inject */ protected $someRepository; Or should I create categoryController.php and add it in there? – Jeppe Donslund Nov 01 '13 at 13:32
  • You dont have to create a new controller. Just inject the categoryRepository into your DesignController like you (hopefully) did with the designRepository... – Daniel Nov 01 '13 at 13:39
  • 'class DesignController extends \TYPO3\CMS\Extbase\Mvc\Controller\ActionController { /** * designRepository * * (at)var \TYPO3\OrigKentaurProducts\Domain\Repository\DesignRepository * (at)inject */ protected $designRepository; /** * (at)var \TYPO3\OrigKentaurProducts\Domain\Repository\CategoryRepository * (at)inject */ protected $categoryRepository;' And: '$categories = $this->categoryRepository->getCategories($catId);' That gives me "Call to a member function getCategories() on a non-object" In CategoryRepository.php I have: 'public function getCategories($catId){' – Jeppe Donslund Nov 01 '13 at 14:01
  • Sorry for missing line breaks. – Jeppe Donslund Nov 01 '13 at 14:02
  • Is your CategoryRepository.php is in the same folder as your designRepository. Make sure they are, you telling extbase they are. – Daniel Nov 01 '13 at 14:18
  • Then, check the namespaces in both Repositories. It has to be TYPO3\OrigKentaurProducts\Domain\Repository\. – Daniel Nov 01 '13 at 14:19
  • 6
    And after adding an Dependency Injection, you have to clear the cache – Daniel Nov 01 '13 at 14:19
  • It is fantastic: Now everything works. I thank you so much for your time. – Jeppe Donslund Nov 04 '13 at 08:02
  • Glad to help out. Can you mark my answer as accepted then, so the question is marked as answered, please? – Daniel Nov 04 '13 at 11:44
  • in my case, i needed to deactive/active the extension to make it work – Gerson Jul 25 '14 at 15:47