/** * Adds an entity to the repo * * @param object $entity The entity to add * * @return void * * @throws OrmException if the entity could not be added */ public function add($entity) { $idAccessorRegistry = $this->unitOfWork->getEntityRegistry()->getIdAccessorRegistry(); $entityId = $idAccessorRegistry->getEntityId($entity); $mapClass = $this->mapRepository->getEntityClass(); if (empty($entityId)) { $this->entityRepository->add($entity); $map = new $mapClass(); $varJoinName = $this->relation->varJoinName(); $this->mapRepository->add($map); $this->unitOfWork->getEntityRegistry()->registerAggregateRootCallback($entity, $map, function ($root, $child) use($varJoinName, $idAccessorRegistry) { $entityId = $idAccessorRegistry->getEntityId($root); $child->{$varJoinName} = $entityId; $this->map[] = $entityId; }); return; } if (in_array($entityId, $this->map)) { throw new OrmException("Entity with id {$entityId} aleady exists"); } $map = new $mapClass(); $varJoinName = $this->relation->varJoinName(); $map->{$varJoinName} = $entityId; $this->mapRepository->add($map); $this->map[] = $entityId; }
public function testStoreNew() { $article = new Article(); $article->title = "New Article"; $article->teaser = "This is a new article"; $article->body = "It serves test purposes only and should go away afterwards."; $article->author = __METHOD__; $article->license = 'CC'; $article->parentId = 0; $this->repo->add($article); $this->repo->commit(); $this->assertNotEmpty($article->id); $loaded = $this->repo->getById($article->id); $this->assertSame($article, $loaded); return $article->id; }
/** * @testdox The UnitOfWork detects an update made outside of it */ public function testChangesAreDetected() { $this->prepareDatabase(); $this->repo->add($this->entity1); $this->assertContains($this->entity1, $this->unitOfWork->getScheduledEntityInsertions(), "Entity should have been scheduled for insertion"); $this->assertNotContains($this->entity1, $this->unitOfWork->getScheduledEntityUpdates(), "Entity should not have been scheduled for update"); $this->entity1->setUsername("blah"); $this->unitOfWork->checkForUpdates(); /* * This is not a change, since the entity has not yet been inserted! */ $this->assertContains($this->entity1, $this->unitOfWork->getScheduledEntityInsertions(), "Entity should still be scheduled for insertion"); $this->assertNotContains($this->entity1, $this->unitOfWork->getScheduledEntityUpdates(), "Entity should still not be scheduled for update"); $this->unitOfWork->commit(); $this->entity1->setUsername("blub"); $this->unitOfWork->checkForUpdates(); $this->assertNotContains($this->entity1, $this->unitOfWork->getScheduledEntityInsertions(), "Entity should not longer be scheduled for insertion"); $this->assertContains($this->entity1, $this->unitOfWork->getScheduledEntityUpdates(), "Entity should now be scheduled for update"); }
/** * @testdox add() schedules the object for insertion on the unit of work */ public function testAdd() { $this->unitOfWork->expects($this->once())->method('scheduleForInsertion'); $this->repo->add(new Article()); }