Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
SelectBuilder.php
Go to the documentation of this file.
1 <?php
8 
11 
17 {
21  private $resourceConnection;
22 
26  private $connectionName;
27 
31  private $from;
32 
36  private $group = [];
37 
41  private $columns = [];
42 
46  private $filters = [];
47 
51  private $joins = [];
52 
56  private $params = [];
57 
61  private $having = [];
62 
68  public function __construct(
69  ResourceConnection $resourceConnection
70  ) {
71  $this->resourceConnection = $resourceConnection;
72  }
73 
79  public function getJoins()
80  {
81  return $this->joins;
82  }
83 
90  public function setJoins($joins)
91  {
92  $this->joins = $joins;
93  }
94 
100  public function getConnectionName()
101  {
102  return $this->connectionName;
103  }
104 
111  public function setConnectionName($connectionName)
112  {
113  $this->connectionName = $connectionName;
114  }
115 
121  public function getColumns()
122  {
123  return $this->columns;
124  }
125 
132  public function setColumns($columns)
133  {
134  $this->columns = $columns;
135  }
136 
142  public function getFilters()
143  {
144  return $this->filters;
145  }
146 
153  public function setFilters($filters)
154  {
155  $this->filters = $filters;
156  }
157 
163  public function getFrom()
164  {
165  return $this->from;
166  }
167 
174  public function setFrom($from)
175  {
176  $this->from = $from;
177  }
178 
186  private function processJoin(Select $select, $joinConfig)
187  {
188  switch ($joinConfig['link-type']) {
189  case 'left':
190  $select->joinLeft($joinConfig['table'], $joinConfig['condition'], []);
191  break;
192  case 'inner':
193  $select->joinInner($joinConfig['table'], $joinConfig['condition'], []);
194  break;
195  case 'right':
196  $select->joinRight($joinConfig['table'], $joinConfig['condition'], []);
197  break;
198  }
199  return $select;
200  }
201 
207  public function create()
208  {
209  $connection = $this->resourceConnection->getConnection($this->getConnectionName());
210  $select = $connection->select();
211  $select->from($this->getFrom(), []);
212  $select->columns($this->getColumns());
213  foreach ($this->getFilters() as $filter) {
214  $select->where($filter);
215  }
216  foreach ($this->getJoins() as $joinConfig) {
217  $select = $this->processJoin($select, $joinConfig);
218  }
219  if (!empty($this->getGroup())) {
220  $select->group(implode(', ', $this->getGroup()));
221  }
222  return $select;
223  }
224 
230  public function getGroup()
231  {
232  return $this->group;
233  }
234 
241  public function setGroup($group)
242  {
243  $this->group = $group;
244  }
245 
251  public function getParams()
252  {
253  return $this->params;
254  }
255 
262  public function setParams($params)
263  {
264  $this->params = $params;
265  }
266 
272  public function getHaving()
273  {
274  return $this->having;
275  }
276 
283  public function setHaving($having)
284  {
285  $this->having = $having;
286  }
287 }
group($spec)
Definition: Select.php:526
columns($cols=' *', $correlationName=null)
Definition: Select.php:264
__construct(ResourceConnection $resourceConnection)
$connection
Definition: bulk.php:13