Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
SelectHydrator.php
Go to the documentation of this file.
1 <?php
8 
12 
17 {
23  private $predefinedSelectParts =
24  [
36  ];
37 
41  private $selectParts;
42 
46  private $resourceConnection;
47 
51  private $objectManager;
52 
58  public function __construct(
59  ResourceConnection $resourceConnection,
60  ObjectManagerInterface $objectManager,
61  $selectParts = []
62  ) {
63  $this->resourceConnection = $resourceConnection;
64  $this->objectManager = $objectManager;
65  $this->selectParts = $selectParts;
66  }
67 
71  private function getSelectParts()
72  {
73  return array_merge($this->predefinedSelectParts, $this->selectParts);
74  }
75 
83  public function extract(Select $select)
84  {
85  $parts = [];
86  foreach ($this->getSelectParts() as $partName) {
87  $parts[$partName] = $select->getPart($partName);
88  }
89  return $parts;
90  }
91 
96  public function recreate(array $selectParts)
97  {
98  $select = $this->resourceConnection->getConnection()->select();
99 
100  $select = $this->processColumns($select, $selectParts);
101 
102  foreach ($selectParts as $partName => $partValue) {
103  $select->setPart($partName, $partValue);
104  }
105 
106  return $select;
107  }
108 
119  private function processColumns(Select $select, array &$selectParts)
120  {
121  if (!empty($selectParts[Select::COLUMNS]) && is_array($selectParts[Select::COLUMNS])) {
122  $part = [];
123 
124  foreach ($selectParts[Select::COLUMNS] as $columnEntry) {
125  list($correlationName, $column, $alias) = $columnEntry;
126  if (is_array($column) && !empty($column['class'])) {
127  $expression = $this->objectManager->create(
128  $column['class'],
129  isset($column['arguments']) ? $column['arguments'] : []
130  );
131  $part[] = [$correlationName, $expression, $alias];
132  } else {
133  $part[] = $columnEntry;
134  }
135  }
136 
137  $select->setPart(Select::COLUMNS, $part);
138  unset($selectParts[Select::COLUMNS]);
139  }
140 
141  return $select;
142  }
143 }
const FOR_UPDATE
Definition: Select.php:57
const HAVING
Definition: Select.php:53
const ORDER
Definition: Select.php:54
$objectManager
Definition: bootstrap.php:17
__construct(ResourceConnection $resourceConnection, ObjectManagerInterface $objectManager, $selectParts=[])
const FROM
Definition: Select.php:49
const DISTINCT
Definition: Select.php:47
const COLUMNS
Definition: Select.php:48
const LIMIT_OFFSET
Definition: Select.php:56
const GROUP
Definition: Select.php:52
const WHERE
Definition: Select.php:51
const UNION
Definition: Select.php:50
if(!trim($html)) $alias
Definition: details.phtml:20
const LIMIT_COUNT
Definition: Select.php:55