Skip to content

Instantly share code, notes, and snippets.

@s2b
Last active August 29, 2015 14:06
Show Gist options
  • Save s2b/0bb5585226f8591afdf8 to your computer and use it in GitHub Desktop.
Save s2b/0bb5585226f8591afdf8 to your computer and use it in GitHub Desktop.
TYPO3/ExtBase: Repository helper functions for custom queries http://somethingphp.com/custom-queries-in-extbase/
<?php
class MyRepository extends \TYPO3\CMS\Extbase\Persistence\Repository {
/**
* Returns an enableFields SQL statement for the specified table
* @param string $tableName name of the database table
* @return string enableFields SQL statement
*/
protected function enableFields($tableName) {
if (TYPO3_MODE === 'FE') {
// Use enableFields in frontend mode
$enableFields = $GLOBALS['TSFE']->sys_page->enableFields($tableName);
} else {
// Use enableFields in backend mode
$enableFields = \TYPO3\CMS\Backend\Utility\BackendUtility::deleteClause($tableName);
$enableFields .= \TYPO3\CMS\Backend\Utility\BackendUtility::BEenableFields($tableName);
}
return $enableFields;
}
/**
* Escapes a string for use in a database query
* Note that this function does not add single quotes
* around the string (see fullQuoteStr())
* @param string $string string that should be escaped
* @param string $tableName name of the database table
* can be omitted without consequences
* @return string escaped string
*/
protected function quoteStr($string, $tableName = '') {
return $GLOBALS['TYPO3_DB']->quoteStr($string, $tableName);
}
/**
* Returns an SQL statement that checks for one or multiple storage pids
* @param \TYPO3\CMS\Extbase\Persistence\QueryInterface $query query object
* @param string $tableName name of database table
* @return string storage pid SQL statement
*/
protected function storagePidStatement(\TYPO3\CMS\Extbase\Persistence\QueryInterface $query, $tableName = '') {
// Get allowed storage pids
$storagePids = $query->getQuerySettings()->getStoragePageIds();
// Sanitize them (just to be sure)
$storagePids = array_map('intval', $storagePids);
// Generate SQL
$tableField = ($tableName) ? $tableName . '.pid' : 'pid';
return " AND $tableField IN (" . implode(', ', $storagePids) . ') ';
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment