/** * @param \Symfony\Component\HttpKernel\Bundle\BundleInterface $bundle * @param string $document * @param array $fields * @param Boolean $withRepository * @throws \RuntimeException */ public function generate(BundleInterface $bundle, $document, array $fields, $withRepository) { $config = $this->documentManager->getConfiguration(); $config->addDocumentNamespace($bundle->getName(), $bundle->getNamespace() . '\\Document'); $documentClass = $config->getDocumentNamespace($bundle->getName()) . '\\' . $document; $documentPath = $bundle->getPath() . '/Document/' . str_replace('\\', '/', $document) . '.php'; if (file_exists($documentPath)) { throw new \RuntimeException(sprintf('Document "%s" already exists.', $documentClass)); } $class = new ClassMetadataInfo($documentClass); if ($withRepository) { $class->setCustomRepositoryClass($documentClass . 'Repository'); } $class->mapField(array('fieldName' => 'id', 'type' => 'integer', 'id' => true)); $class->setIdGeneratorType(ClassMetadataInfo::GENERATOR_TYPE_AUTO); foreach ($fields as $field) { $class->mapField($field); } $documentGenerator = $this->getDocumentGenerator(); $documentCode = $documentGenerator->generateDocumentClass($class); $this->filesystem->mkdir(dirname($documentPath)); file_put_contents($documentPath, rtrim($documentCode) . PHP_EOL, LOCK_EX); if ($withRepository) { $path = $bundle->getPath() . str_repeat('/..', substr_count(get_class($bundle), '\\')); $this->getRepositoryGenerator()->writeDocumentRepositoryClass($class->customRepositoryClassName, $path); } }
protected function setUp() { parent::setUp(); $this->filesystem = new Filesystem(); $this->documentName = ucfirst($this->getName()); $this->tmpDir = sys_get_temp_dir() . '/ismaambrosi'; $this->metadata = new ClassMetadataInfo($this->documentName); $this->metadata->mapField(array('name' => 'id', 'id' => true, 'strategy' => 'auto')); $this->metadata->mapField(array('fieldName' => 'name', 'type' => 'string')); $this->metadata->mapField(array('fieldName' => 'description', 'type' => 'string')); }
public function testDefaultDiscriminatorField() { $cm = new ClassMetadataInfo('stdClass'); $cm->mapField(array('fieldName' => 'assoc', 'reference' => true, 'type' => 'one')); $cm->mapField(array('fieldName' => 'assocWithTargetDocument', 'reference' => true, 'type' => 'one', 'targetDocument' => 'stdClass')); $cm->mapField(array('fieldName' => 'assocWithDiscriminatorField', 'reference' => true, 'type' => 'one', 'discriminatorField' => 'type')); $mapping = $cm->getFieldMapping('assoc'); $this->assertEquals(ClassMetadataInfo::DEFAULT_DISCRIMINATOR_FIELD, $mapping['discriminatorField'], 'Default discriminator field is set for associations without targetDocument and discriminatorField options'); $mapping = $cm->getFieldMapping('assocWithTargetDocument'); $this->assertArrayNotHasKey('discriminatorField', $mapping, 'Default discriminator field is not set for associations with targetDocument option'); $mapping = $cm->getFieldMapping('assocWithDiscriminatorField'); $this->assertEquals('type', $mapping['discriminatorField'], 'Default discriminator field is not set for associations with discriminatorField option'); }
/** * Map a field. * * @param array $mapping The mapping information. * @return void */ public function mapField(array $mapping) { $mapping = parent::mapField($mapping); $reflProp = $this->reflClass->getProperty($mapping['fieldName']); $reflProp->setAccessible(true); $this->reflFields[$mapping['fieldName']] = $reflProp; }
/** * Convert thrift class spec to doctrine ClassMetadataInfo * * @param $className * @return ClassMetadataInfo * @throws \Doctrine\ODM\MongoDB\Mapping\MappingException * @throws \Exception */ public function read($className) { $cmi = new ClassMetadataInfo($className); $types = array(TType::BOOL => 'bool', TType::I32 => 'integer', TType::I64 => 'integer', TType::DOUBLE => 'float', TType::UTF7 => 'string', TType::UTF8 => 'string', TType::LST => 'many', TType::STRUCT => 'one', TType::MAP => 'hash'); $spec = $this->loadSpec($className); foreach ($spec as $id => $fieldSpec) { if (!isset($types[$fieldSpec['type']])) { throw new \Exception('Unknown thrift -> doctrine type conversion'); } $type = $types[$fieldSpec['type']]; if ($fieldSpec['type'] == TType::LST) { // list if ($fieldSpec['etype'] == TType::STRUCT) { // list of structs $mapping = array('fieldName' => $fieldSpec['var'], 'name' => $id . '_' . $fieldSpec['var'], 'embedded' => true, 'type' => $type, 'targetDocument' => substr($fieldSpec['elem']['class'], 1)); } else { // list of simple types $mapping = array('fieldName' => $fieldSpec['var'], 'name' => $id . '_' . $fieldSpec['var'], 'type' => 'hash'); } } elseif ($fieldSpec['type'] == TType::STRUCT) { // single struct $mapping = array('fieldName' => $fieldSpec['var'], 'name' => $id . '_' . $fieldSpec['var'], 'embedded' => true, 'type' => $type, 'targetDocument' => substr($fieldSpec['class'], 1)); } else { // simple field $mapping = array('fieldName' => $fieldSpec['var'], 'name' => $id . '_' . $fieldSpec['var'], 'type' => $type); } $cmi->mapField($mapping); } return $cmi; }
public function generateBookDocumentFixture() { $metadata = new ClassMetadataInfo($this->namespace . '\\DocumentGeneratorBook'); $metadata->namespace = $this->namespace; $metadata->customRepositoryClassName = $this->namespace . '\\DocumentGeneratorBookRepository'; $metadata->collection = 'book'; $metadata->mapField(array('fieldName' => 'name', 'type' => 'string')); $metadata->mapField(array('fieldName' => 'status', 'type' => 'string')); $metadata->mapField(array('fieldName' => 'id', 'type' => 'integer', 'id' => true)); $metadata->mapOneReference(array('fieldName' => 'author', 'targetDocument' => 'Doctrine\\ODM\\MongoDB\\Tests\\Tools\\DocumentGeneratorAuthor')); $metadata->mapManyReference(array('fieldName' => 'comments', 'targetDocument' => 'Doctrine\\ODM\\MongoDB\\Tests\\Tools\\DocumentGeneratorComment')); $metadata->addLifecycleCallback('loading', 'postLoad'); $metadata->addLifecycleCallback('willBeRemoved', 'preRemove'); $metadata->setIdGeneratorType(ClassMetadataInfo::GENERATOR_TYPE_AUTO); $this->generator->writeDocumentClass($metadata, $this->tmpDir); return $metadata; }
private function addFieldMapping(ClassMetadataInfo $class, $mapping) { if (isset($mapping['name'])) { $name = $mapping['name']; } elseif (isset($mapping['fieldName'])) { $name = $mapping['fieldName']; } else { throw new \InvalidArgumentException('Cannot infer a MongoDB name from the mapping'); } if (isset($mapping['type']) && $mapping['type'] === 'collection') { // Note: this strategy is not actually used $mapping['strategy'] = isset($mapping['strategy']) ? $mapping['strategy'] : 'pushAll'; } $class->mapField($mapping); // Index this field if either "index", "unique", or "sparse" are set if (!(isset($mapping['index']) || isset($mapping['unique']) || isset($mapping['sparse']))) { return; } $keys = array($name => isset($mapping['order']) ? $mapping['order'] : 'asc'); $options = array(); if (isset($mapping['background'])) { $options['background'] = (bool) $mapping['background']; } if (isset($mapping['drop-dups'])) { $options['dropDups'] = (bool) $mapping['drop-dups']; } if (isset($mapping['index-name'])) { $options['name'] = (string) $mapping['index-name']; } if (isset($mapping['safe'])) { $options['safe'] = (bool) $mapping['safe']; } if (isset($mapping['sparse'])) { $options['sparse'] = (bool) $mapping['sparse']; } if (isset($mapping['unique'])) { $options['unique'] = (bool) $mapping['unique']; } $class->addIndex($keys, $options); }
private function addFieldMapping(ClassMetadataInfo $class, $mapping) { $keys = null; $name = isset($mapping['name']) ? $mapping['name'] : $mapping['fieldName']; if (isset($mapping['index'])) { $keys = array($name => isset($mapping['index']['order']) ? $mapping['index']['order'] : 'asc'); } if (isset($mapping['unique'])) { $keys = array($name => isset($mapping['unique']['order']) ? $mapping['unique']['order'] : 'asc'); } if ($keys !== null) { $options = array(); if (isset($mapping['index'])) { $options = $mapping['index']; } elseif (isset($mapping['unique'])) { $options = $mapping['unique']; $options['unique'] = true; } $class->addIndex($keys, $options); } $class->mapField($mapping); }
private function addFieldMapping(ClassMetadataInfo $class, $mapping) { $keys = null; $name = isset($mapping['name']) ? $mapping['name'] : $mapping['fieldName']; if (isset($mapping['type']) && $mapping['type'] === 'collection') { $mapping['strategy'] = isset($mapping['strategy']) ? $mapping['strategy'] : 'pushAll'; } if (isset($mapping['index'])) { $keys = array( $name => isset($mapping['order']) ? $mapping['order'] : 'asc' ); } if (isset($mapping['unique'])) { $keys = array( $name => isset($mapping['order']) ? $mapping['order'] : 'asc' ); } if ($keys !== null) { $options = array(); if (isset($mapping['index-name'])) { $options['name'] = (string) $mapping['index-name']; } if (isset($mapping['drop-dups'])) { $options['dropDups'] = (boolean) $mapping['drop-dups']; } if (isset($mapping['background'])) { $options['background'] = (boolean) $mapping['background']; } if (isset($mapping['safe'])) { $options['safe'] = (boolean) $mapping['safe']; } if (isset($mapping['unique'])) { $options['unique'] = (boolean) $mapping['unique']; } $class->addIndex($keys, $options); } $class->mapField($mapping); }
/** * @expectedException \Doctrine\ODM\MongoDB\Mapping\MappingException * @expectedExceptionMessage stdClass used as custom collection class for stdClass::assoc has to implement Doctrine\Common\Collections\Collection interface. */ public function testCollectionClassHasToImplementCommonInterface() { $cm = new ClassMetadataInfo('stdClass'); $cm->mapField(array('fieldName' => 'assoc', 'reference' => true, 'type' => 'many', 'collectionClass' => 'stdClass')); }
/** * @expectedException \Doctrine\ODM\MongoDB\Mapping\MappingException * @expectedExceptionMessage Target document must be specified for simple reference: stdClass::assoc */ public function testSimpleReferenceRequiresTargetDocument() { $cm = new ClassMetadataInfo('stdClass'); $cm->mapField(array('fieldName' => 'assoc', 'reference' => true, 'type' => 'one', 'simple' => true)); }
/** * @dataProvider provideOwningAndInversedRefsNeedTargetDocument * @expectedException \Doctrine\ODM\MongoDB\Mapping\MappingException */ public function testOwningAndInversedRefsNeedTargetDocument($config) { $config = array_merge($config, array('fieldName' => 'many', 'reference' => true, 'strategy' => 'atomicSet')); $cm = new ClassMetadataInfo('stdClass'); $cm->isEmbeddedDocument = true; $cm->mapField($config); }
private function addFieldMapping(ClassMetadataInfo $class, $mapping) { if (isset($mapping['name'])) { $name = $mapping['name']; } elseif (isset($mapping['fieldName'])) { $name = $mapping['fieldName']; } else { throw new \InvalidArgumentException('Cannot infer a MongoDB name from the mapping'); } $class->mapField($mapping); if (!(isset($mapping['index']) || isset($mapping['unique']) || isset($mapping['sparse']))) { return; } // Index this field if either "index", "unique", or "sparse" are set $keys = array($name => 'asc'); if (isset($mapping['index']['order'])) { $keys[$name] = $mapping['index']['order']; unset($mapping['index']['order']); } elseif (isset($mapping['unique']['order'])) { $keys[$name] = $mapping['unique']['order']; unset($mapping['unique']['order']); } elseif (isset($mapping['sparse']['order'])) { $keys[$name] = $mapping['sparse']['order']; unset($mapping['sparse']['order']); } $options = array(); if (isset($mapping['index'])) { $options = is_array($mapping['index']) ? $mapping['index'] : array(); } elseif (isset($mapping['unique'])) { $options = is_array($mapping['unique']) ? $mapping['unique'] : array(); $options['unique'] = true; } elseif (isset($mapping['sparse'])) { $options = is_array($mapping['sparse']) ? $mapping['sparse'] : array(); $options['sparse'] = true; } $class->addIndex($keys, $options); }
/** * @expectedException \Doctrine\ODM\MongoDB\Mapping\MappingException * @expectedExceptionMessage No multikey indexes are allowed in the shard key */ public function testNoCollectionsInShardKey() { $cm = new ClassMetadataInfo('stdClass'); $cm->mapField(['fieldName' => 'collection', 'type' => 'collection']); $cm->setShardKey(array('collection' => 1)); }
private function addFieldMapping(ClassMetadataInfo $class, $mapping) { $keys = null; if (isset($mapping['name'])) { $name = $mapping['name']; } elseif (isset($mapping['fieldName'])) { $name = $mapping['fieldName']; } else { throw new \InvalidArgumentException('Cannot infer a MongoDB name from the mapping'); } if (isset($mapping['index'])) { $keys = array($name => isset($mapping['index']['order']) ? $mapping['index']['order'] : 'asc'); } if (isset($mapping['unique'])) { $keys = array($name => isset($mapping['unique']['order']) ? $mapping['unique']['order'] : 'asc'); } if ($keys !== null) { $options = array(); if (isset($mapping['index'])) { $options = $mapping['index']; } elseif (isset($mapping['unique'])) { $options = $mapping['unique']; $options['unique'] = true; } elseif (isset($mapping['sparse'])) { $options = $mapping['sparse']; $options['sparse'] = true; } $class->addIndex($keys, $options); } $class->mapField($mapping); }
/** * @expectedException \Doctrine\ODM\MongoDB\Mapping\MappingException * @expectedExceptionMessage ReferenceMany's sort can not be used with addToSet and pushAll strategies, pushAll used in stdClass::ref */ public function testReferenceManySortMustNotBeUsedWithNonSetCollectionStrategy() { $cm = new ClassMetadataInfo('stdClass'); $cm->mapField(array('fieldName' => 'ref', 'reference' => true, 'strategy' => 'pushAll', 'type' => 'many', 'sort' => array('foo' => 1))); }
/** * @expectedException \Doctrine\ODM\MongoDB\Mapping\MappingException * @expectedExceptionMessage You must not change identifier field's type: stdClass::id */ public function testIdFieldsTypeMustNotBeOverrriden() { $cm = new ClassMetadataInfo('stdClass'); $cm->setIdentifier('id'); $cm->mapField(array('fieldName' => 'id', 'type' => 'string')); }
private function addFieldMapping(ClassMetadataInfo $class, $mapping) { if (isset($mapping['name'])) { $name = $mapping['name']; } elseif (isset($mapping['fieldName'])) { $name = $mapping['fieldName']; } else { throw new \InvalidArgumentException('Cannot infer a MongoDB name from the mapping'); } $class->mapField($mapping); if (!(isset($mapping['index']) || isset($mapping['unique']) || isset($mapping['sparse']))) { return; } // Multiple index specifications in one field mapping is ambiguous if ((isset($mapping['index']) && is_array($mapping['index'])) + (isset($mapping['unique']) && is_array($mapping['unique'])) + (isset($mapping['sparse']) && is_array($mapping['sparse'])) > 1) { throw new \InvalidArgumentException('Multiple index specifications found among index, unique, and/or sparse fields'); } // Index this field if either "index", "unique", or "sparse" are set $keys = array($name => 'asc'); /* The "order" option is only used in the index specification and should * not be passed along as an index option. */ if (isset($mapping['index']['order'])) { $keys[$name] = $mapping['index']['order']; unset($mapping['index']['order']); } elseif (isset($mapping['unique']['order'])) { $keys[$name] = $mapping['unique']['order']; unset($mapping['unique']['order']); } elseif (isset($mapping['sparse']['order'])) { $keys[$name] = $mapping['sparse']['order']; unset($mapping['sparse']['order']); } /* Initialize $options from any array value among index, unique, and * sparse. Any boolean values for unique or sparse should be merged into * the options afterwards to ensure consistent parsing. */ $options = array(); $unique = null; $sparse = null; if (isset($mapping['index']) && is_array($mapping['index'])) { $options = $mapping['index']; } if (isset($mapping['unique'])) { if (is_array($mapping['unique'])) { $options = $mapping['unique'] + array('unique' => true); } else { $unique = (bool) $mapping['unique']; } } if (isset($mapping['sparse'])) { if (is_array($mapping['sparse'])) { $options = $mapping['sparse'] + array('sparse' => true); } else { $sparse = (bool) $mapping['sparse']; } } if (isset($unique)) { $options['unique'] = $unique; } if (isset($sparse)) { $options['sparse'] = $sparse; } $class->addIndex($keys, $options); }
/** * @expectedException \Doctrine\ODM\MongoDB\Mapping\MappingException * @expectedExceptionMessage atomicSet collection strategy can be used only in top level document, used in stdClass::many */ public function testAtomicCollectionUpdateUsageInEmbeddedDocument() { $cm = new ClassMetadataInfo('stdClass'); $cm->isEmbeddedDocument = true; $cm->mapField(array('fieldName' => 'many', 'reference' => true, 'type' => 'many', 'strategy' => 'atomicSet')); }
private function addFieldMapping(ClassMetadataInfo $class, $mapping) { $keys = null; if (isset($mapping['name'])) { $name = $mapping['name']; } elseif (isset($mapping['fieldName'])) { $name = $mapping['fieldName']; } else { throw new \InvalidArgumentException('Cannot infer a MongoDB name from the mapping'); } if (isset($mapping['type']) && $mapping['type'] === 'collection') { $mapping['strategy'] = isset($mapping['strategy']) ? $mapping['strategy'] : 'pushAll'; } if (isset($mapping['index'])) { $keys = array($name => isset($mapping['order']) ? $mapping['order'] : 'asc'); } if (isset($mapping['unique'])) { $keys = array($name => isset($mapping['order']) ? $mapping['order'] : 'asc'); } if ($keys !== null) { $options = array(); if (isset($mapping['index-name'])) { $options['name'] = (string) $mapping['index-name']; } if (isset($mapping['drop-dups'])) { $options['dropDups'] = (bool) $mapping['drop-dups']; } if (isset($mapping['background'])) { $options['background'] = (bool) $mapping['background']; } if (isset($mapping['safe'])) { $options['safe'] = (bool) $mapping['safe']; } if (isset($mapping['unique'])) { $options['unique'] = (bool) $mapping['unique']; } if (isset($mapping['sparse'])) { $options['sparse'] = (bool) $mapping['sparse']; } $class->addIndex($keys, $options); } $class->mapField($mapping); }
protected function mapField(ClassMetadataInfo $metadata, $name, array $config) { if (empty($config['type']) && empty($config['id'])) { return; } if (empty($config['name'])) { $config['name'] = $name; } if (!empty($config['type']) && ($config['type'] == 'one' || $config['type'] == 'many') && empty($config['reference'])) { $config['embedded'] = true; } if (!empty($config['id']) && empty($config['type'])) { $config['type'] = 'id'; } if (!empty($config['id']) && empty($config['strategy'])) { $config['strategy'] = 'auto'; } $metadata->mapField($config); }