retrieveByIndex() public method

Retrieve an entity by an index
public retrieveByIndex ( string $class_name, string $index_name, string $index_key, boolean $use_cache = true ) : object
$class_name string
$index_name string
$index_key string
$use_cache boolean
return object
Ejemplo n.º 1
0
 /**
  * @dataProvider entityManagerDataProvider
  * @param EntityManager $em
  */
 public function testRebuildIndex(EntityManager $em)
 {
     // Create an article with a slug
     $article = new SluggedArticle();
     $article->setId(8343)->setName('foo')->setSlug('bar');
     $em->persist($article)->flush();
     // Confirm slug works
     /** @var SluggedArticle $article */
     $article = $em->retrieveByIndex(SluggedArticle::class, 'slug', 'bar', false);
     $this->assertEquals('foo', $article->getName());
     $index = $em->getMapper()->getEntityMetadata($article)->getIndexByName('slug');
     // Corrupt the slug, two steps required:
     // 1. Set a new slug
     $em->getDriver()->setSingleValueIndex($em->getKeyScheme()->getIndexKey($index, 'evil'), $article->getId());
     // 2. Remove the correct slug
     $em->getDriver()->clearSingleValueIndex($em->getKeyScheme()->getIndexKey($index, 'bar'));
     $em->getDriver()->flush();
     // Confirm old slug no longer works
     try {
         $em->retrieveByIndex(SluggedArticle::class, 'slug', 'bar', false);
         $this->fail('Old index succeeded');
     } catch (NotFoundException $e) {
     }
     // Confirm new slug does work
     $article = $em->retrieveByIndex(SluggedArticle::class, 'slug', 'evil', false);
     $this->assertEquals('foo', $article->getName());
     // Run maintenance over the table, this should correct the slug
     $maintenance = new Maintenance($em);
     $maintenance->rebuild(SluggedArticle::class);
     // Confirm correct slug works
     $article = $em->retrieveByIndex(SluggedArticle::class, 'slug', 'bar', false);
     $this->assertEquals('foo', $article->getName());
     // The corrupted slug should still work, this is unideal, but there is no reference to it for the maintenance
     // service to know to remove it
     $article = $em->retrieveByIndex(SluggedArticle::class, 'slug', 'evil', false);
     $this->assertEquals('foo', $article->getName());
 }
Ejemplo n.º 2
0
 /**
  * @dataProvider entityManagerDataProvider
  * @param EntityManager $em
  */
 public function testRelatedIndexDeletion(EntityManager $em)
 {
     $home = new Address();
     $home->setId(44)->setStreet('Oxford St');
     $work = new Address();
     $work->setId(45)->setStreet('George St');
     $user = new User();
     $user->setId(23)->setName('Barry')->setAddress($home);
     $em->persist($user)->persist($home)->persist($work)->flush();
     /** @var User|OrmProxyInterface $user_home */
     $user_home = $em->retrieveByIndex(User::class, 'slug', $user->getId() . '.' . $home->getId());
     $this->assertEquals(23, $user_home->getId());
     $this->assertEquals('Oxford St', $user_home->getAddress()->getStreet());
     $slug = $user_home->getIndexOriginalValue('slug');
     $this->assertEquals('23.44', $slug);
     $user_home->setName('Other Barry');
     $user_home->setAddress($work);
     $em->persist($user_home)->flush();
     /** @var User $user_work */
     $user_work = $em->retrieveByIndex(User::class, 'slug', $user->getId() . '.' . $work->getId());
     $this->assertEquals(23, $user_work->getId());
     $this->assertEquals('George St', $user_work->getAddress()->getStreet());
     $this->setExpectedException(NotFoundException::class);
     $em->retrieveByIndex(User::class, 'slug', $user->getId() . '.' . $home->getId());
 }
Ejemplo n.º 3
0
 /**
  * @dataProvider entityManagerDataProvider
  * @param EntityManager $em
  */
 public function testCacheIndex(EntityManager $em)
 {
     $product = new IndexedEntity();
     $product->setId1(212)->setId2('test')->setAlpha('index-test')->setBravo(888);
     $em->persist($product)->flush();
     $r = $em->retrieve(IndexedEntity::class, '212.test');
     $r->setBravo(999);
     $r1 = $em->retrieveByIndex(IndexedEntity::class, 'ab', 'index-test.888');
     $this->assertEquals(999, $r1->getBravo());
     $r2 = $em->retrieveByIndex(IndexedEntity::class, 'ab', 'index-test.888', false);
     $this->assertEquals(888, $r2->getBravo());
     $em->getCache()->purge(IndexedEntity::class, '212.test');
     $r3 = $em->retrieveByIndex(IndexedEntity::class, 'ab', 'index-test.888');
     $this->assertEquals(888, $r3->getBravo());
 }