/** * @param LinkMany $link */ public function update(LinkMany $link) { parent::update($link); $affected = new Models(); foreach ($link->getRemoved() as $removed) { $affected->addAll($removed->setPathAndUpdateDescendants('')); } foreach ($link->getAdded() as $added) { $affected->addAll($added->setPathAndUpdateDescendants($link->getModel()->getChildrenPath())); } return $affected; }
/** * @covers ::update */ public function testUpdate() { $rel = new HasMany('cities', Country::getRepo()->getConfig(), 'Harp\\Harp\\Test\\TestModel\\City'); $model = new Country(['id' => 2]); $foreign1 = new City(['countryId' => 2]); $foreign2 = new City(['countryId' => 2]); $foreign3 = new City(['countryId' => 8]); $link = new LinkMany($model, $rel, [$foreign1, $foreign2]); $link->remove($foreign1); $link->add($foreign3); $rel->update($link); $this->assertEquals(null, $foreign1->countryId); $this->assertEquals(2, $foreign2->countryId); $this->assertEquals(2, $foreign3->countryId); }