TYPO3: repository->findAll() not working
Asked Answered
W

5

5

I am building an extension with a backend module. When I call the findAll() method it returns a "QueryResult" object.

I tried to retrieve objects with findByUid() and it does work.

I set the storage pid in the typoscript:

plugin.tx_hwforms.persistence.storagePid = 112

I can also see it in the typoscript object browser.

I also added this to my repository class:

public function initializeObject()
    {
        $defaultQuerySettings = $this->objectManager->get(\TYPO3\CMS\Extbase\Persistence\Generic\Typo3QuerySettings::class);
        $defaultQuerySettings->setRespectStoragePage(false);
        $this->setDefaultQuerySettings($defaultQuerySettings);
    }

so that the storage pid is ignored ... It's still not working, findAll doesn't return an array of entites as it should

Westphalia answered 14/6, 2017 at 8:14 Comment(5)
seems to be ok the snippet above. Are you sure that you have records in the table related to this repository ?Sabina
Hi Andrei, yes I am sure. I also tried to use a repository from another extension where findAll() is working fine. And I notice it is also not working, so for some reason findAll isn't working at all in my extension.Westphalia
so, var_dump ( use the function from extbase ) the $GLOBALS['TYPO3_DB'] after calling the findAll() function and check the last query buildSabina
I tried that but it completely ignores the findAll method. It works with other repository methods like findByUid or even findAll()->count ... which doesnt make any sense to meWestphalia
weird. Is your repository extends TYPO3::CMS::Extbase::Persistence::Repository ? Have you check for name spelling between repository, model and tablename ?Sabina
V
8

Repository must return a QueryResult from the findAll methods. Only methods which return a single object (findOneByXYZ) will return anything else.

All of the following operations will cause a QueryResult to load the actual results it contains. Until you perform one of these, no results are loaded and debugging the QueryResult will yield no information except for the original Query.

  • $queryResult->toArray();
  • $queryResult->offsetGet($offset); and $queryResult[$offset];
  • $queryResult->offsetExists($offset);
  • $queryResult->offsetSet($offset, $value); and $queryResult[$offset] = $value; (but be aware that doing this yourself with a QueryResult is illogical).
  • $queryResult->offsetUnset($offset); and unset($queryResult[$offset]); (again, illogical to use this yourself)
  • $queryResult->current(), ->key(), ->next(), ->prev(), ->rewind() and ->valid() which can all be called directly or will be called if you begin iterating the QueryResult.

Note that ->getFirst() and ->count() do not cause the original query to fire and will not fill results if they are not already filled. Instead, they will perform an optimised query.

Summa summarum: when you get a QueryResult you must trigger it somehow, which normally happens when you begin to render the result set. It is not a prefilled array; it is a dynamically filled Iterator.

Verleneverlie answered 15/6, 2017 at 13:26 Comment(0)
G
5

This should work.there must be issue with your storage page in FindAll() extbase check for storage but in findByXXX() it ignore storage.

$objectManager = \TYPO3\CMS\Core\Utility\GeneralUtility::makeInstance('TYPO3\\CMS\Extbase\\Object\\ObjectManager');
$querySettings = $objectManager->get('TYPO3\\CMS\\Extbase\\Persistence\\Generic\\Typo3QuerySettings');
$querySettings->setRespectStoragePage(FALSE);

$this->cityRepository->setDefaultQuerySettings($querySettings);
$cities = $this->cityRepository->findAll();
Gatepost answered 14/6, 2017 at 9:57 Comment(2)
I tried your code inside my controller but it still returns a "QueryResult" objectWestphalia
@Westphalia If you want to get results in Array instead of QueryResult object then you need to use toArray().Berey
W
2

Use additionally typoscript module configuration, like

module.tx_hwforms.persistence.storagePid = 112

Ensure your Typoscript is loaded in root. For BE modules I prefere to use

EXT:hwforms/ext_typoscript_setup.txt

where you write your module and extbase configuration.

Won answered 14/6, 2017 at 8:35 Comment(2)
I set the storagePid for the module in the ext_typoscript_setup.txt as you said but unfortunaterly it's still not working :(Westphalia
Correct answer for me! I use config.tx_extbase.persistence.storagePid = 5 in ts_setup. Be sure that your are using the right PID, having some entries!Schnapps
B
1

Try to debbug like below and check findAll() method present for this repositry. I think this is useful for you click here

\TYPO3\CMS\Extbase\Utility\DebuggerUtility::var_dump(get_class_methods($this->yourRepositryName)); exit();

Afetr added all your changes once you need to uninsatll/install extension.

Brogue answered 14/6, 2017 at 9:41 Comment(3)
I tried debugging but it doesn't dump anything with findAll()... I tried with findAll()->count() and it then it works. Also tried uninstall and reinstall the extension but still the sameWestphalia
check once your extension setup.txt and contant.txt file storagePid typoscript sysntaxt rights or not ?Brogue
Yes syntax is right, it's also shows up in the typoscript object browser in the backendWestphalia
C
1

I would inspect the generated query itself. Configure the following option in the install tool:

$GLOBALS["TYPO3_CONF_VARS"]["sqlDebug"]

Note: dont do this in production environemnt!!

Explanation for sqlDebug:

Setting it to "0" will avoid any information being print on screen.

Setting it to "1" will show errors only.

Setting it to "2" will print all queries on screen.

So in Production you want to keep it at "0", in development environments you should set it to "1", if you want to know why some result is empty, configure it to "2".

I would guess that some enablefield configuration causes your problem.

If you retrieve an object by findByUid you will have the return because enablefields are ignored. In every other case enablefields are applied and that may cause your empty result.

Confident answered 14/6, 2017 at 12:11 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.