Doctrine\ODM\MongoDB\SchemaManager::ensureDocumentIndexes PHP 메소드

ensureDocumentIndexes() 공개 메소드

Ensure the given document's indexes are created.
public ensureDocumentIndexes ( string $documentName, integer $timeout = null )
$documentName string
$timeout integer Timeout (ms) for acknowledged index creation
    public function ensureDocumentIndexes($documentName, $timeout = null)
    {
        $class = $this->dm->getClassMetadata($documentName);
        if ($class->isMappedSuperclass || $class->isEmbeddedDocument || $class->isQueryResultDocument) {
            throw new \InvalidArgumentException('Cannot create document indexes for mapped super classes, embedded documents or query result documents.');
        }
        if ($indexes = $this->getDocumentIndexes($documentName)) {
            $collection = $this->dm->getDocumentCollection($class->name);
            foreach ($indexes as $index) {
                $keys = $index['keys'];
                $options = $index['options'];
                if (!isset($options['safe']) && !isset($options['w'])) {
                    $options['w'] = 1;
                }
                if (isset($options['safe']) && !isset($options['w'])) {
                    $options['w'] = is_bool($options['safe']) ? (int) $options['safe'] : $options['safe'];
                    unset($options['safe']);
                }
                if (!isset($options['timeout']) && isset($timeout)) {
                    $options['timeout'] = $timeout;
                }
                $collection->ensureIndex($keys, $options);
            }
        }
    }

Usage Example

 protected function processDocumentIndex(SchemaManager $sm, $document)
 {
     $sm->ensureDocumentIndexes($document);
 }