62 return $this->db->getDataRow($select, [$this,
'rowToElggRelationship']) ?: null;
73 public function delete(
int $id,
bool $call_event =
true):
bool {
79 if ($call_event && !$this->events->trigger(
'delete',
'relationship',
$relationship)) {
86 return (
bool) $this->db->deleteData(
$delete);
103 public function add(
int $guid_one,
string $relationship,
int $guid_two,
bool $return_id =
false): bool|int {
104 if (
strlen($relationship) > self::RELATIONSHIP_COLUMN_LENGTH) {
105 throw new LengthException(
'Relationship name cannot be longer than ' . self::RELATIONSHIP_COLUMN_LENGTH);
110 if ($this->
check($guid_one, $relationship, $guid_two)) {
115 if (!$this->entities->exists($guid_one) || !$this->entities->exists($guid_two)) {
129 $id = $this->db->insertData($insert);
134 $prev = $e->getPrevious();
135 if ($prev instanceof UniqueConstraintViolationException) {
143 $obj = $this->
get(
$id);
145 $result = $this->events->trigger(
'create',
'relationship', $obj);
147 $this->
delete(
$id,
false);
151 return $return_id ? $obj->id :
true;
169 ->andWhere($select->compare(
'relationship',
'=', $relationship,
ELGG_VALUE_STRING))
170 ->andWhere($select->compare(
'guid_two',
'=', $guid_two,
ELGG_VALUE_GUID))
173 $row = $this->db->getDataRow($select, [$this,
'rowToElggRelationship']);
174 return $row instanceof \ElggRelationship ? $row :
false;
188 public function remove(
int $guid_one,
string $relationship,
int $guid_two):
bool {
189 $obj = $this->
check($guid_one, $relationship, $guid_two);
194 return $this->
delete($obj->id);
209 public function removeAll(
int $guid,
string $relationship =
'',
bool $inverse_relationship =
false,
string $type =
'',
bool $trigger_events =
true): bool {
210 if ($trigger_events) {
233 if ($inverse_relationship) {
239 if (!empty($relationship)) {
245 $entity_sub->select(
'guid')
248 if (!$inverse_relationship) {
249 $delete->andWhere(
$delete->compare(
'guid_two',
'in', $entity_sub->getSQL()));
251 $delete->andWhere(
$delete->compare(
'guid_one',
'in', $entity_sub->getSQL()));
255 $this->db->deleteData(
$delete);
275 $select->select(
'*');
277 if ($inverse_relationship) {
278 $select->where($select->compare(
'guid_two',
'=', $guid,
ELGG_VALUE_GUID));
280 $select->where($select->compare(
'guid_one',
'=', $guid,
ELGG_VALUE_GUID));
283 if (!empty($relationship)) {
284 $select->andWhere($select->compare(
'relationship',
'=', $relationship,
ELGG_VALUE_STRING));
289 $entity_sub->select(
'guid')
292 if (!$inverse_relationship) {
293 $select->andWhere($select->compare(
'guid_two',
'in', $entity_sub->getSQL()));
295 $select->andWhere($select->compare(
'guid_one',
'in', $entity_sub->getSQL()));
301 $relationships = $this->db->getData($select, [$this,
'rowToElggRelationship']);
304 foreach ($relationships as $rel) {
305 if (!$this->events->trigger(
'delete',
'relationship', $rel)) {
309 $remove_ids[] = $rel->id;
313 $chunks = array_chunk($remove_ids, 250);
314 foreach ($chunks as $chunk) {
322 $this->db->deleteData(
$delete);
353 return new \ElggRelationship($row);
static find(array $options=[])
Build and execute a new query from an array of legacy options.
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.
add(int $guid_one, string $relationship, int $guid_two, bool $return_id=false)
Create a relationship between two entities.
$id
Generic annotation delete action.
Entity table database service.
$guid
Reset an ElggUpgrade.