Advertisement
snowowl78

Generic Select List Query

Dec 6th, 2019
150
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 4.52 KB | None | 0 0
  1.     /*
  2.      * ListSelection Query Example - REMOVE when finished
  3.      */
  4.  
  5.     /**
  6.      * @param array $fromSelectColumns required columns to display in result
  7.      * @param string $fromTable table to lookup
  8.      * @param string $fromTableAlias alias for this table
  9.      * @param string $idColumn the column to orderby
  10.      * @param array $whereIs array of Conditions expected i.e. [[alias, column, compoarison, value = null],...]
  11.      * @param array $groupBy array of tableAlias and column we need to groupBy accordingly i.e. ['alias','column']
  12.      * @param array $joinTables array of joinTables with array of alias and columns
  13.      *     i.e. ['joinTable' => ['alias' => '<youralias>', 'joinColumn' => '<yourjoinrColumn>'],...],
  14.      *
  15.      * @return array
  16.      */
  17.     public function getListForSelection(
  18.         array $fromSelectColumns, string $fromTable, string $fromTableAlias, string $idColumn
  19.         , array $whereIs = null, array $groupBy = null, array $joinTables = null
  20.     ) {
  21.         /** @var \TYPO3\CMS\Core\Database\Query\QueryBuilder $queryBuilder */
  22.         $queryBuilder = \TYPO3\CMS\Core\Utility\GeneralUtility::makeInstance(\TYPO3\CMS\Core\Database\ConnectionPool::class)->getQueryBuilderForTable($fromTable);
  23.         /** @var \Doctrine\DBAL\Driver\PDOStatement $resultRow */
  24.  
  25.         // SELECT columns
  26.         foreach($fromSelectColumns as $column) {
  27.             if ($this->endsWith($column, '_desc') || in_array($column, $this->badLanguageTables)) {
  28.                 $queryBuilder->addSelect(
  29.                     $fromTableAlias . '.' . $column . '_' . $this->languageKey. ' as '. $column
  30.                 );
  31.             } else {
  32.                 $queryBuilder->addSelect(
  33.                     $fromTableAlias . '.' . $column
  34.                 );
  35.             }
  36.         }
  37.  
  38.         // FROM
  39.         $queryBuilder->from(
  40.             $fromTable, $fromTableAlias
  41.         );
  42.  
  43.         // LEFT JOIN
  44.         // check if we want to join tables
  45.         if (is_array($joinTables)){
  46.  
  47.             // alias for right table in left joins
  48.             $fromTableAliasToJoin = $fromTableAlias;
  49.  
  50.             foreach($joinTables as $joinTable => $join) {
  51.  
  52.                 //pull tablename
  53.                 $joinTableName = $joinTable;
  54.  
  55.                 // get array keys to refernce in lookup, they may be called different then expected
  56.                 $keys = array_keys($join);
  57.  
  58.                 //we expect and assume following order
  59.                 $joinAlias = $join[$keys[0]];
  60.                 $joinColumn = $join[$keys[1]];
  61.  
  62.                 // add tables to joins
  63.                 $queryBuilder->leftJoin(
  64.                     $fromTableAliasToJoin,
  65.                     $joinTableName, $joinAlias,
  66.                     $queryBuilder->expr()->eq(
  67.                         $fromTableAliasToJoin . '.' . $joinColumn,
  68.                         $queryBuilder->quoteIdentifier($joinAlias . '.' . $joinColumn)
  69.                     )
  70.                 );
  71.  
  72.                 // add columns from joined Table to select
  73.                 $queryBuilder->addSelect(
  74.                     $joinAlias . '.' . $joinColumn
  75.                 );
  76.  
  77.                 // prepare alias variable for next loop
  78.                 $fromTableAliasToJoin = $joinAlias;
  79.             }
  80.  
  81.         }
  82.  
  83.         // WHERE
  84.         // we want array to be [[alias, column, compoarison, value = null],...]
  85.         if ($whereIs) {
  86.             foreach (
  87.                 $whereIs as list(
  88.                 $whereAlias,
  89.                 $whereColumn,
  90.                 $whereComparison,
  91.                 $whereValue)
  92.             ) {
  93.                 switch ($whereComparison) {
  94.                     case 'gt':
  95.                         $queryBuilder->andWhere(
  96.                             $queryBuilder->expr()->gt($whereAlias . '.' . $whereColumn
  97.                                 , $queryBuilder->createNamedParameter($whereValue, \PDO::PARAM_STR))
  98.                         );
  99.                         break;
  100.                     case 'eq':
  101.                         $queryBuilder->andWhere(
  102.                             $queryBuilder->expr()->eq($whereAlias . '.' . $whereColumn
  103.                                 , $queryBuilder->createNamedParameter($whereValue, \PDO::PARAM_STR))
  104.                         );
  105.                         break;
  106.                 }
  107.             }
  108.         }
  109.  
  110.         // ORDER BY
  111.         $queryBuilder->orderBy($fromTableAlias . '.' . $idColumn);
  112.         if($groupBy) {
  113.             foreach($groupBy as $value) {
  114.                 $queryBuilder->addGroupBy($value);
  115.             }
  116.         }
  117.         $selectionList = $queryBuilder->execute();
  118.         return $selectionList->fetchAll();
  119.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement