Elgg  Version 1.11
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 
95  function add($guid_one, $relationship, $guid_two) {
96 
97 
98  if (strlen($relationship) > \ElggRelationship::RELATIONSHIP_LIMIT) {
99  $msg = "relationship name cannot be longer than " . \ElggRelationship::RELATIONSHIP_LIMIT;
100  throw new \InvalidArgumentException($msg);
101  }
102 
103  $guid_one = (int)$guid_one;
104  $relationship = sanitise_string($relationship);
105  $guid_two = (int)$guid_two;
106  $time = time();
107 
108  // Check for duplicates
109  if (check_entity_relationship($guid_one, $relationship, $guid_two)) {
110  return false;
111  }
112 
113  $id = _elgg_services()->db->insertData("INSERT INTO {$this->CONFIG->dbprefix}entity_relationships
114  (guid_one, relationship, guid_two, time_created)
115  VALUES ($guid_one, '$relationship', $guid_two, $time)");
116 
117  if ($id !== false) {
118  $obj = get_relationship($id);
119 
120  // this event has been deprecated in 1.9. Use 'create', 'relationship'
121  $result_old = _elgg_services()->events->trigger('create', $relationship, $obj);
122 
123  $result = _elgg_services()->events->trigger('create', 'relationship', $obj);
124  if ($result && $result_old) {
125  return true;
126  } else {
128  }
129  }
130 
131  return false;
132  }
133 
145  function check($guid_one, $relationship, $guid_two) {
146 
147 
148  $guid_one = (int)$guid_one;
149  $relationship = sanitise_string($relationship);
150  $guid_two = (int)$guid_two;
151 
152  $query = "SELECT * FROM {$this->CONFIG->dbprefix}entity_relationships
153  WHERE guid_one=$guid_one
154  AND relationship='$relationship'
155  AND guid_two=$guid_two limit 1";
156 
157  $row = row_to_elggrelationship(_elgg_services()->db->getDataRow($query));
158  if ($row) {
159  return $row;
160  }
161 
162  return false;
163  }
164 
176  function remove($guid_one, $relationship, $guid_two) {
177 
178 
179  $guid_one = (int)$guid_one;
180  $relationship = sanitise_string($relationship);
181  $guid_two = (int)$guid_two;
182 
183  $obj = check_entity_relationship($guid_one, $relationship, $guid_two);
184  if ($obj == false) {
185  return false;
186  }
187 
188  // this event has been deprecated in 1.9. Use 'delete', 'relationship'
189  $result_old = _elgg_services()->events->trigger('delete', $relationship, $obj);
190 
191  $result = _elgg_services()->events->trigger('delete', 'relationship', $obj);
192  if ($result && $result_old) {
193  $query = "DELETE FROM {$this->CONFIG->dbprefix}entity_relationships
194  WHERE guid_one = $guid_one
195  AND relationship = '$relationship'
196  AND guid_two = $guid_two";
197 
198  return (bool)_elgg_services()->db->deleteData($query);
199  } else {
200  return false;
201  }
202  }
203 
215  function removeAll($guid, $relationship = "", $inverse_relationship = false, $type = '') {
216 
217 
218  $guid = (int) $guid;
219 
220  if (!empty($relationship)) {
221  $relationship = sanitize_string($relationship);
222  $where = "AND er.relationship = '$relationship'";
223  } else {
224  $where = "";
225  }
226 
227  if (!empty($type)) {
229  if (!$inverse_relationship) {
230  $join = "JOIN {$this->CONFIG->dbprefix}entities e ON e.guid = er.guid_two";
231  } else {
232  $join = "JOIN {$this->CONFIG->dbprefix}entities e ON e.guid = er.guid_one";
233  $where .= " AND ";
234  }
235  $where .= " AND e.type = '$type'";
236  } else {
237  $join = "";
238  }
239 
240  $guid_col = $inverse_relationship ? "guid_two" : "guid_one";
241 
242  _elgg_services()->db->deleteData("
243  DELETE er FROM {$this->CONFIG->dbprefix}entity_relationships AS er
244  $join
245  WHERE $guid_col = $guid
246  $where
247  ");
248 
249  return true;
250  }
251 
261  function getAll($guid, $inverse_relationship = false) {
262 
263 
264  $guid = (int)$guid;
265 
266  $where = ($inverse_relationship ? "guid_two='$guid'" : "guid_one='$guid'");
267 
268  $query = "SELECT * from {$this->CONFIG->dbprefix}entity_relationships where {$where}";
269 
270  return _elgg_services()->db->getData($query, "row_to_elggrelationship");
271  }
272 
311  function getEntities($options) {
312  $defaults = array(
313  'relationship' => null,
314  'relationship_guid' => null,
315  'inverse_relationship' => false,
316  'relationship_join_on' => 'guid',
317 
318  'relationship_created_time_lower' => ELGG_ENTITIES_ANY_VALUE,
319  'relationship_created_time_upper' => ELGG_ENTITIES_ANY_VALUE,
320  );
321 
322  $options = array_merge($defaults, $options);
323 
324  $join_column = "e.{$options['relationship_join_on']}";
325  $clauses = elgg_get_entity_relationship_where_sql($join_column, $options['relationship'],
326  $options['relationship_guid'], $options['inverse_relationship']);
327 
328  if ($clauses) {
329  // merge wheres to pass to get_entities()
330  if (isset($options['wheres']) && !is_array($options['wheres'])) {
331  $options['wheres'] = array($options['wheres']);
332  } elseif (!isset($options['wheres'])) {
333  $options['wheres'] = array();
334  }
335 
336  $options['wheres'] = array_merge($options['wheres'], $clauses['wheres']);
337 
338  // limit based on time created
339  $time_wheres = _elgg_get_entity_time_where_sql('r', $options['relationship_created_time_upper'],
340  $options['relationship_created_time_lower']);
341  if ($time_wheres) {
342  $options['wheres'] = array_merge($options['wheres'], array($time_wheres));
343  }
344  // merge joins to pass to get_entities()
345  if (isset($options['joins']) && !is_array($options['joins'])) {
346  $options['joins'] = array($options['joins']);
347  } elseif (!isset($options['joins'])) {
348  $options['joins'] = array();
349  }
350 
351  $options['joins'] = array_merge($options['joins'], $clauses['joins']);
352 
353  if (isset($options['selects']) && !is_array($options['selects'])) {
354  $options['selects'] = array($options['selects']);
355  } elseif (!isset($options['selects'])) {
356  $options['selects'] = array();
357  }
358 
359  $select = array('r.id');
360 
361  $options['selects'] = array_merge($options['selects'], $select);
362 
363  if (!isset($options['group_by'])) {
364  $options['group_by'] = $clauses['group_by'];
365  }
366  }
367 
369  }
370 
385  function getEntityRelationshipWhereSql($column, $relationship = null,
386  $relationship_guid = null, $inverse_relationship = false) {
387 
388  if ($relationship == null && $relationship_guid == null) {
389  return '';
390  }
391 
392  $wheres = array();
393  $joins = array();
394  $group_by = '';
395 
396  if ($inverse_relationship) {
397  $joins[] = "JOIN {$this->CONFIG->dbprefix}entity_relationships r on r.guid_one = $column";
398  } else {
399  $joins[] = "JOIN {$this->CONFIG->dbprefix}entity_relationships r on r.guid_two = $column";
400  }
401 
402  if ($relationship) {
403  $wheres[] = "r.relationship = '" . sanitise_string($relationship) . "'";
404  }
405 
406  if ($relationship_guid) {
407  if ($inverse_relationship) {
408  $wheres[] = "r.guid_two = '$relationship_guid'";
409  } else {
410  $wheres[] = "r.guid_one = '$relationship_guid'";
411  }
412  } else {
413  // See #5775. Queries that do not include a relationship_guid must be grouped by entity table alias,
414  // otherwise the result set is not unique
415  $group_by = $column;
416  }
417 
418  if ($where_str = implode(' AND ', $wheres)) {
419 
420  return array('wheres' => array("($where_str)"), 'joins' => $joins, 'group_by' => $group_by);
421  }
422 
423  return '';
424  }
425 
435  function getEntitiesFromCount(array $options = array()) {
436  $options['selects'][] = "COUNT(e.guid) as total";
437  $options['group_by'] = 'r.guid_two';
438  $options['order_by'] = 'total desc';
440  }
441 }
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:553
add($guid_one, $relationship, $guid_two)
Create a relationship between two entities.
$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:1967
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