public function testOnUpsertManyToMany() { $this->loadFixtures(['Books', 'Genres', 'BookGenres']); $genre = Genre::find(3); $this->assertEquals(8, $genre->book_count); // Create new record and increase to 9 $book = new Book(); $book->addBehavior($this->object->track('Genres', 'book_count')); $book->series_id = 1; $book->name = 'The Winds of Winter'; $book->link($genre); $book->save(['validate' => false]); $genre = Genre::find(3); $this->assertEquals(9, $genre->book_count); // Update a record and add to increase to 10 $book = Book::find(12); $book->addBehavior($this->object->track('Genres', 'book_count')); $book->released = time(); $book->link($genre); $book->save(['validate' => false]); $genre = Genre::find(3); $this->assertEquals(10, $genre->book_count); }
public function testCreateWithManyToMany() { $this->loadFixtures(['Genres', 'Books', 'BookGenres']); $book = new Book(); $book->series_id = 1; $book->name = 'The Winds of Winter'; $book->linkMany([new Genre(['id' => 3, 'name' => 'Action-Adventure']), new Genre(['name' => 'Epic-Horror'])]); $this->assertEquals(16, $book->save(['validate' => false])); $this->assertEquals(new Book(['id' => 16, 'series_id' => 1, 'name' => 'The Winds of Winter', 'isbn' => '', 'released' => '', 'Genres' => new ModelCollection([new Genre(['id' => 3, 'name' => 'Action-Adventure', 'book_count' => 8, 'junction' => new Entity(['id' => 46, 'book_id' => 16, 'genre_id' => 3])]), new Genre(['id' => 12, 'name' => 'Epic-Horror', 'book_count' => 0, 'junction' => new Entity(['id' => 47, 'book_id' => 16, 'genre_id' => 12])])])]), Book::select()->with('Genres')->where('id', 16)->first()); }