65 return $this->db->getDataRow($select, [$this,
'rowToElggRelationship']) ?: null;
76 public function delete(
int $id,
bool $call_event =
true):
bool {
82 if ($call_event && !$this->events->trigger(
'delete',
'relationship',
$relationship)) {
89 return (
bool) $this->db->deleteData(
$delete);
106 public function add(
int $guid_one,
string $relationship,
int $guid_two,
bool $return_id =
false): bool|int {
107 if (
strlen($relationship) > self::RELATIONSHIP_COLUMN_LENGTH) {
108 throw new LengthException(
'Relationship name cannot be longer than ' . self::RELATIONSHIP_COLUMN_LENGTH);
113 if ($this->
check($guid_one, $relationship, $guid_two)) {
118 if (!$this->entities->exists($guid_one) || !$this->entities->exists($guid_two)) {
132 $id = $this->db->insertData($insert);
137 $prev = $e->getPrevious();
138 if ($prev instanceof UniqueConstraintViolationException) {
146 $obj = $this->
get(
$id);
148 $result = $this->events->trigger(
'create',
'relationship', $obj);
150 $this->
delete(
$id,
false);
154 return $return_id ? $obj->id :
true;
172 ->andWhere($select->compare(
'relationship',
'=', $relationship,
ELGG_VALUE_STRING))
173 ->andWhere($select->compare(
'guid_two',
'=', $guid_two,
ELGG_VALUE_GUID))
176 $row = $this->db->getDataRow($select, [$this,
'rowToElggRelationship']);
177 return $row instanceof \ElggRelationship ? $row :
false;
191 public function remove(
int $guid_one,
string $relationship,
int $guid_two):
bool {
192 $obj = $this->
check($guid_one, $relationship, $guid_two);
197 return $this->
delete($obj->id);
212 public function removeAll(
int $guid,
string $relationship =
'',
bool $inverse_relationship =
false,
string $type =
'',
bool $trigger_events =
true): bool {
213 if ($trigger_events) {
236 if ($inverse_relationship) {
242 if (!empty($relationship)) {
247 $entity_sub =
$delete->subquery(
'entities');
248 $entity_sub->select(
'guid')
251 if (!$inverse_relationship) {
252 $delete->andWhere(
$delete->compare(
'guid_two',
'in', $entity_sub->getSQL()));
254 $delete->andWhere(
$delete->compare(
'guid_one',
'in', $entity_sub->getSQL()));
258 $this->db->deleteData(
$delete);
278 $select->select(
'*');
280 if ($inverse_relationship) {
281 $select->where($select->compare(
'guid_two',
'=', $guid,
ELGG_VALUE_GUID));
283 $select->where($select->compare(
'guid_one',
'=', $guid,
ELGG_VALUE_GUID));
286 if (!empty($relationship)) {
287 $select->andWhere($select->compare(
'relationship',
'=', $relationship,
ELGG_VALUE_STRING));
291 $entity_sub = $select->subquery(
'entities');
292 $entity_sub->select(
'guid')
295 if (!$inverse_relationship) {
296 $select->andWhere($select->compare(
'guid_two',
'in', $entity_sub->getSQL()));
298 $select->andWhere($select->compare(
'guid_one',
'in', $entity_sub->getSQL()));
304 $relationships = $this->db->getData($select, [$this,
'rowToElggRelationship']);
307 foreach ($relationships as $rel) {
308 if (!$this->events->trigger(
'delete',
'relationship', $rel)) {
312 $remove_ids[] = $rel->id;
316 $chunks = array_chunk($remove_ids, 250);
317 foreach ($chunks as $chunk) {
325 $this->db->deleteData(
$delete);
356 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)
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.
getCurrentTime($modifier= '')
Get the (cloned) time.
static intoTable($table)
{}
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
Relationships table database service.
A generic parent class for database exceptions.
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...
__construct(Database $db, EntityTable $entities, MetadataTable $metadata, EventsService $events)
Constructor.
Extends QueryBuilder with ORDER BY clauses.
const ELGG_VALUE_TIMESTAMP
static fromTable($table, $alias=null)
{}
add(int $guid_one, string $relationship, int $guid_two, bool $return_id=false)
Create a relationship between two entities.
$id
Generic annotation delete action.
static fromTable($table, $alias=null)
{}
Entity table database service.
$guid
Reset an ElggUpgrade.