Most important functions and usages

Query object

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

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

// = 
$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

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

/*
 * 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>

From: http://lbrmedia.net/codebase/Eintrag/extbase-query-methods/

Be the first to comment

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.