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

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. class QueryBuilder
  17. {
  18. /**
  19. * Build a query based on the given options
  20. *
  21. * @param array $arrOptions The options array
  22. *
  23. * @return string The query string
  24. */
  25. public static function find(array $arrOptions)
  26. {
  27. $objBase = DcaExtractor::getInstance($arrOptions['table']);
  28. if (!$objBase->hasRelations())
  29. {
  30. $strQuery = "SELECT * FROM " . $arrOptions['table'];
  31. }
  32. else
  33. {
  34. $arrJoins = array();
  35. $arrFields = array($arrOptions['table'] . ".*");
  36. $intCount = 0;
  37. foreach ($objBase->getRelations() as $strKey=>$arrConfig)
  38. {
  39. // Automatically join the single-relation records
  40. if (($arrConfig['load'] ?? null) == 'eager' || ($arrOptions['eager'] ?? null))
  41. {
  42. if ($arrConfig['type'] == 'hasOne' || $arrConfig['type'] == 'belongsTo')
  43. {
  44. ++$intCount;
  45. $objRelated = DcaExtractor::getInstance($arrConfig['table']);
  46. foreach (array_keys($objRelated->getFields()) as $strField)
  47. {
  48. $arrFields[] = 'j' . $intCount . '.' . Database::quoteIdentifier($strField) . ' AS ' . $strKey . '__' . $strField;
  49. }
  50. $arrJoins[] = " LEFT JOIN " . $arrConfig['table'] . " j$intCount ON " . $arrOptions['table'] . "." . Database::quoteIdentifier($strKey) . "=j$intCount." . $arrConfig['field'];
  51. }
  52. }
  53. }
  54. // Generate the query
  55. $strQuery = "SELECT " . implode(', ', $arrFields) . " FROM " . $arrOptions['table'] . implode("", $arrJoins);
  56. }
  57. // Where condition
  58. if (isset($arrOptions['column']))
  59. {
  60. $strQuery .= " WHERE " . (\is_array($arrOptions['column']) ? implode(" AND ", $arrOptions['column']) : $arrOptions['table'] . '.' . Database::quoteIdentifier($arrOptions['column']) . "=?");
  61. }
  62. // Group by
  63. if (isset($arrOptions['group']))
  64. {
  65. 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.');
  66. $strQuery .= " GROUP BY " . $arrOptions['group'];
  67. }
  68. // Having (see #6446)
  69. if (isset($arrOptions['having']))
  70. {
  71. $strQuery .= " HAVING " . $arrOptions['having'];
  72. }
  73. // Order by
  74. if (isset($arrOptions['order']))
  75. {
  76. $strQuery .= " ORDER BY " . $arrOptions['order'];
  77. }
  78. return $strQuery;
  79. }
  80. /**
  81. * Build a query based on the given options to count the number of records
  82. *
  83. * @param array $arrOptions The options array
  84. *
  85. * @return string The query string
  86. */
  87. public static function count(array $arrOptions)
  88. {
  89. $strQuery = "SELECT COUNT(*) AS count FROM " . $arrOptions['table'];
  90. if (isset($arrOptions['column']))
  91. {
  92. $strQuery .= " WHERE " . (\is_array($arrOptions['column']) ? implode(" AND ", $arrOptions['column']) : $arrOptions['table'] . '.' . Database::quoteIdentifier($arrOptions['column']) . "=?");
  93. }
  94. return $strQuery;
  95. }
  96. }
  97. class_alias(QueryBuilder::class, 'Model\QueryBuilder');