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()); }
public function testLinkManyArray() { $book = new Book(['name' => 'A Game Of Thrones']); $genre1 = new Genre(['name' => 'Horror']); $genre2 = new Genre(['name' => 'Action']); $book->linkMany([$genre1, $genre2]); $this->assertEquals(new ModelCollection([$genre1, $genre2]), $book->getRelation('Genres')->getLinked()); }