本文整理汇总了PHP中Doctrine\ODM\MongoDB\Mapping\ClassMetadataInfo::mapField方法的典型用法代码示例。如果您正苦于以下问题:PHP ClassMetadataInfo::mapField方法的具体用法?PHP ClassMetadataInfo::mapField怎么用?PHP ClassMetadataInfo::mapField使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Doctrine\ODM\MongoDB\Mapping\ClassMetadataInfo
的用法示例。
在下文中一共展示了ClassMetadataInfo::mapField方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的PHP代码示例。
示例1: generate
/**
* @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);
}
}
示例2: setUp
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'));
}
示例3: testDefaultDiscriminatorField
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');
}
示例4: mapField
/**
* 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;
}
示例5: read
/**
* 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;
}
示例6: generateBookDocumentFixture
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;
}
示例7: addFieldMapping
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);
}
示例8: addFieldMapping
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);
}
示例9: addFieldMapping
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);
}
示例10: testCollectionClassHasToImplementCommonInterface
/**
* @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'));
}
示例11: testSimpleReferenceRequiresTargetDocument
/**
* @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));
}
示例12: testOwningAndInversedRefsNeedTargetDocument
/**
* @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);
}
示例13: addFieldMapping
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);
}
示例14: testNoCollectionsInShardKey
/**
* @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));
}
示例15: addFieldMapping
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);
}