protected function createAndUpdate(ORMMetadata $class, EntityRepository $repository)
 {
     $paginator = new Nette\Utils\Paginator();
     $paginator->itemsPerPage = 1000;
     $countQuery = $this->buildCountForUpdateQuery($repository)->getQuery();
     $paginator->itemCount = $countQuery->getSingleScalarResult();
     $this->onIndexStart($this, $paginator, $class);
     if ($paginator->itemCount <= 0) {
         return;
     }
     $qb = $this->buildSelectForUpdateQuery($repository, $class);
     if ($identifier = $class->getSingleIdentifierColumnName()) {
         $qb->orderBy(sprintf('e.%s', $identifier), 'ASC');
     }
     $lastId = NULL;
     $selectQueryBuilder = $qb->setMaxResults($paginator->getLength());
     while (1) {
         if ($lastId !== NULL) {
             $qbCopy = clone $selectQueryBuilder;
             $query = $qbCopy->andWhere(sprintf('e.%s > :lastId', $identifier))->setParameter('lastId', $lastId)->getQuery();
         } else {
             $query = $selectQueryBuilder->getQuery();
             if (!$identifier) {
                 $query->setFirstResult($paginator->getOffset());
             }
         }
         $beginTime = microtime(TRUE);
         $entities = $query->getResult();
         $this->postFetch($entities, $repository, $class);
         $loadedTime = microtime(TRUE);
         $this->doPersistEntities($entities);
         $this->searchManager->flush();
         $this->searchManager->clear();
         try {
             $this->onItemsIndexed($this, $entities);
         } catch (\Exception $e) {
         }
         if ($identifier) {
             $lastIdentifier = $class->getIdentifierValues(end($entities));
             // [id => value]
             $lastId = reset($lastIdentifier);
             if (!is_numeric($lastId)) {
                 trigger_error(E_USER_WARNING, 'Expected numeric identifier');
             }
         }
         $this->entityManager->clear();
         $this->onIndexStats($this, $class, microtime(TRUE) - $loadedTime, $loadedTime - $beginTime);
         if ($paginator->isLast()) {
             break;
         }
         $paginator->page += 1;
     }
 }