ElggEntity::deleteRelationships PHP Method

deleteRelationships() public method

If you pass a relationship name, only relationships matching that name will be deleted.
See also: ElggEntity::addRelationship()
See also: ElggEntity::removeRelationship()
public deleteRelationships ( null | string $relationship = null ) : boolean
$relationship null | string The name of the relationship to remove.
return boolean
    public function deleteRelationships($relationship = null)
    {
        $relationship = (string) $relationship;
        $result = remove_entity_relationships($this->getGUID(), $relationship);
        return $result && remove_entity_relationships($this->getGUID(), $relationship, true);
    }

Usage Example

Example #1
0
 public function testEntityMethodDeleteRelationships()
 {
     $this->assertTrue($this->entity1->addRelationship($this->entity2->guid, 'test_relationship'));
     $r = check_entity_relationship($this->entity1->guid, 'test_relationship', $this->entity2->guid);
     $this->assertIsA($r, 'ElggRelationship');
     $this->assertTrue($this->entity1->addRelationship($this->entity3->guid, 'test_relationship'));
     $r = check_entity_relationship($this->entity1->guid, 'test_relationship', $this->entity3->guid);
     $this->assertIsA($r, 'ElggRelationship');
     $this->assertTrue($this->entity3->addRelationship($this->entity1->guid, 'test_relationship'));
     $r = check_entity_relationship($this->entity3->guid, 'test_relationship', $this->entity1->guid);
     $this->assertIsA($r, 'ElggRelationship');
     $this->assertTrue($this->entity1->deleteRelationships('test_relationship'));
     $this->assertFalse(check_entity_relationship($this->entity1->guid, 'test_relationship', $this->entity2->guid));
     $this->assertFalse(check_entity_relationship($this->entity1->guid, 'test_relationship', $this->entity3->guid));
     // inverse relationships should be gone too
     $this->assertFalse(check_entity_relationship($this->entity3->guid, 'test_relationship', $this->entity1->guid));
     // Repeat above test, but with no relationship - should remove all relationships
     $this->assertTrue($this->entity1->addRelationship($this->entity2->guid, 'test_relationship'));
     $r = check_entity_relationship($this->entity1->guid, 'test_relationship', $this->entity2->guid);
     $this->assertIsA($r, 'ElggRelationship');
     $this->assertTrue($this->entity1->addRelationship($this->entity3->guid, 'test_relationship'));
     $r = check_entity_relationship($this->entity1->guid, 'test_relationship', $this->entity3->guid);
     $this->assertIsA($r, 'ElggRelationship');
     $this->assertTrue($this->entity3->addRelationship($this->entity1->guid, 'test_relationship'));
     $r = check_entity_relationship($this->entity3->guid, 'test_relationship', $this->entity1->guid);
     $this->assertIsA($r, 'ElggRelationship');
     $this->assertTrue($this->entity1->addRelationship($this->entity2->guid, 'test_relationship2'));
     $r = check_entity_relationship($this->entity1->guid, 'test_relationship2', $this->entity2->guid);
     $this->assertIsA($r, 'ElggRelationship');
     $this->assertTrue($this->entity1->addRelationship($this->entity3->guid, 'test_relationship2'));
     $r = check_entity_relationship($this->entity1->guid, 'test_relationship2', $this->entity3->guid);
     $this->assertIsA($r, 'ElggRelationship');
     $this->assertTrue($this->entity1->deleteRelationships());
     $this->assertFalse(check_entity_relationship($this->entity1->guid, 'test_relationship', $this->entity2->guid));
     $this->assertFalse(check_entity_relationship($this->entity1->guid, 'test_relationship', $this->entity3->guid));
     $this->assertFalse(check_entity_relationship($this->entity1->guid, 'test_relationship2', $this->entity2->guid));
     $this->assertFalse(check_entity_relationship($this->entity1->guid, 'test_relationship2', $this->entity3->guid));
     // inverse relationships should be gone too
     $this->assertFalse(check_entity_relationship($this->entity3->guid, 'test_relationship', $this->entity1->guid));
 }