42 return new \ElggRelationship(
$row);
58 return _elgg_services()->db->getDataRow(
"SELECT * FROM {$this->CONFIG->dbprefix}entity_relationships WHERE id = $id");
68 function delete(
$id) {
76 return _elgg_services()->db->deleteData(
"DELETE FROM {$this->CONFIG->dbprefix}entity_relationships WHERE id = $id");
95 function add($guid_one, $relationship, $guid_two) {
100 throw new \InvalidArgumentException($msg);
103 $guid_one = (int)$guid_one;
105 $guid_two = (int)$guid_two;
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)");
121 $result_old =
_elgg_services()->events->trigger(
'create', $relationship, $obj);
145 function check($guid_one, $relationship, $guid_two) {
148 $guid_one = (int)$guid_one;
150 $guid_two = (int)$guid_two;
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";
176 function remove($guid_one, $relationship, $guid_two) {
179 $guid_one = (int)$guid_one;
181 $guid_two = (int)$guid_two;
189 $result_old =
_elgg_services()->events->trigger(
'delete', $relationship, $obj);
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";
220 if (!empty($relationship)) {
222 $where =
"AND er.relationship = '$relationship'";
229 if (!$inverse_relationship) {
230 $join =
"JOIN {$this->CONFIG->dbprefix}entities e ON e.guid = er.guid_two";
232 $join =
"JOIN {$this->CONFIG->dbprefix}entities e ON e.guid = er.guid_one";
235 $where .=
" AND e.type = '$type'";
240 $guid_col = $inverse_relationship ?
"guid_two" :
"guid_one";
243 DELETE er FROM {$this->CONFIG->dbprefix}entity_relationships AS er
245 WHERE $guid_col = $guid
266 $where = ($inverse_relationship ?
"guid_two='$guid'" :
"guid_one='$guid'");
268 $query =
"SELECT * from {$this->CONFIG->dbprefix}entity_relationships where {$where}";
270 return _elgg_services()->db->getData($query,
"row_to_elggrelationship");
313 'relationship' =>
null,
314 'relationship_guid' =>
null,
315 'inverse_relationship' =>
false,
316 'relationship_join_on' =>
'guid',
324 $join_column =
"e.{$options['relationship_join_on']}";
332 } elseif (!isset(
$options[
'wheres'])) {
340 $options[
'relationship_created_time_lower']);
347 } elseif (!isset(
$options[
'joins'])) {
355 } elseif (!isset(
$options[
'selects'])) {
359 $select = array(
'r.id');
364 $options[
'group_by'] = $clauses[
'group_by'];
386 $relationship_guid =
null, $inverse_relationship =
false) {
388 if ($relationship ==
null && $relationship_guid ==
null) {
396 if ($inverse_relationship) {
397 $joins[] =
"JOIN {$this->CONFIG->dbprefix}entity_relationships r on r.guid_one = $column";
399 $joins[] =
"JOIN {$this->CONFIG->dbprefix}entity_relationships r on r.guid_two = $column";
403 $wheres[] =
"r.relationship = '" .
sanitise_string($relationship) .
"'";
406 if ($relationship_guid) {
407 if ($inverse_relationship) {
408 $wheres[] =
"r.guid_two = '$relationship_guid'";
410 $wheres[] =
"r.guid_one = '$relationship_guid'";
418 if ($where_str = implode(
' AND ', $wheres)) {
420 return array(
'wheres' => array(
"($where_str)"),
'joins' => $joins,
'group_by' => $group_by);
436 $options[
'selects'][] =
"COUNT(e.guid) as total";
437 $options[
'group_by'] =
'r.guid_two';
438 $options[
'order_by'] =
'total desc';
if(! $collection_name) $id
__construct()
Constructor.
getEntitiesFromCount(array $options=array())
Gets the number of entities by a the number of entities related to them in a particular way.
add($guid_one, $relationship, $guid_two)
Create a relationship between two entities.
getEntities($options)
Return entities matching a given query joining against a relationship.
getRow($id)
Get a database row from the relationship table.
getAll($guid, $inverse_relationship=false)
Get all the relationships for a given GUID.
getEntityRelationshipWhereSql($column, $relationship=null, $relationship_guid=null, $inverse_relationship=false)
Returns SQL appropriate for relationship joins and wheres.
removeAll($guid, $relationship="", $inverse_relationship=false, $type='')
Removes all relationships originating from a particular entity.
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.
const ELGG_ENTITIES_ANY_VALUE
sanitise_string($string)
Wrapper function for alternate English spelling (.
sanitize_string($string)
Sanitize a string for database use.
_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.
get_relationship($id)
Get a relationship by its ID.
elgg_get_entities_from_relationship($options)
Return entities matching a given query joining against a relationship.
elgg_get_entity_relationship_where_sql($column, $relationship=null, $relationship_guid=null, $inverse_relationship=false)
Returns SQL appropriate for relationship joins and wheres.
delete_relationship($id)
Delete a relationship by its ID.
_elgg_get_relationship_row($id)
Get a database row from the relationship table.
check_entity_relationship($guid_one, $relationship, $guid_two)
Check if a relationship exists between two entities.
row_to_elggrelationship($row)
Convert a database row to a new \ElggRelationship.