Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- * ListSelection Query Example - REMOVE when finished
- */
- /**
- * @param array $fromSelectColumns required columns to display in result
- * @param string $fromTable table to lookup
- * @param string $fromTableAlias alias for this table
- * @param string $idColumn the column to orderby
- * @param array $whereIs array of Conditions expected i.e. [[alias, column, compoarison, value = null],...]
- * @param array $groupBy array of tableAlias and column we need to groupBy accordingly i.e. ['alias','column']
- * @param array $joinTables array of joinTables with array of alias and columns
- * i.e. ['joinTable' => ['alias' => '<youralias>', 'joinColumn' => '<yourjoinrColumn>'],...],
- *
- * @return array
- */
- public function getListForSelection(
- array $fromSelectColumns, string $fromTable, string $fromTableAlias, string $idColumn
- , array $whereIs = null, array $groupBy = null, array $joinTables = null
- ) {
- /** @var \TYPO3\CMS\Core\Database\Query\QueryBuilder $queryBuilder */
- $queryBuilder = \TYPO3\CMS\Core\Utility\GeneralUtility::makeInstance(\TYPO3\CMS\Core\Database\ConnectionPool::class)->getQueryBuilderForTable($fromTable);
- /** @var \Doctrine\DBAL\Driver\PDOStatement $resultRow */
- // SELECT columns
- foreach($fromSelectColumns as $column) {
- if ($this->endsWith($column, '_desc') || in_array($column, $this->badLanguageTables)) {
- $queryBuilder->addSelect(
- $fromTableAlias . '.' . $column . '_' . $this->languageKey. ' as '. $column
- );
- } else {
- $queryBuilder->addSelect(
- $fromTableAlias . '.' . $column
- );
- }
- }
- // FROM
- $queryBuilder->from(
- $fromTable, $fromTableAlias
- );
- // LEFT JOIN
- // check if we want to join tables
- if (is_array($joinTables)){
- // alias for right table in left joins
- $fromTableAliasToJoin = $fromTableAlias;
- foreach($joinTables as $joinTable => $join) {
- //pull tablename
- $joinTableName = $joinTable;
- // get array keys to refernce in lookup, they may be called different then expected
- $keys = array_keys($join);
- //we expect and assume following order
- $joinAlias = $join[$keys[0]];
- $joinColumn = $join[$keys[1]];
- // add tables to joins
- $queryBuilder->leftJoin(
- $fromTableAliasToJoin,
- $joinTableName, $joinAlias,
- $queryBuilder->expr()->eq(
- $fromTableAliasToJoin . '.' . $joinColumn,
- $queryBuilder->quoteIdentifier($joinAlias . '.' . $joinColumn)
- )
- );
- // add columns from joined Table to select
- $queryBuilder->addSelect(
- $joinAlias . '.' . $joinColumn
- );
- // prepare alias variable for next loop
- $fromTableAliasToJoin = $joinAlias;
- }
- }
- // WHERE
- // we want array to be [[alias, column, compoarison, value = null],...]
- if ($whereIs) {
- foreach (
- $whereIs as list(
- $whereAlias,
- $whereColumn,
- $whereComparison,
- $whereValue)
- ) {
- switch ($whereComparison) {
- case 'gt':
- $queryBuilder->andWhere(
- $queryBuilder->expr()->gt($whereAlias . '.' . $whereColumn
- , $queryBuilder->createNamedParameter($whereValue, \PDO::PARAM_STR))
- );
- break;
- case 'eq':
- $queryBuilder->andWhere(
- $queryBuilder->expr()->eq($whereAlias . '.' . $whereColumn
- , $queryBuilder->createNamedParameter($whereValue, \PDO::PARAM_STR))
- );
- break;
- }
- }
- }
- // ORDER BY
- $queryBuilder->orderBy($fromTableAlias . '.' . $idColumn);
- if($groupBy) {
- foreach($groupBy as $value) {
- $queryBuilder->addGroupBy($value);
- }
- }
- $selectionList = $queryBuilder->execute();
- return $selectionList->fetchAll();
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement