Elgg  Version 1.12
RelationshipsTable.php
Go to the documentation of this file.
1 <?php
2 namespace Elgg\Database;
3 
19  private $CONFIG;
20 
24  public function __construct() {
25  global $CONFIG;
26  $this->CONFIG = $CONFIG;
27  }
28 
36  function get($id) {
38  if (!$row) {
39  return false;
40  }
41 
42  return new \ElggRelationship($row);
43  }
44 
53  function getRow($id) {
54 
55 
56  $id = (int)$id;
57 
58  return _elgg_services()->db->getDataRow("SELECT * FROM {$this->CONFIG->dbprefix}entity_relationships WHERE id = $id");
59  }
60 
68  function delete($id) {
69 
70 
71  $id = (int)$id;
72 
73  $relationship = get_relationship($id);
74 
75  if (_elgg_services()->events->trigger('delete', 'relationship', $relationship)) {
76  return _elgg_services()->db->deleteData("DELETE FROM {$this->CONFIG->dbprefix}entity_relationships WHERE id = $id");
77  }
78 
79  return false;
80  }
81 
96  function add($guid_one, $relationship, $guid_two, $return_id = false) {
97  if (strlen($relationship) > \ElggRelationship::RELATIONSHIP_LIMIT) {
98  $msg = "relationship name cannot be longer than " . \ElggRelationship::RELATIONSHIP_LIMIT;
99  throw new \InvalidArgumentException($msg);
100  }
101 
102  $guid_one = (int)$guid_one;
103  $relationship = sanitise_string($relationship);
104  $guid_two = (int)$guid_two;
105  $time = time();
106 
107  // Check for duplicates
108  if (check_entity_relationship($guid_one, $relationship, $guid_two)) {
109  return false;
110  }
111 
112  $id = _elgg_services()->db->insertData("INSERT INTO {$this->CONFIG->dbprefix}entity_relationships
113  (guid_one, relationship, guid_two, time_created)
114  VALUES ($guid_one, '$relationship', $guid_two, $time)");
115 
116  if ($id !== false) {
117  $obj = get_relationship($id);
118 
119  // this event has been deprecated in 1.9. Use 'create', 'relationship'
120  $result_old = _elgg_services()->events->trigger('create', $relationship, $obj);
121 
122  $result = _elgg_services()->events->trigger('create', 'relationship', $obj);
123  if ($result && $result_old) {
124  return $return_id ? $id : true;
125  } else {
126  delete_relationship($obj->id);
127  }
128  }
129 
130  return false;
131  }
132 
144  function check($guid_one, $relationship, $guid_two) {
145 
146 
147  $guid_one = (int)$guid_one;
148  $relationship = sanitise_string($relationship);
149  $guid_two = (int)$guid_two;
150 
151  $query = "SELECT * FROM {$this->CONFIG->dbprefix}entity_relationships
152  WHERE guid_one=$guid_one
153  AND relationship='$relationship'
154  AND guid_two=$guid_two limit 1";
155 
156  $row = row_to_elggrelationship(_elgg_services()->db->getDataRow($query));
157  if ($row) {
158  return $row;
159  }
160 
161  return false;
162  }
163 
175  function remove($guid_one, $relationship, $guid_two) {
176 
177 
178  $guid_one = (int)$guid_one;
179  $relationship = sanitise_string($relationship);
180  $guid_two = (int)$guid_two;
181 
182  $obj = check_entity_relationship($guid_one, $relationship, $guid_two);
183  if ($obj == false) {
184  return false;
185  }
186 
187  // this event has been deprecated in 1.9. Use 'delete', 'relationship'
188  $result_old = _elgg_services()->events->trigger('delete', $relationship, $obj);
189 
190  $result = _elgg_services()->events->trigger('delete', 'relationship', $obj);
191  if ($result && $result_old) {
192  $query = "DELETE FROM {$this->CONFIG->dbprefix}entity_relationships
193  WHERE guid_one = $guid_one
194  AND relationship = '$relationship'
195  AND guid_two = $guid_two";
196 
197  return (bool)_elgg_services()->db->deleteData($query);
198  } else {
199  return false;
200  }
201  }
202 
214  function removeAll($guid, $relationship = "", $inverse_relationship = false, $type = '') {
215 
216 
217  $guid = (int) $guid;
218 
219  if (!empty($relationship)) {
220  $relationship = sanitize_string($relationship);
221  $where = "AND er.relationship = '$relationship'";
222  } else {
223  $where = "";
224  }
225 
226  if (!empty($type)) {
228  if (!$inverse_relationship) {
229  $join = "JOIN {$this->CONFIG->dbprefix}entities e ON e.guid = er.guid_two";
230  } else {
231  $join = "JOIN {$this->CONFIG->dbprefix}entities e ON e.guid = er.guid_one";
232  $where .= " AND ";
233  }
234  $where .= " AND e.type = '$type'";
235  } else {
236  $join = "";
237  }
238 
239  $guid_col = $inverse_relationship ? "guid_two" : "guid_one";
240 
241  _elgg_services()->db->deleteData("
242  DELETE er FROM {$this->CONFIG->dbprefix}entity_relationships AS er
243  $join
244  WHERE $guid_col = $guid
245  $where
246  ");
247 
248  return true;
249  }
250 
260  function getAll($guid, $inverse_relationship = false) {
261 
262 
263  $guid = (int)$guid;
264 
265  $where = ($inverse_relationship ? "guid_two='$guid'" : "guid_one='$guid'");
266 
267  $query = "SELECT * from {$this->CONFIG->dbprefix}entity_relationships where {$where}";
268 
269  return _elgg_services()->db->getData($query, "row_to_elggrelationship");
270  }
271 
313  function getEntities($options) {
314  $defaults = array(
315  'relationship' => null,
316  'relationship_guid' => null,
317  'inverse_relationship' => false,
318  'relationship_join_on' => 'guid',
319 
320  'relationship_created_time_lower' => ELGG_ENTITIES_ANY_VALUE,
321  'relationship_created_time_upper' => ELGG_ENTITIES_ANY_VALUE,
322  );
323 
324  $options = array_merge($defaults, $options);
325 
326  $join_column = "e.{$options['relationship_join_on']}";
327  $clauses = elgg_get_entity_relationship_where_sql($join_column, $options['relationship'],
328  $options['relationship_guid'], $options['inverse_relationship']);
329 
330  if ($clauses) {
331  // merge wheres to pass to get_entities()
332  if (isset($options['wheres']) && !is_array($options['wheres'])) {
333  $options['wheres'] = array($options['wheres']);
334  } elseif (!isset($options['wheres'])) {
335  $options['wheres'] = array();
336  }
337 
338  $options['wheres'] = array_merge($options['wheres'], $clauses['wheres']);
339 
340  // limit based on time created
341  $time_wheres = _elgg_get_entity_time_where_sql('r', $options['relationship_created_time_upper'],
342  $options['relationship_created_time_lower']);
343  if ($time_wheres) {
344  $options['wheres'] = array_merge($options['wheres'], array($time_wheres));
345  }
346  // merge joins to pass to get_entities()
347  if (isset($options['joins']) && !is_array($options['joins'])) {
348  $options['joins'] = array($options['joins']);
349  } elseif (!isset($options['joins'])) {
350  $options['joins'] = array();
351  }
352 
353  $options['joins'] = array_merge($options['joins'], $clauses['joins']);
354 
355  if (isset($options['selects']) && !is_array($options['selects'])) {
356  $options['selects'] = array($options['selects']);
357  } elseif (!isset($options['selects'])) {
358  $options['selects'] = array();
359  }
360 
361  $select = array('r.id');
362 
363  $options['selects'] = array_merge($options['selects'], $select);
364 
365  if (!isset($options['group_by'])) {
366  $options['group_by'] = $clauses['group_by'];
367  }
368  }
369 
371  }
372 
387  function getEntityRelationshipWhereSql($column, $relationship = null,
388  $relationship_guid = null, $inverse_relationship = false) {
389 
390  if ($relationship == null && $relationship_guid == null) {
391  return '';
392  }
393 
394  $wheres = array();
395  $joins = array();
396  $group_by = '';
397 
398  if ($inverse_relationship) {
399  $joins[] = "JOIN {$this->CONFIG->dbprefix}entity_relationships r on r.guid_one = $column";
400  } else {
401  $joins[] = "JOIN {$this->CONFIG->dbprefix}entity_relationships r on r.guid_two = $column";
402  }
403 
404  if ($relationship) {
405  $wheres[] = "r.relationship = '" . sanitise_string($relationship) . "'";
406  }
407 
408  if ($relationship_guid) {
409  if ($inverse_relationship) {
410  $wheres[] = "r.guid_two = '$relationship_guid'";
411  } else {
412  $wheres[] = "r.guid_one = '$relationship_guid'";
413  }
414  } else {
415  // See #5775. Queries that do not include a relationship_guid must be grouped by entity table alias,
416  // otherwise the result set is not unique
417  $group_by = $column;
418  }
419 
420  if ($where_str = implode(' AND ', $wheres)) {
421 
422  return array('wheres' => array("($where_str)"), 'joins' => $joins, 'group_by' => $group_by);
423  }
424 
425  return '';
426  }
427 
437  function getEntitiesFromCount(array $options = array()) {
438  $options['selects'][] = "COUNT(e.guid) as total";
439  $options['group_by'] = 'r.guid_two';
440  $options['order_by'] = 'total desc';
442  }
443 }
elgg_get_entities_from_metadata(array $options=array())
interfaces
Definition: metadata.php:255
get_relationship($id)
Get a relationship by its ID.
getRow($id)
Get a database row from the relationship table.
$defaults
_elgg_get_entity_time_where_sql($table, $time_created_upper=null, $time_created_lower=null, $time_updated_upper=null, $time_updated_lower=null)
Returns SQL where clause for entity time limits.
Definition: entities.php:557
$column
Definition: add.php:13
getAll($guid, $inverse_relationship=false)
Get all the relationships for a given GUID.
check($guid_one, $relationship, $guid_two)
Check if a relationship exists between two entities.
$guid
Removes an admin notice.
events($event="", $object_type="", $function="", $priority=500, $call=false, $object=null)
Deprecated events core function.
sanitize_string($string)
Sanitize a string for database use.
Definition: database.php:140
$options
Definition: index.php:14
getEntityRelationshipWhereSql($column, $relationship=null, $relationship_guid=null, $inverse_relationship=false)
Returns SQL appropriate for relationship joins and wheres.
_elgg_services()
Definition: autoloader.php:14
sanitise_string($string)
Wrapper function for alternate English spelling (.
Definition: database.php:150
const ELGG_ENTITIES_ANY_VALUE
Definition: elgglib.php:2006
add($guid_one, $relationship, $guid_two, $return_id=false)
Create a relationship between two entities.
elgg_get_entity_relationship_where_sql($column, $relationship=null, $relationship_guid=null, $inverse_relationship=false)
Returns SQL appropriate for relationship joins and wheres.
check_entity_relationship($guid_one, $relationship, $guid_two)
Check if a relationship exists between two entities.
elgg global
Pointer to the global context.
Definition: elgglib.js:12
removeAll($guid, $relationship="", $inverse_relationship=false, $type= '')
Removes all relationships originating from a particular entity.
$type
Definition: add.php:8
delete_relationship($id)
Delete a relationship by its ID.
getEntities($options)
Return entities matching a given query joining against a relationship.
row_to_elggrelationship($row)
Convert a database row to a new .
getEntitiesFromCount(array $options=array())
Gets the number of entities by a the number of entities related to them in a particular way...
$row
elgg_get_entities_from_relationship($options)
Return entities matching a given query joining against a relationship.
_elgg_get_relationship_row($id)
Get a database row from the relationship table.
if(!$collection_name) $id
Definition: add.php:17