public function testOnDeleteManyToMany() { $this->loadFixtures(['Books', 'Genres', 'BookGenres']); $genre = Genre::find(8); $this->assertEquals(15, $genre->book_count); // Delete a record to go down to 14 $book = Book::find(1); $book->addBehavior($this->object->track('Genres', 'book_count')); $book->delete(); $genre = Genre::find(8); $this->assertEquals(14, $genre->book_count); // Delete multiple records $book = new Book(); $book->addBehavior($this->object->track('Genres', 'book_count')); $book->query(Query::DELETE)->where('series_id', 1)->save(); $genre = Genre::find(8); $this->assertEquals(10, $genre->book_count); }
public function testDeleteWithManyToMany() { $this->loadFixtures(['Books', 'Genres', 'BookGenres']); $book = Book::select()->where('id', 5)->with('Genres')->first(); $this->assertEquals(new Book(['id' => 5, 'series_id' => 1, 'name' => 'A Dance with Dragons', 'isbn' => '0-553-80147-3', 'released' => '2011-07-19', 'Genres' => new ModelCollection([new Genre(['id' => 3, 'name' => 'Action-Adventure', 'book_count' => 8, 'junction' => new Entity(['id' => 14, 'book_id' => 5, 'genre_id' => 3])]), new Genre(['id' => 5, 'name' => 'Horror', 'book_count' => 5, 'junction' => new Entity(['id' => 15, 'book_id' => 5, 'genre_id' => 5])]), new Genre(['id' => 8, 'name' => 'Fantasy', 'book_count' => 15, 'junction' => new Entity(['id' => 13, 'book_id' => 5, 'genre_id' => 8])])])]), $book); $this->assertEquals(1, $book->delete()); $book = Book::find(5); $this->assertFalse($book->exists()); // Related record is not deleted $genre = Genre::find(3); $this->assertEquals(new Genre(['id' => 3, 'name' => 'Action-Adventure', 'book_count' => 8]), $genre); // Junction records are deleted $repo = new Repository(['table' => 'books_genres']); $this->assertEquals(0, $repo->select()->where('book_id', 5)->count()); }