9

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?

tshepang
  • 10,772
  • 21
  • 84
  • 127
kapale
  • 485
  • 1
  • 7
  • 14

4 Answers4

12

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.

Hendrik
  • 556
  • 6
  • 10
3

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.

greg
  • 1,618
  • 1
  • 18
  • 30
2

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..

PeterTheOne
  • 333
  • 5
  • 15
  • 1
    Thx for the hint with the upper/lowerCamelCase. I hopped that this would be the solution... I'll have a deeper look on this to be sure not to have this on severeal places. I don't get an error, I get a Tx_Extbase_Persistence_QueryResult object back, but without a result. Neither do I get an error message. Thank you for your support! – kapale Jul 23 '12 at 14:02
  • Found the UpperCamelCase problem on a second possition, but unfortunately it didn't solved my problem. (After deleting all caches) – kapale Jul 23 '12 at 14:13
1

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

konjye
  • 11
  • 1