Doctrine\ODM\MongoDB\SchemaManager::updateDocumentIndexes PHP Метод

updateDocumentIndexes() публичный Метод

Indexes that exist in MongoDB but not the document metadata will be deleted.
public updateDocumentIndexes ( string $documentName, integer $timeout = null )
$documentName string
$timeout integer Timeout (ms) for acknowledged index creation
    public function updateDocumentIndexes($documentName, $timeout = null)
    {
        $class = $this->dm->getClassMetadata($documentName);
        if ($class->isMappedSuperclass || $class->isEmbeddedDocument || $class->isQueryResultDocument) {
            throw new \InvalidArgumentException('Cannot update document indexes for mapped super classes, embedded documents or aggregation result documents.');
        }
        $documentIndexes = $this->getDocumentIndexes($documentName);
        $collection = $this->dm->getDocumentCollection($documentName);
        $mongoIndexes = $collection->getIndexInfo();
        /* Determine which Mongo indexes should be deleted. Exclude the ID index
         * and those that are equivalent to any in the class metadata.
         */
        $self = $this;
        $mongoIndexes = array_filter($mongoIndexes, function ($mongoIndex) use($documentIndexes, $self) {
            if ('_id_' === $mongoIndex['name']) {
                return false;
            }
            foreach ($documentIndexes as $documentIndex) {
                if ($self->isMongoIndexEquivalentToDocumentIndex($mongoIndex, $documentIndex)) {
                    return false;
                }
            }
            return true;
        });
        // Delete indexes that do not exist in class metadata
        foreach ($mongoIndexes as $mongoIndex) {
            if (isset($mongoIndex['name'])) {
                /* Note: MongoCollection::deleteIndex() cannot delete
                 * custom-named indexes, so use the deleteIndexes command.
                 */
                $collection->getDatabase()->command(array('deleteIndexes' => $collection->getName(), 'index' => $mongoIndex['name']));
            }
        }
        $this->ensureDocumentIndexes($documentName, $timeout);
    }

Usage Example

Пример #1
0
 /**
  * @param SchemaManager $sm
  * @param object $document
  */
 protected function processDocumentIndex(SchemaManager $sm, $document)
 {
     $sm->updateDocumentIndexes($document, $this->timeout);
 }