Benutzer: Gast
Eintrag: Extbase: Query and Repository Overview

Extbase: Query and Repository Overview

von: marcel
Tags: Extbase, Typo3
Kreise: Typo3

Query-Object in Repository-Method

Code: PHP
// INITialize query-object
$query = $this->createQuery();

// AND / OR / NOT
$query->logicalAnd($constraints);
$query->logicalOr($constraints);
$query->logicalNot($constraints);

// A direct SQL-Statement
$query->statement("SELECT * FROM my_table WHERE 1 " . $GLOBALS['TSFE']->sys_page->enableFields("my_table")); // string

// ORDER BY
$query->setOrderings(array("field" => \TYPO3\CMS\Extbase\Persistence\QueryInterface::ORDER_ASCENDING)); // array
$query->setOrderings(array("field" => \TYPO3\CMS\Extbase\Persistence\QueryInterface::ORDER_DESCENDING)); // array

// LIMIT
$query->setLimit($limit); // integer

// OFFSET
$query->setOffset($offset); // integer

// only COUNT()
$query->count(); // execute query and get amount of matches

// execute query
$query->execute();

// only get the first result (LIMIT 1)
$query->getFirst();

/*
 * typical method
 */
public function findSomethingSpecial() {
  $query = $this->createQuery();
  $query->matching(
    $query->logicalAnd(
      $query->equals("mySpecialProperty", "isMySpecialValue"),
      $query->equals("someOtherProperty", "isAnotherValue")
    )
  );

  $query->setOrderings(array("crdate" => \TYPO3\CMS\Extbase\Persistence\QueryInterface::ORDER_ASCENDING))
    ->setOffset(10)
    ->setLimit(20);

  return $query->execute();
}

Standard-Repository-Methods

Code: PHP
// add
$myRepository->add($myModel);

// remove
$myRepository->remove($myModel);

// removeAll
$myRepository->removeAll(); // don't do this at home

// replace
$myRepository->replace($myModel, $myOtherModel);

// update
$myRepository->update($myModifiedModel);

// count ALL
$myRepository->countAll(); // get the amount of all objects

// count constraint
$myRepository->countBy[myProperty]($thisMustMatchMyProperty);

// UID
$myRepository->findByUid($theUid); // returns ONE Model-Object (...if found)

// all
$myRepository->findAll(); // only constraint by enablecolumns, language and storagePid

// findBy...
$myRepository->findBy[myProperty]($thisMustMatchMyProperty); // returns a Storage-object

// findOneBy...
$myRepository->findOneBy[myProperty]($thisMustMatchMyProperty); // returns ONE Model-Object (...if found)

"Matching"-Methods for constraints

Code: PHP
// = 
$query->equals($myProperty, $equalsThis, $caseSensitive = true);

// <
$query->lessThan($myProperty, $isLessThanThis);

// >
$query->greaterThan($myProperty, $isGreaterThanThis);

// <=
$query->lessThanOrEqual($myProperty, $isLessThanOrEqualThis);

// >=
$query->greaterThanOrEqual($myProperty, $isGreaterThanOrEqualThis);

// %search%
$query->like($myProperty, $theLikeString);

// contains
$query->contains($myProperty, $hasThisValue);

// in
$query->in($myProperty, $isInThisObjectOrArray);

Query-Settings

Code: PHP
// METHOD 1: Presets for Repository
public function initializeObject() {
  // get the settings
  $querySettings = $this->objectManager->get('TYPO3\\CMS\\Extbase\\Persistence\\Generic\\Typo3QuerySettings');

  // modify the settings
  $querySettings->setSomething();

  // store the settings as default-values
  $this->setDefaultQuerySettings($querySettings);
}

// METHOD 2: Set only in Repo-Method
public function findSomething() {
  $query = $this->createQuery();
  $query->getQuerySettings()->setSomething();
  // ...
  return $query->execute();
}

// HERE are the settings:
$querySettings->setRespectStoragePage(FALSE); // ignore the storagePid
$querySettings->setStoragePageIds(array(1, 2, 3)); // set some special storagePids
$querySettings->setRespectEnableFields(FALSE); // ignore enableFields (…is deprecated)
$querySettings->setIgnoreEnableFields(TRUE); // ignore the fields which are defined in TCA in key "enablecolumns"
$querySettings->setEnableFieldsToBeIgnored(array('disabled', 'starttime')); // only ignore single enableFields
$querySettings->setIncludeDeleted(TRUE); // also find the deleted rows
$querySettings->setRespectSysLanguage(FALSE); // ignore the sys_language
$querySettings->setSysLanguageUid(2); // set a special sys_language

Force the repository to do the database stuff

Code: PHP
/*
 * Action-Method in Controller...
 */
// create new Model-Object
$newItem = new \VENDOR\MyExt\Domain\Model\Item();
$newItem->setSomeStuff();
$myItemRepository->add($newItem);

// now persist all to have the possibility to use the new ITEM-UID p.e. in view...
$persistenceManager = $this->objectManager->get('TYPO3\\CMS\\Extbase\\Persistence\\Generic\\PersistenceManager');
$persistenceManager->persistAll();

$this->view->assign("newItem", $newItem);

/*
 * Fluid-View
 */
<f:link.action controller="Item" action="show" arguments="{item:newItem}">show new item</f:link.action>
(1) Michael Gross schrieb am 25. 11. 2013 um 12:25 Uhr
You should write the manual for extbase! Very nice overview!
(2) Jainish Senjaliya schrieb am 14. 3. 2014 um 14:40 Uhr
Thanks for share :)
(3) NEOline neoline schrieb am 1. 4. 2014 um 10:14 Uhr
Perfect, good to have it like a Cheat Sheet.
Danke!
(4) Maulik Bhojani schrieb am 11. 4. 2014 um 09:13 Uhr
It's perfect. I always use this page for reference while working with extbase.
Thanks :)
(5) Unknown not given schrieb am 7. 5. 2014 um 09:31 Uhr
I think its very much helpful if example is given :)
(6) Vivek Parmar schrieb am 21. 1. 2015 um 07:55 Uhr
Thanks for share,its really helpful..
(7) Test Tester schrieb am 30. 7. 2015 um 19:54 Uhr
Es würde hilfreicher sein, wenn dieses noch korrigiert werden würde:
"$query->logicalNot($constraints); // commaseparated list of constraints or array"

Richtig ist:
$query->logicalNot($constraint); // constraint
(8) Ravi Dada schrieb am 28. 4. 2016 um 13:00 Uhr
Superb!...Nice One!..
(9) niks patel schrieb am 17. 3. 2017 um 14:30 Uhr
Thanks for sharing!!!!
Keep it up..