Repository


Standard Repository-Methods

  1. // add
  2. $myRepository->add($myModel);
  3.  
  4. // remove
  5. $myRepository->remove($myModel);
  6.  
  7. // removeAll
  8. $myRepository->removeAll(); // don't do this at home
  9.  
  10. // replace
  11. $myRepository->replace($myModel, $myOtherModel);
  12.  
  13. // update
  14. $myRepository->update($myModifiedModel);
  15.  
  16. // count ALL
  17. $myRepository->countAll(); // get the amount of all objects
  18.  
  19. // count constraint
  20. $myRepository->countBy[myProperty]($thisMustMatchMyProperty);
  21.  
  22. // UID
  23. $myRepository->findByUid($theUid); // returns ONE Model-Object (...if found)
  24.  
  25. // all
  26. $myRepository->findAll(); // only constraint by enablecolumns, language and storagePid
  27.  
  28. // findBy...
  29. $myRepository->findBy[myProperty]($thisMustMatchMyProperty); // returns a Storage-object
  30.  
  31. // findOneBy...
  32. $myRepository->findOneBy[myProperty]($thisMustMatchMyProperty); // returns ONE Model-Object (...if found)

Query-Object in Repository-Method

  1. // INITialize query-object
  2. $query = $this->createQuery();
  3.  
  4. // AND / OR / NOT
  5. $query->logicalAnd($constraints); // commaseparated list of constraints or array
  6. $query->logicalOr($constraints); // commaseparated list of constraints or array
  7. $query->logicalNot($constraints); // commaseparated list of constraints or array
  8.  
  9. // A direct SQL-Statement
  10. $query->statement("SELECT * FROM my_table WHERE 1 " . $GLOBALS['TSFE']->sys_page->enableFields("my_table")); // string
  11.  
  12. // ORDER BY
  13. $query->setOrderings(array("field" => \TYPO3\CMS\Extbase\Persistence\QueryInterface::ORDER_ASCENDING)); // array
  14. $query->setOrderings(array("field" => \TYPO3\CMS\Extbase\Persistence\QueryInterface::ORDER_DESCENDING)); // array
  15.  
  16. // LIMIT
  17. $query->setLimit($limit); // integer
  18.  
  19. // OFFSET
  20. $query->setOffset($offset); // integer
  21.  
  22. // only COUNT()
  23. $query->count(); // execute query and get amount of matches
  24.  
  25. // execute query
  26. $query->execute();
  27.  
  28. // only get the first result (LIMIT 1)
  29. $query->getFirst();
  30.  
  31. /*
  32.  * typical method
  33.  */
  34. public function findSomethingSpecial() {
  35. $query = $this->createQuery();
  36. $query->matching(
  37. $query->logicalAnd(
  38. $query->equals("mySpecialProperty", "isMySpecialValue"),
  39. $query->equals("someOtherProperty", "isAnotherValue")
  40. )
  41. );
  42.  
  43. $query->setOrderings(array("crdate" => \TYPO3\CMS\Extbase\Persistence\QueryInterface::ORDER_ASCENDING))
  44. ->setOffset(10)
  45. ->setLimit(20);
  46.  
  47. return $query->execute();
  48. }

Matching Methods for constraints

  1. // =
  2. $query->equals($myProperty, $equalsThis, $caseSensitive = true);
  3.  
  4. // <
  5. $query->lessThan($myProperty, $isLessThanThis);
  6.  
  7. // >
  8. $query->greaterThan($myProperty, $isGreaterThanThis);
  9.  
  10. // <=
  11. $query->lessThanOrEqual($myProperty, $isLessThanOrEqualThis);
  12.  
  13. // >=
  14. $query->greaterThanOrEqual($myProperty, $isGreaterThanOrEqualThis);
  15.  
  16. // %search%
  17. $query->like($myProperty, $theLikeString);
  18.  
  19. // contains
  20. $query->contains($myProperty, $hasThisValue);
  21.  
  22. // in
  23. $query->in($myProperty, $isInThisObjectOrArray);

Query-Settings

  1. // METHOD 1: Presets for Repository
  2. public function initializeObject() {
  3. // get the settings
  4. $querySettings = $this->objectManager->get('TYPO3\\CMS\\Extbase\\Persistence\\Generic\\Typo3QuerySettings');
  5.  
  6. // modify the settings
  7. $querySettings->setSomething();
  8.  
  9. // store the settings as default-values
  10. $this->setDefaultQuerySettings($querySettings);
  11. }
  12.  
  13. // METHOD 2: Set only in Repo-Method
  14. public function findSomething() {
  15. $query = $this->createQuery();
  16. $query->getQuerySettings()->setSomething();
  17. // ...
  18. return $query->execute();
  19. }
  20.  
  21. // HERE are the settings:
  22. $querySettings->setRespectStoragePage(FALSE); // ignore the storagePid
  23. $querySettings->setStoragePageIds(array(1, 2, 3)); // set some special storagePids
  24. $querySettings->setRespectEnableFields(FALSE); // ignore enableFields (…is deprecated)
  25. $querySettings->setIgnoreEnableFields(TRUE); // ignore the fields which are defined in TCA in key "enablecolumns"
  26. $querySettings->setEnableFieldsToBeIgnored(array('disabled', 'starttime')); // only ignore single enableFields
  27. $querySettings->setIncludeDeleted(TRUE); // also find the deleted rows
  28. $querySettings->setRespectSysLanguage(FALSE); // ignore the sys_language
  29. $querySettings->setSysLanguageUid(2); // set a special sys_language

Force the repository to do the database stuff

  1. /*
  2.  * Action-Method in Controller...
  3.  */
  4. // create new Model-Object
  5. $newItem = new \VENDOR\MyExt\Domain\Model\Item();
  6. $newItem->setSomeStuff();
  7. $myItemRepository->add($newItem);
  8.  
  9. // now persist all to have the possibility to use the new ITEM-UID p.e. in view...
  10. $persistenceManager = $this->objectManager->get('TYPO3\\CMS\\Extbase\\Persistence\\Generic\\PersistenceManager');
  11. $persistenceManager->persistAll();
  12.  
  13. $this->view->assign("newItem", $newItem);
  14.  
  15. /*
  16.  * Fluid-View
  17.  */
  18. <f:link.action controller="Item" action="show" arguments="{item:newItem}">show new item</f:link.action>