28 public function calculate($function, $property, $property_type = null) {
34 if (!isset($property_type)) {
36 $property_type =
'attribute';
38 $property_type =
'metadata';
46 switch ($property_type) {
52 $alias = $select->joinEntitiesTable(
'er', $join_column,
'inner',
'e');
53 $select->select(
"{$function}({$alias}.{$property}) AS calculation");
58 if (!empty($this->options->annotation_name_value_pairs) && $this->options->annotation_name_value_pairs[0]->names != $property) {
59 $alias = $select->joinAnnotationTable(
'er', $join_column, $property);
61 $select->select(
"{$function}($alias.value) AS calculation");
65 $alias = $select->joinMetadataTable(
'er', $join_column, $property);
66 $select->select(
"{$function}({$alias}.value) AS calculation");
69 case 'private_setting' :
70 $alias = $select->joinPrivateSettingsTable(
'er', $join_column, $property);
71 $select->select(
"{$function}({$alias}.value) AS calculation");
82 return (
int)
$result->calculation;
92 $count_expr = $this->options->distinct ?
"DISTINCT er.id" :
"*";
93 $select->select(
"COUNT({$count_expr}) AS total");
110 if ($this->options->annotation_calculation) {
111 $clauses = $this->options->annotation_name_value_pairs;
112 if (
count($clauses) > 1 && $this->options->annotation_name_value_pairs_operator !==
'OR') {
113 throw new LogicException(
"Annotation calculation can not be performed on multiple annotation name value pairs merged with AND");
116 $clause = array_shift($clauses);
118 return $this->
calculate($this->options->annotation_calculation, $clause->names,
'annotation');
119 }
elseif ($this->options->metadata_calculation) {
120 $clauses = $this->options->metadata_name_value_pairs;
121 if (
count($clauses) > 1 && $this->options->metadata_name_value_pairs_operator !==
'OR') {
122 throw new LogicException(
"Metadata calculation can not be performed on multiple metadata name value pairs merged with AND");
125 $clause = array_shift($clauses);
127 return $this->
calculate($this->options->metadata_calculation, $clause->names,
'metadata');
128 }
elseif ($this->options->count) {
129 return $this->
count();
130 }
elseif ($this->options->batch) {
131 return $this->
batch($this->options->limit, $this->options->offset, $this->options->callback);
133 return $this->
get($this->options->limit, $this->options->offset, $this->options->callback);
143 $distinct = $this->options->distinct ?
'DISTINCT' :
'';
144 $select->select(
"$distinct er.*");
151 $original_order =
elgg_extract(
'order_by', $this->options->__original_options);
152 if (empty($original_order) && $original_order !==
false) {
153 $select->addOrderBy(
'er.time_created',
'desc');
154 $select->addOrderBy(
'er.id',
'desc');
158 $select->setMaxResults((
int)
$limit);
159 $select->setFirstResult((
int) $offset);
162 $callback = $callback ? : $this->options->callback;
163 if (!isset($callback)) {
164 $callback =
function ($row) {
165 return new \ElggRelationship($row);
171 $preload = array_filter(
$results,
function($e) {
172 return $e instanceof \ElggRelationship;
194 foreach ($this->options->joins as $join) {
195 $join->prepare($qb,
'er');
198 foreach ($this->options->wheres as $where) {
199 $ands[] = $where->prepare($qb,
'er');
203 $ands[] = $this->
buildPairedMetadataClause($qb, $this->options->metadata_name_value_pairs, $this->options->metadata_name_value_pairs_operator);
205 $ands[] = $this->
buildPairedAnnotationClause($qb, $this->options->annotation_name_value_pairs, $this->options->annotation_name_value_pairs_operator);
206 $ands[] = $this->
buildPairedPrivateSettingsClause($qb, $this->options->private_setting_name_value_pairs, $this->options->private_setting_name_value_pairs_operator);
209 $ands = $qb->
merge($ands);
211 $qb->andWhere($ands);
227 return EntityWhereClause::factory($this->options)->prepare($qb, $joined_alias);
245 foreach ($clauses as $clause) {
252 $parts[] = $clause->prepare($qb, $joined_alias);
256 return $qb->
merge($parts, $boolean);
274 foreach ($clauses as $clause) {
280 $parts[] = $clause->prepare($qb, $joined_alias);
283 return $qb->
merge($parts, $boolean);
301 foreach ($clauses as $clause) {
307 $parts[] = $clause->prepare($qb, $joined_alias);
310 return $qb->
merge($parts, $boolean);
325 foreach ($clauses as $clause) {
326 $parts[] = $clause->prepare($qb,
'er');
329 return $qb->
merge($parts, $boolean);
338 if (!empty($this->options->inverse_relationship)) {
Exception thrown if an argument is not of the expected type.
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...
buildPairedAnnotationClause(QueryBuilder $qb, $clauses, $boolean= 'AND')
Process annotation name value pairs Joins the annotation table on guid_one|guid_two in relationships ...
calculate($function, $property, $property_type=null)
Apply numeric calculation to a column.Calculation, e.g. max, min, avg Property name Property typeint|...
Relationships repository contains methods for fetching relationships from database or performing calc...
Database abstraction query builder.
execute()
Apply correct execution method based on calculation, count or other criteria.mixed ...
if($pagination &&($position== 'after'||$position== 'both')) $limit
buildPairedRelationshipClause(QueryBuilder $qb, $clauses, $boolean= 'AND')
Process relationship pairs.
buildPairedPrivateSettingsClause(QueryBuilder $qb, $clauses, $boolean= 'AND')
Process private setting name value pairs Joins the private settings table on guid_one|guid_two in rel...
joinEntitiesTable($from_alias= '', $from_column= 'guid', $join_type= 'inner', $joined_alias=null)
Join entity table from alias and return joined table alias.
Abstract methods for interfacing with the database.
expandInto(QueryBuilder $qb, $table_alias=null)
Extend query builder with select, group_by, having and order_by clauses from $options.
buildQuery(QueryBuilder $qb)
Build a database query.
Exception that represents error in the program logic.
joinAnnotationTable($from_alias= '', $from_column= 'guid', $name=null, $join_type= 'inner', $joined_alias=null)
Join annotations table from alias and return joined table alias.
elgg_extract($key, $array, $default=null, $strict=true)
Checks for $array[$key] and returns its value if it exists, else returns $default.
static fromTable($table, $alias=null)
{}
joinPrivateSettingsTable($from_alias= '', $from_column= 'guid', $name=null, $join_type= 'inner', $joined_alias=null)
Join private settings table from alias and return joined table alias.
merge($parts=null, $boolean= 'AND')
Merges multiple composite expressions with a boolean.
if($item instanceof\ElggEntity) elseif($item instanceof\ElggRiverItem) elseif($item instanceof ElggRelationship) elseif(is_callable([$item, 'getType']))
_elgg_services()
Get the global service provider.
joinMetadataTable($from_alias= '', $from_column= 'guid', $name=null, $join_type= 'inner', $joined_alias=null)
Join metadata table from alias and return joined table alias.
buildPairedMetadataClause(QueryBuilder $qb, $clauses, $boolean= 'AND')
Process metadata name value pairs Joins the metadata table on guid_one|guid_two in relationships tabl...
getJoinColumn()
Return the base column to use in joins.
buildEntityClause(QueryBuilder $qb)
Process entity attribute wheres Joins entities table on guid_one|guid_two in relationships table and ...