Bravo3\Orm\Services\Maintenance::rebuild PHP Method

rebuild() public method

The end result will be: - new inverse indices will be created - changes to serialisation will be updated on all entities - added/removed fields will be updated on all entities
public rebuild ( string $class_name, integer $batch_size = 100 )
$class_name string
$batch_size integer
    public function rebuild($class_name, $batch_size = 100)
    {
        $this->maintenanceOperation(function () use($class_name, $batch_size) {
            $metadata = $this->entity_manager->getMapper()->getEntityMetadata($class_name);
            $this->logger->info("Rebuilding `" . $metadata->getTableName() . "`..");
            $records = $this->entity_manager->indexedQuery(new IndexedQuery($class_name, ['@id' => '*']), false);
            $this->logger->info(number_format($records->count()) . ' records to rebuild, ' . number_format($batch_size) . ' at a time');
            $ts = microtime(true);
            $this->rebuildRecords($records, $batch_size);
            $delta = microtime(true) - $ts;
            $this->logger->info("Rebuild of `" . $metadata->getTableName() . "` completed in " . number_format($delta, 2) . " seconds");
        });
    }

Usage Example

Example #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\Maintenance::rebuild