Пример #1
0
 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);
 }
Пример #2
0
 public function testUpdateWithManyToMany()
 {
     $this->loadFixtures(['Genres', 'Books', 'BookGenres']);
     $book = Book::find(5);
     $this->assertEquals(new Book(['id' => 5, 'series_id' => 1, 'name' => 'A Dance with Dragons', 'isbn' => '0-553-80147-3', 'released' => '2011-07-19']), $book);
     $genres = $book->Genres;
     $this->assertEquals(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])])]), $genres);
     // Update the book, a genre, and add a new genre
     $book->name = 'ADWD';
     $genre1 = $genres[0];
     $genre1->name = 'Action/Adventure';
     $book->link($genre1);
     $genre2 = new Genre();
     $genre2->name = 'Drama';
     $book->link($genre2);
     $this->assertEquals(5, $book->save(['validate' => false]));
     $this->assertEquals(new Book(['id' => 5, 'series_id' => 1, 'name' => 'ADWD', '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])]), new Genre(['id' => 12, 'name' => 'Drama', 'book_count' => 0, 'junction' => new Entity(['id' => 46, 'book_id' => 5, 'genre_id' => 12])])])]), Book::select()->with('Genres')->where('id', 5)->first());
 }