diff --git a/src/Transit.php b/src/Transit.php index f3d163a..d654653 100644 --- a/src/Transit.php +++ b/src/Transit.php @@ -68,6 +68,24 @@ public function select(string $domainClass, array $whereEquals = []) : TransitSe ); } + /** + * Attach a domain object to plan. + * + * @param object $domain + * + * @return \Atlas\Transit\Transit + * @throws \Atlas\Transit\Exception + */ + public function attach(object $domain): Transit + { + $handler = $this->handlerLocator->get($domain); + /** @var \Atlas\Mapper\Record $record */ + $record = $handler->updateSource($domain, $this->plan); + $record->getRow()->init(''); + + return $this; + } + // PLAN TO insert/update public function store(object $domain) : void { diff --git a/tests/TransitTest.php b/tests/TransitTest.php index b552215..be90bd0 100644 --- a/tests/TransitTest.php +++ b/tests/TransitTest.php @@ -533,4 +533,29 @@ public function testStore() $this->assertSame(101, $actual['responses'][0]['responseId']); $this->assertSame(14, $actual['responses'][0]['author']['authorId']); } + + public function testAttach() + { + // Creation of author in first context + $entity = new Author('Author'); + $this->transit->store($entity); + $this->transit->persist(); + + // New transit class for new context + $secondTransit = FakeTransit::new( + $this->atlas, + 'Atlas\\Testing\\DataSource\\' + ); + $secondTransit->attach($entity); + + // Modify entity after attach to context + $entity->setName('Arthur 2123 fdgfd'); + + $secondTransit->store($entity); + $secondTransit->persist(); + + /** @var \Atlas\Mapper\Record $record */ + $record = $secondTransit->getStorage()->offsetGet($entity); + $this->assertEquals('UPDATED', $record->getRow()->getStatus()); + } }