Elgg  Version 6.0
Repository.php
Go to the documentation of this file.
1 <?php
2 
3 namespace Elgg\Database;
4 
12 
16 abstract class Repository {
17 
19 
25  public function __construct(array $options = []) {
26  $this->options = new QueryOptions($options, \ArrayObject::ARRAY_AS_PROPS);
27  }
28 
32  public function __get($name) {
33  if (!isset($this->options->$name)) {
34  return;
35  }
36 
37  $val = &$this->options->$name;
38 
39  return $val;
40  }
41 
45  public function __set($name, $value) {
46  $this->options->$name = $value;
47  }
48 
52  public function __unset($name) {
53  unset($this->options->$name);
54  }
55 
59  public function __isset($name) {
60  return isset($this->options->$name);
61  }
62 
70  public static function with(array $options = []) {
71  return new static($options);
72  }
73 
79  abstract public function count();
80 
90  abstract public function calculate($function, $property, $property_type = null);
91 
101  abstract public function get($limit = null, $offset = null, $callback = null);
102 
110  public static function find(array $options = []) {
111  return static::with($options)->execute();
112  }
113 
123  public function batch($limit = null, $offset = null, $callback = null) {
124  $options = $this->options->getArrayCopy();
125 
126  $options['limit'] = (int) $limit;
127  $options['offset'] = (int) $offset;
128  $options['callback'] = $callback;
129  unset($options['count'],
130  $options['batch'],
131  $options['batch_size'],
132  $options['batch_inc_offset']
133  );
134 
135  $batch_size = $this->options->batch_size;
136  $batch_inc_offset = $this->options->batch_inc_offset;
137 
138  return new \ElggBatch([static::class, 'find'], $options, null, $batch_size, $batch_inc_offset);
139  }
140 
146  abstract public function execute();
147 
158  public function filter(\Closure $closure) {
159  $this->options->where(new WhereClause($closure));
160 
161  return $this;
162  }
163 
171  public function select($expression) {
172  $this->options->select(new SelectClause($expression));
173 
174  return $this;
175  }
176 
193  public function join($joined_table, $joined_alias = null, $join_column = null, $comparison = null, $values = null, $type = null, $case_sensitive = null) {
194  $join = new JoinClause($joined_table, $joined_alias, function (QueryBuilder $qb, $joined_alias) use ($join_column, $comparison, $values, $type, $case_sensitive) {
195  return $qb->compare("{$joined_alias}.{$join_column}", $comparison, $values, $type, $case_sensitive);
196  });
197  $this->options->join($join);
198 
199  return $this;
200  }
201 
209  public function groupBy($expression) {
210  $this->options->groupBy(new GroupByClause($expression));
211 
212  return $this;
213  }
214 
222  public function having($expression) {
223  $this->options->having(new HavingClause($expression));
224 
225  return $this;
226  }
227 
236  public function orderBy($expression, $direction) {
237  $this->options->orderBy(new OrderByClause($expression, $direction));
238 
239  return $this;
240  }
241 
250  public function expandInto(QueryBuilder $qb, $table_alias = null) {
251  foreach ($this->options->selects as $select_clause) {
252  $select_clause->prepare($qb, $table_alias);
253  }
254 
255  foreach ($this->options->group_by as $group_by_clause) {
256  $group_by_clause->prepare($qb, $table_alias);
257  }
258 
259  foreach ($this->options->having as $having_clause) {
260  $having_clause->prepare($qb, $table_alias);
261  }
262 
263  if (!empty($this->options->order_by)) {
264  foreach ($this->options->order_by as $order_by_clause) {
265  $order_by_clause->prepare($qb, $table_alias);
266  }
267  }
268  }
269 }
batch($limit=null, $offset=null, $callback=null)
Fetch rows as an ElggBatch.
Definition: Repository.php:123
calculate($function, $property, $property_type=null)
Apply numeric calculation to a column.
static find(array $options=[])
Build and execute a new query from an array of legacy options.
Definition: Repository.php:110
if(!$user||!$user->canDelete()) $name
Definition: delete.php:22
if(empty($count)) $offset
Definition: pagination.php:26
Extends QueryBuilder with JOIN clauses.
Definition: JoinClause.php:11
Extends QueryBuilder with SELECT clauses.
Database abstraction query builder.
$type
Definition: delete.php:21
join($joined_table, $joined_alias=null, $join_column=null, $comparison=null, $values=null, $type=null, $case_sensitive=null)
Add JOIN clause Join a database table on an $x to $y comparison.
Definition: Repository.php:193
Extends QueryBuilder with GROUP BY statements.
$value
Definition: generic.php:51
filter(\Closure $closure)
Filter query prior to execution Callback function will receive QueryBuilder as the first argument and...
Definition: Repository.php:158
Abstract methods for interfacing with the database.
Definition: Repository.php:16
$limit
Definition: pagination.php:28
Extends QueryBuilder with HAVING clauses.
compare(string $x, string $comparison, $y=null, string $type=null, bool $case_sensitive=null)
Build value comparison clause.
expandInto(QueryBuilder $qb, $table_alias=null)
Extend query builder with select, group_by, having and order_by clauses from $options.
Definition: Repository.php:250
select($expression)
Add SELECT.
Definition: Repository.php:171
Extends QueryBuilder with ORDER BY clauses.
Builds a clause from closure or composite expression.
Definition: WhereClause.php:11
groupBy($expression)
Add GROUP BY.
Definition: Repository.php:209
__set($name, $value)
{}
Definition: Repository.php:45
having($expression)
Add HAVING.
Definition: Repository.php:222
orderBy($expression, $direction)
Add ORDER BY.
Definition: Repository.php:236
__construct(array $options=[])
Constructor.
Definition: Repository.php:25
$qb
Definition: queue.php:12
static with(array $options=[])
Constructs a new.
Definition: Repository.php:70
execute()
Apply correct execution method based on calculation, count or other criteria.