Elgg  Version 4.3
Repository.php
Go to the documentation of this file.
1 <?php
2 
3 namespace Elgg\Database;
4 
12 
16 abstract class Repository implements QueryExecuting {
17 
21  protected $options;
22 
28  public function __construct(array $options = []) {
29  $this->options = new QueryOptions($options, \ArrayObject::ARRAY_AS_PROPS);
30  }
31 
35  public function __get($name) {
36  if (!isset($this->options->$name)) {
37  return;
38  }
39 
40  $val = &$this->options->$name;
41 
42  return $val;
43  }
44 
48  public function __set($name, $value) {
49  $this->options->$name = $value;
50  }
51 
55  public function __unset($name) {
56  unset($this->options->$name);
57  }
58 
62  public function __isset($name) {
63  return isset($this->options->$name);
64  }
65 
73  public static function with(array $options = []) {
74  $query = new static($options);
75 
76  return $query;
77  }
78 
86  public static function find(array $options = []) {
87  try {
88  return static::with($options)->execute();
89  } catch (DataFormatException $e) {
90  return elgg_extract('count', $options) ? 0 : false;
91  }
92  }
93 
97  public function batch($limit = null, $offset = null, $callback = null) {
98 
99  $options = $this->options->getArrayCopy();
100 
101  $options['limit'] = (int) $limit;
102  $options['offset'] = (int) $offset;
103  $options['callback'] = $callback;
104  unset($options['count'],
105  $options['batch'],
106  $options['batch_size'],
107  $options['batch_inc_offset']
108  );
109 
110  $batch_size = $this->options->batch_size;
111  $batch_inc_offset = $this->options->batch_inc_offset;
112 
113  return new \ElggBatch([static::class, 'find'], $options, null, $batch_size, $batch_inc_offset);
114  }
115 
119  public function filter(\Closure $closure) {
120  $this->options->where(new WhereClause($closure));
121 
122  return $this;
123  }
124 
128  public function select($expression) {
129  $this->options->select(new SelectClause($expression));
130 
131  return $this;
132  }
133 
137  public function join($joined_table, $joined_alias = null, $x = null, $comparison = null, $y = null, $type = null, $case_sensitive = null) {
138  $join = new JoinClause($joined_table, $joined_alias, function (QueryBuilder $qb, $joined_alias) use ($x, $comparison, $y, $type, $case_sensitive) {
139  return $qb->compare("$joined_alias.$x", $comparison, $y, $type, $case_sensitive);
140  });
141  $this->options->join($join);
142 
143  return $this;
144  }
145 
149  public function groupBy($expression) {
150  $this->options->groupBy(new GroupByClause($expression));
151 
152  return $this;
153  }
154 
158  public function having($expression) {
159  $this->options->having(new HavingClause($expression));
160 
161  return $this;
162  }
163 
167  public function orderBy($expression, $direction) {
168  $this->options->orderBy(new OrderByClause($expression, $direction));
169 
170  return $this;
171  }
172 
181  public function expandInto(QueryBuilder $qb, $table_alias = null) {
182  foreach ($this->options->selects as $select_clause) {
183  $select_clause->prepare($qb, $table_alias);
184  }
185 
186  foreach ($this->options->group_by as $group_by_clause) {
187  $group_by_clause->prepare($qb, $table_alias);
188  }
189 
190  foreach ($this->options->having as $having_clause) {
191  $having_clause->prepare($qb, $table_alias);
192  }
193 
194  if (!empty($this->options->order_by)) {
195  foreach ($this->options->order_by as $order_by_clause) {
196  $order_by_clause->prepare($qb, $table_alias);
197  }
198  }
199  }
200 }
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:97
static find(array $options=[])
Build and execute a new query from an array of legacy options.
Definition: Repository.php:86
if(!$user||!$user->canDelete()) $name
Definition: delete.php:22
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
if($pagination &&($position== 'after'||$position== 'both')) $limit
Definition: list.php:108
Extends QueryBuilder with GROUP BY statements.
$value
Definition: generic.php:51
filter(\Closure $closure)
{}
Definition: Repository.php:119
Abstract methods for interfacing with the database.
Definition: Repository.php:16
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:137
expandInto(QueryBuilder $qb, $table_alias=null)
Extend query builder with select, group_by, having and order_by clauses from $options.
Definition: Repository.php:181
compare($x, $comparison, $y=null, $type=null, $case_sensitive=null)
Build value comparison clause.
select($expression)
{Add SELECT.Selectstatic}
Definition: Repository.php:128
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:547
Extends QueryBuilder with ORDER BY clauses.
Builds a clause from closure or composite expression.
Definition: WhereClause.php:12
groupBy($expression)
{Add GROUP BY.Group bystatic}
Definition: Repository.php:149
An exception thrown when there is a problem in the format of some data.
$query
__set($name, $value)
{}
Definition: Repository.php:48
having($expression)
{Add HAVING.Havingstatic}
Definition: Repository.php:158
orderBy($expression, $direction)
{Add ORDER BY.Column/calculation Directionstatic}
Definition: Repository.php:167
This interface defines methods for building fluent interactions with a database repository.
__construct(array $options=[])
Constructor.
Definition: Repository.php:28
$qb
Definition: queue.php:11
static with(array $options=[])
Constructs a new.
Definition: Repository.php:73