| 1 | <?php |
||
| 15 | trait ColumnsTrait |
||
| 16 | { |
||
| 17 | /** |
||
| 18 | * Set columns into SelectQuery. |
||
| 19 | * |
||
| 20 | * @param SelectQuery $query |
||
| 21 | * @param bool $minify Minify column names (will work in case when query parsed in |
||
| 22 | * FETCH_NUM mode). |
||
| 23 | * @param string $prefix Prefix to be added for each column name. |
||
| 24 | * @param bool $overwrite When set to true existed columns will be removed. |
||
| 25 | */ |
||
| 26 | protected function mountColumns( |
||
| 27 | SelectQuery $query, |
||
| 28 | bool $minify = false, |
||
| 29 | string $prefix = '', |
||
| 30 | bool $overwrite = false |
||
| 31 | ) { |
||
| 32 | //Column source alias |
||
| 33 | $alias = $this->getAlias(); |
||
| 34 | |||
| 35 | $columns = $overwrite ? [] : $query->getColumns(); |
||
| 36 | foreach ($this->getColumns() as $name) { |
||
| 37 | $column = $name; |
||
| 38 | |||
| 39 | if ($minify) { |
||
| 40 | //Let's use column number instead of full name |
||
| 41 | $column = 'c' . count($columns); |
||
| 42 | } |
||
| 43 | |||
| 44 | $columns[] = "{$alias}.{$name} AS {$prefix}{$column}"; |
||
| 45 | } |
||
| 46 | |||
| 47 | //Updating column set |
||
| 48 | $query->columns($columns); |
||
| 49 | } |
||
| 50 | |||
| 51 | /** |
||
| 52 | * Joined table alias. |
||
| 53 | * |
||
| 54 | * @return string |
||
| 55 | */ |
||
| 56 | abstract protected function getAlias(): string; |
||
| 57 | |||
| 58 | /** |
||
| 59 | * list of columns to be loaded. |
||
| 60 | * |
||
| 61 | * @return array |
||
| 62 | */ |
||
| 63 | abstract protected function getColumns(): array; |
||
| 64 | } |