63 public function get(
$id) {
69 return new \ElggRelationship(
$row);
81 $sql =
"SELECT * FROM {$this->db->prefix}entity_relationships WHERE id = :id";
85 return $this->db->getDataRow($sql, null,
$params);
96 public function delete(
$id, $call_event =
true) {
99 $relationship = $this->
get(
$id);
101 if ($call_event && !$this->events->trigger(
'delete',
'relationship', $relationship)) {
105 $sql =
"DELETE FROM {$this->db->prefix}entity_relationships WHERE id = :id";
109 return $this->db->deleteData($sql,
$params);
126 public function add($guid_one, $relationship, $guid_two, $return_id =
false) {
129 throw new \InvalidArgumentException($msg);
134 if ($this->
check($guid_one, $relationship, $guid_two)) {
139 INSERT INTO {$this->db->prefix}entity_relationships 140 (guid_one, relationship, guid_two, time_created) 141 VALUES (:guid1, :relationship, :guid2, :time) 144 ':guid1' => (int)$guid_one,
145 ':guid2' => (
int)$guid_two,
146 ':relationship' => $relationship,
156 $prev = $e->getPrevious();
157 if ($prev instanceof UniqueConstraintViolationException) {
164 $obj = $this->
get(
$id);
166 $result = $this->events->trigger(
'create',
'relationship', $obj);
168 $this->
delete(
$id,
false);
172 return $return_id ? $obj->id :
true;
186 public function check($guid_one, $relationship, $guid_two) {
188 SELECT * FROM {$this->db->prefix}entity_relationships 189 WHERE guid_one = :guid1 190 AND relationship = :relationship 191 AND guid_two = :guid2 195 ':guid1' => (int)$guid_one,
196 ':guid2' => (
int)$guid_two,
197 ':relationship' => $relationship,
218 public function remove($guid_one, $relationship, $guid_two) {
219 $obj = $this->
check($guid_one, $relationship, $guid_two);
224 return $this->
delete($obj->id);
242 if (!empty($relationship)) {
243 $where =
"AND er.relationship = :relationship";
244 $params[
':relationship'] = $relationship;
250 if (!$inverse_relationship) {
251 $join =
"JOIN {$this->db->prefix}entities e ON e.guid = er.guid_two";
253 $join =
"JOIN {$this->db->prefix}entities e ON e.guid = er.guid_one";
256 $where .=
" AND e.type = :type";
262 $guid_col = $inverse_relationship ?
"guid_two" :
"guid_one";
264 $this->db->deleteData(
" 265 DELETE er FROM {$this->db->prefix}entity_relationships AS er 267 WHERE $guid_col = $guid 286 $where = ($inverse_relationship ?
"guid_two = :guid" :
"guid_one = :guid");
288 $query =
"SELECT * from {$this->db->prefix}entity_relationships WHERE {$where}";
290 return $this->db->getData($query, [$this,
'rowToElggRelationship'],
$params);
336 'relationship' => null,
337 'relationship_guid' => null,
338 'inverse_relationship' =>
false,
339 'relationship_join_on' =>
'guid',
347 $join_column =
"e.{$options['relationship_join_on']}";
356 } elseif (!isset(
$options[
'wheres'])) {
363 $time_wheres = $this->entities->getEntityTimeWhereSql(
'r',
364 $options[
'relationship_created_time_upper'],
365 $options[
'relationship_created_time_lower']);
372 } elseif (!isset(
$options[
'joins'])) {
380 } elseif (!isset(
$options[
'selects'])) {
389 $options[
'group_by'] = $clauses[
'group_by'];
411 $relationship_guid = null, $inverse_relationship =
false) {
413 if ($relationship == null && $relationship_guid == null) {
421 if ($inverse_relationship) {
422 $joins[] =
"JOIN {$this->db->prefix}entity_relationships r on r.guid_one = $column";
424 $joins[] =
"JOIN {$this->db->prefix}entity_relationships r on r.guid_two = $column";
428 $wheres[] =
"r.relationship = '" . $this->db->sanitizeString($relationship) .
"'";
431 if ($relationship_guid) {
432 if ($inverse_relationship) {
433 $wheres[] =
"r.guid_two = '$relationship_guid'";
435 $wheres[] =
"r.guid_one = '$relationship_guid'";
443 if ($where_str = implode(
' AND ', $wheres)) {
445 return array(
'wheres' => array(
"($where_str)"),
'joins' => $joins,
'group_by' => $group_by);
461 $options[
'selects'][] =
"COUNT(e.guid) as total";
462 $options[
'group_by'] =
'r.guid_two';
463 $options[
'order_by'] =
'total desc';
477 return new \ElggRelationship(
$row);
elgg menu metadata
for compatibility with Elgg 3.0
getRow($id)
Get a database row from the relationship table.
rowToElggRelationship($row)
Convert a database row to a new .
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.
getCurrentTime($modifier= '')
Get the (cloned) time.
$guid
Removes an admin notice.
getEntityRelationshipWhereSql($column, $relationship=null, $relationship_guid=null, $inverse_relationship=false)
Returns SQL appropriate for relationship joins and wheres.
const ELGG_ENTITIES_ANY_VALUE
add($guid_one, $relationship, $guid_two, $return_id=false)
Create a relationship between two entities.
removeAll($guid, $relationship="", $inverse_relationship=false, $type= '')
Removes all relationships originating from a particular entity.
getEntities($options)
Return entities matching a given query joining against a relationship.
__construct(Database $db, EntityTable $entities, MetadataTable $metadata, EventsService $events)
Constructor.
getEntitiesFromCount(array $options=array())
Gets the number of entities by a the number of entities related to them in a particular way...
trait TimeUsing
Adds methods for setting the current time (for testing)
if(!$collection_name) $id
http free of to any person obtaining a copy of this software and associated documentation to deal in the Software without including without limitation the rights to use