Skip to content

Commit

Permalink
Add delete function to ManyMany
Browse files Browse the repository at this point in the history
Allows deleting the intermediate maphper entries that match the id of the current object
  • Loading branch information
solleer committed Jun 23, 2018
1 parent b771a68 commit 8c8288d
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 0 deletions.
4 changes: 4 additions & 0 deletions Maphper/Relation/ManyMany.php
Original file line number Diff line number Diff line change
Expand Up @@ -111,4 +111,8 @@ private function offsetSetAutotraverse($value, $relatedField, $valueField) {
public function offsetUnset($id) {
//$this->relation->mapper->filter([$relatedField => $this->object->$valueField, $this->relation->parentField => $id])->delete();
}

public function delete() {
$this->getResults()->delete();
}
}
19 changes: 19 additions & 0 deletions tests/MySqlDatabaseTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -1123,6 +1123,25 @@ public function testManyManyGetByIndexNotExist() {
$this->assertFalse(isset($actors[1]->movies[2]));
}

public function testDeleteManyMany() {
list($actors, $movies, $cast) = $this->setUpMoviesActors();

// Set up some relations
$actors[1]->movies[] = $movies[2];
$actors[1]->movies[] = $movies[3];
$actors[2]->movies[] = $movies[2];
$actors[2]->movies[] = $movies[1];

$this->assertNotEquals(0, count($cast));
$this->assertEquals(4, count($cast));

$actors[1]->movies->delete();

$this->assertEquals(2, count($cast));
$this->assertEquals(0, count($actors[1]->movies));
$this->assertEquals(2, count($actors[2]->movies));
}


private function setupMoviesActorsReal() {
$cast = new \Maphper\Maphper($this->getDataSource('cast', ['movieId', 'actorId'], ['editmode' => true]));
Expand Down

0 comments on commit 8c8288d

Please sign in to comment.