Elgg  Version 3.0
Repository.php
Go to the documentation of this file.
1 <?php
2 
3 namespace Elgg\Database;
4 
11 
15 abstract class Repository implements QueryExecuting {
16 
20  protected $options;
21 
27  public function __construct(array $options = []) {
28  $this->options = new QueryOptions($options, \ArrayObject::ARRAY_AS_PROPS);
29  }
30 
34  public function __get($name) {
35  if (!isset($this->options->$name)) {
36  return;
37  }
38 
39  $val = &$this->options->$name;
40 
41  return $val;
42  }
43 
47  public function __set($name, $value) {
48  $this->options->$name = $value;
49  }
50 
54  public function __unset($name) {
55  unset($this->options->$name);
56  }
57 
61  public function __isset($name) {
62  return isset($this->options->$name);
63  }
64 
72  public static function with(array $options = []) {
73  $query = new static($options);
74 
75  return $query;
76  }
77 
85  public static function find(array $options = []) {
86  try {
87  return static::with($options)->execute();
88  } catch (\DataFormatException $e) {
89  return elgg_extract('count', $options) ? 0 : false;
90  }
91  }
92 
96  public function batch($limit = null, $offset = null, $callback = null) {
97 
98  $options = $this->options->getArrayCopy();
99 
100  $options['limit'] = (int) $limit;
101  $options['offset'] = (int) $offset;
102  $options['callback'] = $callback;
103  unset($options['count'],
104  $options['batch'],
105  $options['batch_size'],
106  $options['batch_inc_offset']
107  );
108 
109  $batch_size = $this->options->batch_size;
110  $batch_inc_offset = $this->options->batch_inc_offset;
111 
112  return new \ElggBatch([static::class, 'find'], $options, null, $batch_size, $batch_inc_offset);
113  }
114 
118  public function filter(\Closure $closure) {
119  $this->options->where(new WhereClause($closure));
120 
121  return $this;
122  }
123 
127  public function select($expression) {
128  $this->options->select(new SelectClause($expression));
129 
130  return $this;
131  }
132 
136  public function join($joined_table, $joined_alias = null, $x = null, $comparison = null, $y = null, $type = null, $case_sensitive = null) {
137  $join = new JoinClause($joined_table, $joined_alias, function (QueryBuilder $qb, $joined_alias) use ($x, $comparison, $y, $type, $case_sensitive) {
138  return $qb->compare("$joined_alias.$x", $comparison, $y, $type, $case_sensitive);
139  });
140  $this->options->join($join);
141 
142  return $this;
143  }
144 
148  public function groupBy($expression) {
149  $this->options->groupBy(new GroupByClause($expression));
150 
151  return $this;
152  }
153 
157  public function having($expression) {
158  $this->options->having(new HavingClause($expression));
159 
160  return $this;
161  }
162 
166  public function orderBy($expression, $direction) {
167  $this->options->orderBy(new OrderByClause($expression, $direction));
168 
169  return $this;
170  }
171 
180  public function expandInto(QueryBuilder $qb, $table_alias = null) {
181  foreach ($this->options->selects as $select_clause) {
182  $select_clause->prepare($qb, $table_alias);
183  }
184 
185  foreach ($this->options->group_by as $group_by_clause) {
186  $group_by_clause->prepare($qb, $table_alias);
187  }
188 
189  foreach ($this->options->having as $having_clause) {
190  $having_clause->prepare($qb, $table_alias);
191  }
192 
193  if (!empty($this->options->order_by)) {
194  foreach ($this->options->order_by as $order_by_clause) {
195  $order_by_clause->prepare($qb, $table_alias);
196  }
197  }
198  }
199 }
$query
Definition: groups.php:8
if(!$user||!$user->canDelete()) $name
Definition: delete.php:22
batch($limit=null, $offset=null, $callback=null)
{Fetch rows as an ElggBatch.Number of rows to fetch Index of the first row Callback function to run d...
Definition: Repository.php:96
static find(array $options=[])
Build and execute a new query from an array of legacy options.
Definition: Repository.php:85
Extends QueryBuilder with JOIN clauses.
Definition: JoinClause.php:12
Extends QueryBuilder with SELECT clauses.
if(!$count) $offset
Definition: pagination.php:26
Database abstraction query builder.
$type
Definition: delete.php:21
Extends QueryBuilder with GROUP BY statements.
filter(\Closure $closure)
{}
Definition: Repository.php:118
Abstract methods for interfacing with the database.
Definition: Repository.php:15
$limit
Definition: userpicker.php:52
Extends QueryBuilder with HAVING clauses.
join($joined_table, $joined_alias=null, $x=null, $comparison=null, $y=null, $type=null, $case_sensitive=null)
{Add JOIN clause Join a database table on an $x to $y comparison.Name of the table (with or without d...
Definition: Repository.php:136
expandInto(QueryBuilder $qb, $table_alias=null)
Extend query builder with select, group_by, having and order_by clauses from $options.
Definition: Repository.php:180
compare($x, $comparison, $y=null, $type=null, $case_sensitive=null)
Build value comparison clause.
select($expression)
{Add SELECT.Selectstatic}
Definition: Repository.php:127
elgg_extract($key, $array, $default=null, $strict=true)
Checks for $array[$key] and returns its value if it exists, else returns $default.
Definition: elgglib.php:1131
Extends QueryBuilder with ORDER BY clauses.
$value
Definition: debugging.php:7
Builds a clause from closure or composite expression.
Definition: WhereClause.php:12
groupBy($expression)
{Add GROUP BY.Group bystatic}
Definition: Repository.php:148
__set($name, $value)
{}
Definition: Repository.php:47
class
Definition: placeholder.php:21
having($expression)
{Add HAVING.Havingstatic}
Definition: Repository.php:157
orderBy($expression, $direction)
{Add ORDER BY.Column/calculation Directionstatic}
Definition: Repository.php:166
This interface defines methods for building fluent interactions with a database repository.
__construct(array $options=[])
Constructor.
Definition: Repository.php:27
static with(array $options=[])
Constructs a new.
Definition: Repository.php:72