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 testCreateWithManyToOne() { $this->loadFixtures(['Series', 'Books']); $series = Series::find(1); $this->assertEquals(new Series(['id' => 1, 'author_id' => 1, 'name' => 'A Song of Ice and Fire']), $series); // Change a value and see if it gets saved when the book does $series->name = 'ASOIAF'; $book = new Book(); $book->name = 'The Winds of Winter'; $book->link($series); $this->assertEquals(16, $book->save(['validate' => false])); $this->assertEquals(new Book(['id' => 16, 'series_id' => 1, 'name' => 'The Winds of Winter', 'isbn' => '', 'released' => '', 'Series' => new Series(['id' => 1, 'author_id' => 1, 'name' => 'ASOIAF'])]), Book::select()->with('Series')->where('id', 16)->first()); }