Bravo3\Orm\Services\EntityManager::retrieveByIndex PHP Method

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
    public function retrieveByIndex($class_name, $index_name, $index_key, $use_cache = true)
    {
        $metadata = $this->mapper->getEntityMetadata($class_name);
        $index = $metadata->getIndexByName($index_name);
        if (!$index) {
            throw new InvalidArgumentException('Index "' . $index_name . '" is not valid');
        }
        $id = $this->driver->getSingleValueIndex($this->key_scheme->getIndexKey($index, $index_key));
        if (!$id) {
            throw new NotFoundException('Index "' . $index_key . '" not found');
        }
        return $this->retrieve($class_name, $id, $use_cache);
    }

Usage Example

Esempio 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());
 }
All Usage Examples Of Bravo3\Orm\Services\EntityManager::retrieveByIndex