chris-at-github
11/3/2015 - 3:35 PM

Custom queries in extbase https://somethingphp.com/custom-queries-in-extbase/

<?php
	/**
	 * 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) . ') ';
	}