62 return $this->db->getDataRow($select, [$this,
'rowToElggRelationship']) ?: null;
72 public function delete(
int $id):
bool {
78 return $this->events->triggerSequence(
'delete',
'relationship',
$relationship,
function() use (
$id) {
82 return (
bool) $this->db->deleteData(
$delete);
99 if (
strlen($relationship->relationship) > self::RELATIONSHIP_COLUMN_LENGTH) {
100 throw new LengthException(
'Relationship name cannot be longer than ' . self::RELATIONSHIP_COLUMN_LENGTH);
105 if ($this->
check($relationship->guid_one, $relationship->relationship, $relationship->guid_two)) {
110 if (!$this->entities->exists($relationship->guid_one) || !$this->entities->exists($relationship->guid_two)) {
117 $result = $this->events->triggerSequence(
'create',
'relationship', $relationship,
function (\
ElggRelationship $relationship) use (&
$id) {
120 'guid_one' => $insert->param($relationship->guid_one,
ELGG_VALUE_GUID),
121 'relationship' => $insert->param($relationship->relationship,
ELGG_VALUE_STRING),
122 'guid_two' => $insert->param($relationship->guid_two,
ELGG_VALUE_GUID),
127 $id = $this->db->insertData($insert);
132 $prev = $e->getPrevious();
133 if ($prev instanceof UniqueConstraintViolationException) {
148 return $return_id ?
$id :
true;
166 ->andWhere($select->compare(
'relationship',
'=', $relationship,
ELGG_VALUE_STRING))
167 ->andWhere($select->compare(
'guid_two',
'=', $guid_two,
ELGG_VALUE_GUID))
170 $row = $this->db->getDataRow($select, [$this,
'rowToElggRelationship']);
171 return $row instanceof \ElggRelationship ? $row :
false;
185 public function remove(
int $guid_one,
string $relationship,
int $guid_two):
bool {
186 $obj = $this->
check($guid_one, $relationship, $guid_two);
191 return $this->
delete($obj->id);
206 public function removeAll(
int $guid,
string $relationship =
'',
bool $inverse_relationship =
false,
string $type =
'',
bool $trigger_events =
true): bool {
207 if ($trigger_events) {
230 if ($inverse_relationship) {
236 if (!empty($relationship)) {
242 $entity_sub->select(
'guid')
245 if (!$inverse_relationship) {
246 $delete->andWhere(
$delete->compare(
'guid_two',
'in', $entity_sub->getSQL()));
248 $delete->andWhere(
$delete->compare(
'guid_one',
'in', $entity_sub->getSQL()));
252 $this->db->deleteData(
$delete);
272 $select->select(
'*');
274 if ($inverse_relationship) {
275 $select->where($select->compare(
'guid_two',
'=', $guid,
ELGG_VALUE_GUID));
277 $select->where($select->compare(
'guid_one',
'=', $guid,
ELGG_VALUE_GUID));
280 if (!empty($relationship)) {
281 $select->andWhere($select->compare(
'relationship',
'=', $relationship,
ELGG_VALUE_STRING));
286 $entity_sub->select(
'guid')
289 if (!$inverse_relationship) {
290 $select->andWhere($select->compare(
'guid_two',
'in', $entity_sub->getSQL()));
292 $select->andWhere($select->compare(
'guid_one',
'in', $entity_sub->getSQL()));
298 $relationships = $this->db->getData($select, [$this,
'rowToElggRelationship']);
301 foreach ($relationships as $rel) {
302 if (!$this->events->triggerBefore(
'delete',
'relationship', $rel)) {
306 if (!$this->events->trigger(
'delete',
'relationship', $rel)) {
310 $remove_ids[] = $rel->id;
314 $chunks = array_chunk($remove_ids, 250);
315 foreach ($chunks as $chunk) {
323 $this->db->deleteData(
$delete);
327 foreach ($relationships as $rel) {
328 if (!in_array($rel->id, $remove_ids)) {
332 $this->events->triggerAfter(
'delete',
'relationship', $rel);
363 return new \ElggRelationship($row);
static find(array $options=[])
Build and execute a new query from an array of legacy options.
add(\ElggRelationship $relationship, bool $return_id=false)
Create a relationship between two entities.
removeAllWithoutEvents(int $guid, string $relationship= '', bool $inverse_relationship=false, string $type= '')
Removes all relationships originating from a particular entity.
$relationship
Elgg default relationship view.
Extends QueryBuilder with SELECT clauses.
Extends QueryBuilder with GROUP BY statements.
trait TimeUsing
Adds methods for setting the current time (for testing)
static intoTable(string $table)
Returns a QueryBuilder for inserting data in a given table.
check(int $guid_one, string $relationship, int $guid_two)
Check if a relationship exists between two entities.
removeAllWithEvents(int $guid, string $relationship= '', bool $inverse_relationship=false, string $type= '')
Removes all relationships originating from a particular entity.
Exception thrown if a length is invalid.
__construct(protected Database $db, protected EntityTable $entities, protected MetadataTable $metadata, protected EventsService $events)
Constructor.
getCurrentTime($modifier= '')
Get the (cloned) time.
if($who_can_change_language=== 'nobody') elseif($who_can_change_language=== 'admin_only'&&!elgg_is_admin_logged_in()) $options
removeAll(int $guid, string $relationship= '', bool $inverse_relationship=false, string $type= '', bool $trigger_events=true)
Removes all relationships originating from a particular entity.
const RELATIONSHIP_COLUMN_LENGTH
foreach($recommendedExtensions as $extension) if(empty(ini_get('session.gc_probability'))||empty(ini_get('session.gc_divisor'))) $db
Relationships table database service.
A generic parent class for database exceptions.
static fromTable(string $table)
Returns a QueryBuilder for deleting data from a given table.
rowToElggRelationship(\stdClass $row)
Convert a database row to a new .
getEntitiesFromCount(array $options=[])
Gets the number of entities by a the number of entities related to them in a particular way...
Extends QueryBuilder with ORDER BY clauses.
const ELGG_VALUE_TIMESTAMP
static fromTable(string $table, string $alias=null)
Returns a QueryBuilder for selecting data from a given table.
$id
Generic annotation delete action.
Entity table database service.
$guid
Reset an ElggUpgrade.