extbase repository findAll() returns result null

2019-02-12 17:17发布

问题:

I have several Controllers like those: CategoryController and NewsController As well as the domain models for category and news and reposirtories for both.

In the NewsController I do a dependencyInjection like this (the same way as in categoryController):

    /**
 * categoryRepository
 *
 * @var Tx_MyExtension_Domain_Repository_CategoryRepository
 */
protected $categoryRepository;


    /**
 * injectCategoryRepository
 *
 * @param Tx_MyExtension_Domain_Repository_CategoryRepository $CategoryRepository
 * @return void
 */
public function injectCategoryRepository(Tx_MyExtension_Domain_Repository_CategoryRepository $categoryRepository) {
    $this->categoryRepository = $categoryRepository;
}

When I'm trying now in a function something like this:

    /**
 * action getCategoriesAjax
 *
 * @param Tx_MyExtension_Domain_Model_News
 * @return void
 */
public function getCategoriesAjaxAction() {
    $categories = $this->categoryRepository->findAll();
    $this->view->assign('categories',$categories);
}

I get an empty result back.

The strange thing for me is, that if I'm doing this in the CategoryController, the same function works like charm and returns all elements in the database and even stranger for me is, that if I do a $this->categoryRepository->findByUid(1) I get the correct element as result.

I also added to my categoryRepository a test function:

public function test(){
  $query = $this->createQuery();
  $result = $query->execute();
  $amount = $result.count();
}

If I call this function from categoryController, I get back the correct amount of elements. If I'm calling this from my newsController I get "0" back...

I don't get it...

What do I miss??? Where is my mistake?

回答1:

This has bugged me for days (or weeks). The StoragePid (the reference to the page where your database items are attached to) does not make it to the database query if you don't define the following somewhere in your TypoScript:

plugin.tx_myextension.persistence.storagePid = 4

Put this in your Page-TS and the findAll method from Tx_Extbase_Persistence_Repository should be working fine.

Weeks.



回答2:

Or you could force Repository to ignore Storage Page:

class MymodelRepository extends \TYPO3\CMS\Extbase\Persistence\Repository {
  public function initializeObject() {

  $this->defaultQuerySettings = $this->objectManager->get('TYPO3\\CMS\\Extbase\\Persistence\\Generic\\Typo3QuerySettings');
  $this->defaultQuerySettings->setRespectStoragePage(FALSE);
 }
}

From now on your Repository will pull every record from the database.



回答3:

Are you sure that the return value of findAll() is NULL?

It could be that your Dependency Injection doesn't work. Then the Exception should be something like this:

findAll() is called on a Non-Object ($this->categoryRepository)

Also in your injectCategoryRepository you write $categoryRepository in lowerCamelCase and in the annotations in UpperCamelCase $CategoryRepository

Hope this helps..



回答4:

As Hendrik said you can set it in your page-TS. Another solution is: in you BE, edit your page content that contains the plugin, select the Sys Folder, in which your db items are stored. This is found under the tab "Behaviour" for Typo3 v6.1