vendor/contao/core-bundle/src/Resources/contao/library/Contao/Model/QueryBuilder.php line 33

Open in your IDE?
  1. <?php
  2. /*
  3.  * This file is part of Contao.
  4.  *
  5.  * (c) Leo Feyer
  6.  *
  7.  * @license LGPL-3.0-or-later
  8.  */
  9. namespace Contao\Model;
  10. use Contao\Database;
  11. use Contao\DcaExtractor;
  12. /**
  13.  * The class reads the relation metadata from the DCA and creates the necessary
  14.  * JOIN queries to retrieve an object from the database.
  15.  *
  16.  * @author Leo Feyer <https://github.com/leofeyer>
  17.  */
  18. class QueryBuilder
  19. {
  20.     /**
  21.      * Build a query based on the given options
  22.      *
  23.      * @param array $arrOptions The options array
  24.      *
  25.      * @return string The query string
  26.      */
  27.     public static function find(array $arrOptions)
  28.     {
  29.         $objBase DcaExtractor::getInstance($arrOptions['table']);
  30.         if (!$objBase->hasRelations())
  31.         {
  32.             $strQuery "SELECT * FROM " $arrOptions['table'];
  33.         }
  34.         else
  35.         {
  36.             $arrJoins = array();
  37.             $arrFields = array($arrOptions['table'] . ".*");
  38.             $intCount 0;
  39.             foreach ($objBase->getRelations() as $strKey=>$arrConfig)
  40.             {
  41.                 // Automatically join the single-relation records
  42.                 if (($arrConfig['load'] ?? null) == 'eager' || ($arrOptions['eager'] ?? null))
  43.                 {
  44.                     if ($arrConfig['type'] == 'hasOne' || $arrConfig['type'] == 'belongsTo')
  45.                     {
  46.                         ++$intCount;
  47.                         $objRelated DcaExtractor::getInstance($arrConfig['table']);
  48.                         foreach (array_keys($objRelated->getFields()) as $strField)
  49.                         {
  50.                             $arrFields[] = 'j' $intCount '.' Database::quoteIdentifier($strField) . ' AS ' $strKey '__' $strField;
  51.                         }
  52.                         $arrJoins[] = " LEFT JOIN " $arrConfig['table'] . " j$intCount ON " $arrOptions['table'] . "." Database::quoteIdentifier($strKey) . "=j$intCount." $arrConfig['field'];
  53.                     }
  54.                 }
  55.             }
  56.             // Generate the query
  57.             $strQuery "SELECT " implode(', '$arrFields) . " FROM " $arrOptions['table'] . implode(""$arrJoins);
  58.         }
  59.         // Where condition
  60.         if (isset($arrOptions['column']))
  61.         {
  62.             $strQuery .= " WHERE " . (\is_array($arrOptions['column']) ? implode(" AND "$arrOptions['column']) : $arrOptions['table'] . '.' Database::quoteIdentifier($arrOptions['column']) . "=?");
  63.         }
  64.         // Group by
  65.         if (isset($arrOptions['group']))
  66.         {
  67.             trigger_deprecation('contao/core-bundle''4.4''Using the "group" option has been deprecated and will no longer work in Contao 5.0. See https://github.com/contao/contao/issues/1680.');
  68.             $strQuery .= " GROUP BY " $arrOptions['group'];
  69.         }
  70.         // Having (see #6446)
  71.         if (isset($arrOptions['having']))
  72.         {
  73.             $strQuery .= " HAVING " $arrOptions['having'];
  74.         }
  75.         // Order by
  76.         if (isset($arrOptions['order']))
  77.         {
  78.             $strQuery .= " ORDER BY " $arrOptions['order'];
  79.         }
  80.         return $strQuery;
  81.     }
  82.     /**
  83.      * Build a query based on the given options to count the number of records
  84.      *
  85.      * @param array $arrOptions The options array
  86.      *
  87.      * @return string The query string
  88.      */
  89.     public static function count(array $arrOptions)
  90.     {
  91.         $strQuery "SELECT COUNT(*) AS count FROM " $arrOptions['table'];
  92.         if (isset($arrOptions['column']))
  93.         {
  94.             $strQuery .= " WHERE " . (\is_array($arrOptions['column']) ? implode(" AND "$arrOptions['column']) : $arrOptions['table'] . '.' Database::quoteIdentifier($arrOptions['column']) . "=?");
  95.         }
  96.         return $strQuery;
  97.     }
  98. }
  99. class_alias(QueryBuilder::class, 'Model\QueryBuilder');