retrieve() public method

Retrieve an entity, throwing a NotFoundException if the entity is not found
public retrieve ( string $class_name, string $id, boolean $use_cache = true ) : object
$class_name string
$id string
$use_cache boolean
return object
Esempio n. 1
0
 /**
  * Testing race conditions of new entities, without a flush between persist calls
  *
  * @see: docs/RaceConditions.md
  * @dataProvider entityManagerDataProvider
  * @param EntityManager $em
  */
 public function testOneToManyRaceNoFlush(EntityManager $em)
 {
     $time = new \DateTime();
     $article1 = new Article();
     $article1->setId(201)->setTitle('Article 201')->setTimeCreated($time)->setLastModified($time);
     $category1 = new Category();
     $category1->setId(201)->setName('Category 201');
     $category1->addArticle($article1);
     $em->persist($category1)->persist($article1)->flush();
     /** @var Article|OrmProxyInterface $r_article */
     $r_article = $em->retrieve(Article::class, 201);
     $this->assertEquals('Article 201', $r_article->getTitle());
     // Should make DB query here
     $r_category = $r_article->getCanonicalCategory();
     $this->assertTrue($r_category instanceof Category);
     // Check inverse side too -
     /** @var Category|OrmProxyInterface $ir_category */
     $ir_category = $em->retrieve(Category::class, 201);
     $this->assertEquals('Category 201', $ir_category->getName());
     // Should make DB query here
     $ir_articles = $ir_category->getArticles();
     $this->assertCount(1, $ir_articles);
     $ir_article = $ir_articles[0];
     $this->assertTrue($ir_article instanceof Article);
 }
Esempio n. 2
0
 /**
  * Loads the user for the given username.
  *
  * @param string $username The username
  * @return UserInterface
  * @throws UsernameNotFoundException if the user is not found
  *
  */
 public function loadUserByUsername($username)
 {
     try {
         return $this->entity_manager->retrieve($this->user_class, $username);
     } catch (NotFoundException $e) {
         throw new UsernameNotFoundException("User '" . $username . "' not found");
     }
 }
Esempio n. 3
0
 /**
  * Reads the session data.
  *
  * @param string $sessionId Session ID, see http://php.net/function.session-id
  * @return string Same session data as passed in write() or empty string when non-existent or on failure
  */
 public function read($sessionId)
 {
     try {
         /** @var SessionInterface $entity */
         $entity = $this->entity_manager->retrieve($this->session_class, $sessionId);
         return $entity->getData();
     } catch (NotFoundException $e) {
         return '';
     }
 }
Esempio n. 4
0
 /**
  * @dataProvider entityManagerDataProvider
  * @param EntityManager $em
  */
 public function testForwardConditionalRelationship(EntityManager $em)
 {
     $category = new Category();
     $category->setId(1000)->setName('Conditional Category');
     $em->persist($category)->flush();
     for ($i = 45; $i < 56; $i++) {
         $article = new Article();
         $article->setId($i)->setTitle('Conditional Article #' . $i);
         if ($i == 53) {
             $article->setPublished(false);
         } else {
             $article->setPublished(true);
         }
         $category->addArticle($article);
         $em->persist($article);
     }
     for ($i = 45; $i < 56; $i++) {
         $asset = new Asset();
         $asset->setId($i)->setTitle('Conditional Asset #' . $i);
         if ($i == 53) {
             $asset->setPublished(false);
         } else {
             $asset->setPublished(true);
         }
         $category->addAsset($asset);
         $em->persist($asset);
     }
     $em->persist($category)->flush();
     $articles = $em->sortedQuery(new SortedQuery($category, 'articles', 'last_modified'));
     $this->assertCount(4, $articles);
     $articles = $em->sortedQuery(new SortedQuery($category, 'articles', 'last_modified_all'));
     $this->assertCount(10, $articles);
     $articles = $em->sortedQuery(new SortedQuery($category, 'articles', 'id'));
     $this->assertCount(11, $articles);
     // Update an entity to make it fail the condition -
     $article = $em->retrieve(Article::class, 54);
     $article->setPublished(false);
     $em->persist($article)->flush();
     $articles = $em->sortedQuery(new SortedQuery($category, 'articles', 'last_modified'));
     $this->assertCount(3, $articles);
     /**
      * Assert against Assets
      */
     $assets = $em->sortedQuery(new SortedQuery($category, 'assets', 'last_modified'));
     $this->assertCount(4, $assets);
     $assets = $em->sortedQuery(new SortedQuery($category, 'assets', 'id'));
     $this->assertCount(11, $assets);
     // Update an entity to make it fail the condition -
     $asset = $em->retrieve(Asset::class, 54);
     $asset->setPublished(false);
     $em->persist($asset)->flush();
     $assets = $em->sortedQuery(new SortedQuery($category, 'assets', 'last_modified'));
     $this->assertCount(3, $assets);
 }
Esempio n. 5
0
 /**
  * Hydrate an entity
  *
  * @param string $id
  * @return $this
  */
 private function hydrateEntity($id)
 {
     try {
         $this->entities[$id] = $this->entity_manager->retrieve($this->query->getClassName(), $id, $this->use_cache);
     } catch (NotFoundException $e) {
         throw new CorruptedEntityException("Entity in sorted index not found: " . $e->getMessage(), 0, $e);
     }
     return $this;
 }
Esempio n. 6
0
 /**
  * Testing race conditions of new entities, without a flush between persist calls
  * @see: docs/RaceConditions.md
  * @dataProvider entityManagerDataProvider
  * @param EntityManager $em
  */
 public function testOneToOneRaceNoFlush(EntityManager $em)
 {
     $user = new User();
     $user->setId(102)->setName('Ray');
     $address = new Address();
     $address->setId(502)->setStreet('Purchase St');
     $user->setAddress($address);
     $em->persist($user)->persist($address)->flush();
     /** @var User|OrmProxyInterface $r_user */
     $r_user = $em->retrieve(User::class, 102);
     $this->assertEquals('Ray', $r_user->getName());
     // Should make DB query here
     $r_address = $r_user->getAddress();
     $this->assertTrue($r_address instanceof Address);
     $this->assertTrue($r_address instanceof OrmProxyInterface);
 }
Esempio n. 7
0
 /**
  * @dataProvider entityManagerDataProvider
  * @param EntityManager $em
  */
 public function testIndex(EntityManager $em)
 {
     $entity = new IndexedEntity();
     $entity->setId1(100)->setId2('id2');
     $entity->setAlpha('alpha')->setBravo('200')->setCharlie(true);
     $metadata = $em->getMapper()->getEntityMetadata($entity);
     $reader = new Reader($metadata, $entity);
     $this->assertEquals('100.id2', $reader->getId());
     $indices = $metadata->getIndices();
     $this->assertCount(3, $indices);
     $ab = $metadata->getIndexByName('ab');
     $this->assertContains('alpha', $ab->getColumns());
     $this->assertContains('bravo', $ab->getColumns());
     $this->assertCount(2, $ab->getColumns());
     $this->assertEquals('alpha.200', $reader->getIndexValue($ab));
     $bc = $metadata->getIndexByName('bc');
     $this->assertContains('bravo', $bc->getColumns());
     $this->assertContains('getCharlie', $bc->getMethods());
     $this->assertCount(1, $bc->getColumns());
     $this->assertCount(1, $bc->getMethods());
     $this->assertEquals('200.1', $reader->getIndexValue($bc));
     $b = $metadata->getIndexByName('b');
     $this->assertContains('bravo', $b->getColumns());
     $this->assertCount(1, $b->getColumns());
     $this->assertEquals('200', $reader->getIndexValue($b));
     $em->persist($entity)->flush();
     /** @var IndexedEntity $retrieved */
     $retrieved = $em->retrieve(self::INDEXED_ENTITY, '100.id2');
     $retrieved->setAlpha('omega')->setId1(101);
     $em->persist($retrieved)->flush();
     try {
         $em->retrieveByIndex(self::INDEXED_ENTITY, 'ab', 'alpha.200');
         $this->fail("Former index was found");
     } catch (NotFoundException $e) {
     }
     /** @var IndexedEntity $retrieved_by_index */
     $retrieved_by_index = $em->retrieveByIndex(self::INDEXED_ENTITY, 'ab', 'omega.200');
     $this->assertEquals(101, $retrieved_by_index->getId1());
     $this->assertEquals('id2', $retrieved_by_index->getId2());
     $this->assertSame('omega', $retrieved_by_index->getAlpha());
     $this->assertSame(200, $retrieved_by_index->getBravo());
     $this->assertSame(true, $retrieved_by_index->getCharlie());
 }
 /**
  * @dataProvider entityManagerDataProvider
  * @param EntityManager $em
  * @expectedException \Bravo3\Orm\Exceptions\CorruptedEntityException
  */
 public function testSortedQueryThrowsNotFoundException(EntityManager $em)
 {
     $category = (new Category())->setId(5000);
     $article1 = (new Article())->setId(5001)->setTitle('A');
     $article2 = (new Article())->setId(5002)->setTitle('B');
     $article3 = (new Article())->setId(5003)->setTitle('C');
     $category->addArticle($article1);
     $category->addArticle($article2);
     $category->addArticle($article3);
     $em->persist($article1)->persist($article2)->persist($article3)->persist($category)->flush();
     // Forcefully break the relationship via the driver manually
     $em->getDriver()->delete($em->getKeyScheme()->getEntityKey('article', '5001'));
     $em->getDriver()->flush();
     $category = $em->retrieve(Category::class, 5000, false);
     $results = $em->sortedQuery(new SortedQuery($category, 'articles', 'sort_date'));
     // Iterating through these results should trigger an exception
     foreach ($results as $result) {
     }
 }
Esempio n. 9
0
 /**
  * Hydrate a relationship
  *
  * @param Relationship $relative
  * @return $this
  */
 public function hydrateRelative(Relationship $relative)
 {
     $this->entity_manager->getDriver()->debugLog("Hydrating relative for " . $this->metadata->getTableName() . "[" . $this->getReader()->getId() . "]::" . $relative->getName());
     $setter = $relative->getSetter();
     $key = $this->entity_manager->getKeyScheme()->getRelationshipKey($relative, $this->entity_manager->getMapper()->getEntityMetadata($relative->getSource())->getTableName(), $this->entity_manager->getMapper()->getEntityMetadata($relative->getTarget())->getTableName(), $this->getReader()->getId());
     if (RelationshipType::isMultiIndex($relative->getRelationshipType())) {
         $items = [];
         $ids = $this->entity_manager->getDriver()->getMultiValueIndex($key);
         foreach ($ids as $id) {
             $items[] = $this->entity_manager->retrieveEntityOrNew($relative->getTarget(), $id);
         }
         $this->proxy->{$setter}($items);
     } else {
         $id = $this->entity_manager->getDriver()->getSingleValueIndex($key);
         if ($id) {
             $this->proxy->{$setter}($this->entity_manager->retrieve($relative->getTarget(), $id));
         }
     }
     return $this;
 }
Esempio n. 10
0
 /**
  * @dataProvider entityManagerDataProvider
  * @param EntityManager $em
  */
 public function testRebuildIndicesManyToMany(EntityManager $em)
 {
     $charlie = new Charlie();
     $charlie->setId('charlie')->setName('Charlie');
     $delta = new Delta();
     $delta->setId('delta')->setName('Delta');
     $charlie->setDelta([$delta]);
     $em->persist($charlie)->persist($delta)->flush();
     $c = $em->retrieve(Charlie::class, 'charlie');
     $this->assertEquals('Charlie', $c->getName());
     $this->assertCount(1, $c->getDelta());
     /** @var Delta $d */
     $d = $c->getDelta()[0];
     $this->assertEquals('Delta', $d->getName());
     $maintenance = new Maintenance($em);
     $maintenance->rebuild(CharlieRevised::class);
     /** @var DeltaRevised $d */
     $d = $em->retrieve(DeltaRevised::class, 'delta');
     $this->assertEquals('Delta', $d->getName());
     $this->assertCount(1, $d->getCharlie());
     /** @var CharlieRevised $c */
     $c = $d->getCharlie()[0];
     $this->assertEquals('Charlie', $c->getName());
 }
Esempio n. 11
0
 /**
  * @dataProvider entityManagerDataProvider
  * @param EntityManager $em
  */
 public function testSortOrder(EntityManager $em)
 {
     $category = new Category();
     $category->setId(600);
     $em->persist($category);
     for ($i = 0; $i < 15; $i++) {
         $article = new Article();
         $article->setId(601 + $i);
         $article->setTitle('Art ' . (601 + $i));
         $time = new \DateTime();
         $time->modify('+' . ($i + 1) . ' minutes');
         $article->setSortDate($time);
         $article->setCanonicalCategory($category);
         $em->persist($article);
     }
     $em->flush();
     /** @var Article $article */
     // Date sorting -
     $results = $em->sortedQuery(new SortedQuery($category, 'articles', 'sort_date'));
     $this->assertCount(15, $results);
     $article = $results[0];
     $this->assertEquals('Art 601', $article->getTitle());
     $results = $em->sortedQuery(new SortedQuery($category, 'articles', 'sort_date', Direction::DESC()));
     $this->assertCount(15, $results);
     $this->assertEquals(15, $results->getFullSize());
     $article = $results[0];
     $this->assertEquals('Art 615', $article->getTitle());
     $results = $em->sortedQuery(new SortedQuery($category, 'articles', 'sort_date', Direction::DESC(), 5, -6), true);
     $this->assertCount(5, $results);
     $this->assertEquals(15, $results->getFullSize());
     $article = $results[0];
     $this->assertEquals('Art 610', $article->getTitle());
     $article = $results[4];
     $this->assertEquals('Art 606', $article->getTitle());
     $results = $em->sortedQuery(new SortedQuery($category, 'articles', 'sort_date', Direction::ASC(), 2, 5));
     $this->assertCount(4, $results);
     $this->assertNull($results->getFullSize());
     $article = $results[0];
     $this->assertEquals('Art 603', $article->getTitle());
     $results = $em->sortedQuery(new SortedQuery($category, 'articles', 'sort_date', Direction::ASC(), 20, 29));
     $this->assertCount(0, $results);
     $results = $em->sortedQuery(new SortedQuery($category, 'articles', 'title'));
     $this->assertCount(15, $results);
     $article = $results[0];
     $this->assertEquals('Art 601', $article->getTitle());
     // Lexicographic sorting -
     $results = $em->sortedQuery(new SortedQuery($category, 'articles', 'title'));
     $this->assertCount(15, $results);
     $article = $results[0];
     $this->assertEquals('Art 601', $article->getTitle());
     $results = $em->sortedQuery(new SortedQuery($category, 'articles', 'title', Direction::DESC()));
     $this->assertCount(15, $results);
     $article = $results[0];
     $this->assertEquals('Art 615', $article->getTitle());
     // Modify an entity's sort-by column
     $article = $em->retrieve(Article::class, 609);
     $time = $article->getSortDate();
     $time->modify('+1 day');
     $article->setSortDate($time);
     $em->persist($article)->flush();
     $results = $em->sortedQuery(new SortedQuery($category, 'articles', 'sort_date', Direction::DESC()));
     $this->assertCount(15, $results);
     $article = $results[0];
     $this->assertEquals('Art 609', $article->getTitle());
 }
Esempio n. 12
0
 /**
  * @dataProvider entityManagerDataProvider
  * @param EntityManager $em
  */
 public function testNullObjectSerialisation(EntityManager $em)
 {
     $product = new Product();
     $product->setId(701)->setEnum(null);
     $em->persist($product)->flush();
     /** @var Product $r_product */
     $r_product = $em->retrieve(Product::class, '701');
     $this->assertNull($r_product->getEnum());
 }