本文整理汇总了PHP中Doctrine\ORM\Mapping\ClassMetadata::mapManyToOne方法的典型用法代码示例。如果您正苦于以下问题:PHP ClassMetadata::mapManyToOne方法的具体用法?PHP ClassMetadata::mapManyToOne怎么用?PHP ClassMetadata::mapManyToOne使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Doctrine\ORM\Mapping\ClassMetadata
的用法示例。
在下文中一共展示了ClassMetadata::mapManyToOne方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的PHP代码示例。
示例1: updateTranslationMetadata
protected function updateTranslationMetadata(ClassMetadata $metadata)
{
if (!$metadata->hasAssociation('translatable')) {
$metadata->mapManyToOne(['fieldName' => 'translatable', 'inversedBy' => 'translations', 'fetch' => $this->translationFetchMode, 'joinColumns' => [['name' => 'translatable_id', 'referencedColumnName' => 'id', 'onDelete' => 'CASCADE']], 'targetEntity' => substr($metadata->name, 0, -strlen('Translation'))]);
$metadata->table['uniqueConstraints'][] = ['name' => 'unique_translation', 'columns' => ['translatable_id', 'locale']];
}
}
示例2: mapTranslation
private function mapTranslation(ClassMetadata $classMetadata)
{
if (!$classMetadata->hasAssociation('translatable')) {
$classMetadata->mapManyToOne(['fieldName' => 'translatable', 'inversedBy' => 'translations', 'joinColumns' => [['name' => 'translatable_id', 'referencedColumnName' => 'id', 'onDelete' => 'CASCADE']], 'targetEntity' => substr($classMetadata->name, 0, -11)]);
}
$name = $classMetadata->getTableName() . '_unique_translation';
if (!$this->hasUniqueTranslationConstraint($classMetadata, $name)) {
$classMetadata->setPrimaryTable(['uniqueConstraints' => [['name' => $name, 'columns' => ['translatable_id', 'locale']]]]);
}
}
示例3: mapTranslation
/**
* @param ClassMetadata $classMetadata
* @throws \Doctrine\ORM\Mapping\MappingException
*/
private function mapTranslation(ClassMetadata $classMetadata)
{
if (!$classMetadata->hasAssociation('translatable')) {
$classMetadata->mapManyToOne(array('fieldName' => 'translatable', 'inversedBy' => 'translations', 'fetch' => $this->translationFetchMode, 'joinColumns' => array(array('name' => 'translatable_id', 'referencedColumnName' => 'id', 'onDelete' => 'CASCADE', 'nullable' => false)), 'targetEntity' => $this->getClassName($classMetadata)));
}
if (!$classMetadata->hasField('locale')) {
$classMetadata->mapField(array('fieldName' => 'locale', 'type' => 'string', 'length' => 10));
}
$name = $classMetadata->getTableName() . '_unique_translation';
if (!$this->hasUniqueTranslationConstraint($classMetadata, $name)) {
$classMetadata->setPrimaryTable(array('uniqueConstraints' => array(array('name' => $name, 'columns' => array('translatable_id', 'locale')))));
}
}
示例4: getMetadata
/**
* @return ClassMetadata
*/
public static function getMetadata()
{
$metadata = new ClassMetadata(static::class);
$metadata->setIdentifier(['id']);
$metadata->mapField(['fieldName' => 'id']);
$metadata->mapField(['fieldName' => 'a']);
$metadata->mapField(['fieldName' => 'b']);
$metadata->mapField(['fieldName' => 'c']);
$metadata->mapEmbedded(['fieldName' => 'd', 'class' => DummyEmbeddable::class, 'columnPrefix' => 'd']);
$metadata->mapManyToOne(['fieldName' => 'parent', 'targetEntity' => self::class, 'inversedBy' => 'children']);
$metadata->mapOneToMany(['fieldName' => 'children', 'targetEntity' => self::class, 'mappedBy' => 'parent']);
$metadata->wakeupReflection(new RuntimeReflectionService());
return $metadata;
}
示例5: mapTranslation
/**
* Add mapping data to a translation entity.
*
* @param ClassMetadata $metadata
*/
private function mapTranslation(ClassMetadata $metadata)
{
// In the case A -> B -> TranslationInterface, B might not have mapping defined as it
// is probably defined in A, so in that case, we just return.
if (!isset($this->configs[$metadata->name])) {
return;
}
$metadata->mapManyToOne(array('fieldName' => 'translatable', 'targetEntity' => $this->configs[$metadata->name]['model'], 'inversedBy' => 'translations', 'joinColumns' => array(array('name' => 'translatable_id', 'referencedColumnName' => 'id', 'onDelete' => 'CASCADE', 'nullable' => false))));
if (!$metadata->hasField('locale')) {
$metadata->mapField(array('fieldName' => 'locale', 'type' => 'string', 'nullable' => false));
}
// Map unique index.
$columns = array($metadata->getSingleAssociationJoinColumnName('translatable'), 'locale');
if (!$this->hasUniqueConstraint($metadata, $columns)) {
$constraints = isset($metadata->table['uniqueConstraints']) ? $metadata->table['uniqueConstraints'] : array();
$constraints[$metadata->getTableName() . '_uniq_trans'] = array('columns' => $columns);
$metadata->setPrimaryTable(array('uniqueConstraints' => $constraints));
}
}
示例6: mapTranslation
/**
* Add mapping data to a translation entity.
*
* @param ClassMetadata $metadata
*/
private function mapTranslation(ClassMetadata $metadata)
{
$className = $metadata->name;
try {
$resourceMetadata = $this->registry->getByClass($className);
} catch (\InvalidArgumentException $exception) {
return;
}
$translatableResourceMetadata = $this->registry->get(str_replace('_translation', '', $resourceMetadata->getAlias()));
$metadata->mapManyToOne(['fieldName' => 'translatable', 'targetEntity' => $translatableResourceMetadata->getClass('model'), 'inversedBy' => 'translations', 'joinColumns' => [['name' => 'translatable_id', 'referencedColumnName' => 'id', 'onDelete' => 'CASCADE', 'nullable' => false]]]);
if (!$metadata->hasField('locale')) {
$metadata->mapField(['fieldName' => 'locale', 'type' => 'string', 'nullable' => false]);
}
// Map unique index.
$columns = [$metadata->getSingleAssociationJoinColumnName('translatable'), 'locale'];
if (!$this->hasUniqueConstraint($metadata, $columns)) {
$constraints = isset($metadata->table['uniqueConstraints']) ? $metadata->table['uniqueConstraints'] : [];
$constraints[$metadata->getTableName() . '_uniq_trans'] = ['columns' => $columns];
$metadata->setPrimaryTable(['uniqueConstraints' => $constraints]);
}
}
示例7: mapTranslation
private function mapTranslation(ClassMetadata $classMetadata)
{
if (!$classMetadata->hasAssociation('translatable')) {
$classMetadata->mapManyToOne(['fieldName' => 'translatable', 'inversedBy' => 'translations', 'fetch' => $this->translationFetchMode, 'joinColumns' => [['name' => 'translatable_id', 'referencedColumnName' => 'id', 'onDelete' => 'CASCADE']], 'targetEntity' => $classMetadata->getReflectionClass()->getMethod('getTranslatableEntityClass')->invoke(null)]);
}
$name = $classMetadata->getTableName() . '_unique_translation';
if (!$this->hasUniqueTranslationConstraint($classMetadata, $name)) {
$classMetadata->table['uniqueConstraints'][$name] = ['columns' => ['translatable_id', 'locale']];
}
if (!($classMetadata->hasField('locale') || $classMetadata->hasAssociation('locale'))) {
$classMetadata->mapField(array('fieldName' => 'locale', 'type' => 'string'));
}
}
示例8: mapTranslation
/**
* Add mapping data to a translation entity
*
* @param ClassMetadata $mapping
* @return void
*/
private function mapTranslation(ClassMetadata $mapping)
{
$metadata = $this->getTranslatableMetadata($mapping->name);
// Map translatable relation
if (!$mapping->hasAssociation($metadata->translatable->name)) {
$targetMetadata = $this->getTranslatableMetadata($metadata->targetEntity);
$mapping->mapManyToOne(array('fieldName' => $metadata->translatable->name, 'targetEntity' => $metadata->targetEntity, 'inversedBy' => $targetMetadata->translations->name, 'joinColumns' => array(array('name' => 'translatable_id', 'referencedColumnName' => 'id', 'onDelete' => 'CASCADE', 'nullable' => false))));
}
// Map locale field
if (!$mapping->hasField($metadata->locale->name)) {
$mapping->mapField(array('fieldName' => $metadata->locale->name, 'type' => 'string'));
}
// Map unique index
$columns = array($mapping->getSingleAssociationJoinColumnName($metadata->translatable->name), $metadata->locale->name);
if (!$this->hasUniqueConstraint($mapping, $columns)) {
$constraints = isset($mapping->table['uniqueConstraints']) ? $mapping->table['uniqueConstraints'] : array();
$constraints[$mapping->getTableName() . '_uniq_trans'] = array('columns' => $columns);
$mapping->setPrimaryTable(array('uniqueConstraints' => $constraints));
}
}
示例9: testTargetEntityNotFound
/**
* @group ImproveErrorMessages
*/
public function testTargetEntityNotFound()
{
$cm = new ClassMetadata('Doctrine\\Tests\\Models\\CMS\\CmsUser');
$cm->initializeReflection(new \Doctrine\Common\Persistence\Mapping\RuntimeReflectionService());
$cm->mapManyToOne(array('fieldName' => 'address', 'targetEntity' => 'UnknownClass'));
$this->setExpectedException("Doctrine\\ORM\\Mapping\\MappingException", "The target-entity Doctrine\\Tests\\Models\\CMS\\UnknownClass cannot be found in 'Doctrine\\Tests\\Models\\CMS\\CmsUser#address'.");
$cm->validateAssocations();
}
示例10: testInvalidPropertyAssociationOverrideNameException
/**
* @group DDC-964
* @expectedException Doctrine\ORM\Mapping\MappingException
* @expectedExceptionMessage Invalid field override named 'invalidPropertyName' for class 'Doctrine\Tests\Models\DDC964\DDC964Admin
*/
public function testInvalidPropertyAssociationOverrideNameException()
{
$cm = new ClassMetadata('Doctrine\\Tests\\Models\\DDC964\\DDC964Admin');
$cm->initializeReflection(new \Doctrine\Common\Persistence\Mapping\RuntimeReflectionService());
$cm->mapManyToOne(array('fieldName' => 'address', 'targetEntity' => 'DDC964Address'));
$cm->setAssociationOverride('invalidPropertyName', array());
}
示例11: testInvalidCascade
/**
* @group DDC-1746
*/
public function testInvalidCascade()
{
$cm = new ClassMetadata('Doctrine\\Tests\\Models\\CMS\\CmsUser');
$cm->initializeReflection(new \Doctrine\Common\Persistence\Mapping\RuntimeReflectionService());
$this->setExpectedException("Doctrine\\ORM\\Mapping\\MappingException", "Invalid cascade option(s) specified: 'invalid'. Only 'remove', 'persist', 'refresh', 'merge' and 'detach' are allowed.");
$cm->mapManyToOne(array('fieldName' => 'address', 'targetEntity' => 'UnknownClass', 'cascade' => array('invalid')));
}
示例12: mapUserAccess
private function mapUserAccess(ClassMetadata $classMetadata)
{
if (!$classMetadata->hasAssociation('userAccessible')) {
$classMetadata->mapManyToOne(['fieldName' => 'userAccessible', 'inversedBy' => 'userAccesses', 'joinColumns' => [['name' => 'userAccessible_id', 'referencedColumnName' => 'id', 'onDelete' => 'CASCADE']], 'targetEntity' => substr($classMetadata->name, 0, -10)]);
}
}
示例13: setServerModuleMetaData
private function setServerModuleMetaData(ClassMetadata $metadata)
{
$metadata->mapManyToOne(['targetEntity' => $this->serverClass, 'fieldName' => 'server', 'inversedBy' => 'modules']);
}
示例14: loadMetadataForClass
/**
* Loads the metadata for the specified class into the provided container.
*/
public function loadMetadataForClass($className, ClassMetadata $metadata)
{
$annotClass = new \Addendum\ReflectionAnnotatedClass($className);
// Evaluate DoctrineEntity annotation
if (($entityAnnot = $annotClass->getAnnotation('DoctrineEntity')) === false) {
throw DoctrineException::updateMe("{$className} is no entity.");
}
$metadata->setCustomRepositoryClass($entityAnnot->repositoryClass);
// Evaluate DoctrineTable annotation
if ($tableAnnot = $annotClass->getAnnotation('DoctrineTable')) {
$metadata->setPrimaryTable(array('name' => $tableAnnot->name, 'schema' => $tableAnnot->schema, 'catalog' => $tableAnnot->catalog));
}
// Evaluate DoctrineInheritanceType annotation
if ($inheritanceTypeAnnot = $annotClass->getAnnotation('DoctrineInheritanceType')) {
$metadata->setInheritanceType($inheritanceTypeAnnot->value);
}
// Evaluate DoctrineDiscriminatorColumn annotation
if ($discrColumnAnnot = $annotClass->getAnnotation('DoctrineDiscriminatorColumn')) {
$metadata->setDiscriminatorColumn(array('name' => $discrColumnAnnot->name, 'type' => $discrColumnAnnot->type, 'length' => $discrColumnAnnot->length));
}
// Evaluate DoctrineDiscriminatorMap annotation
if ($discrValueAnnot = $annotClass->getAnnotation('DoctrineDiscriminatorValue')) {
$metadata->setDiscriminatorValue($discrValueAnnot->value);
}
// Evaluate DoctrineSubClasses annotation
if ($subClassesAnnot = $annotClass->getAnnotation('DoctrineSubClasses')) {
$metadata->setSubclasses($subClassesAnnot->value);
}
// Evaluate DoctrineChangeTrackingPolicy annotation
if ($changeTrackingAnnot = $annotClass->getAnnotation('DoctrineChangeTrackingPolicy')) {
$metadata->setChangeTrackingPolicy($changeTrackingAnnot->value);
}
// Evaluate annotations on properties/fields
foreach ($annotClass->getProperties() as $property) {
if ($metadata->hasField($property->getName())) {
continue;
}
$mapping = array();
$mapping['fieldName'] = $property->getName();
// Check for DoctrineJoinColummn/DoctrineJoinColumns annotations
$joinColumns = array();
if ($joinColumnAnnot = $property->getAnnotation('DoctrineJoinColumn')) {
$joinColumns[] = array('name' => $joinColumnAnnot->name, 'referencedColumnName' => $joinColumnAnnot->referencedColumnName, 'unique' => $joinColumnAnnot->unique, 'nullable' => $joinColumnAnnot->nullable, 'onDelete' => $joinColumnAnnot->onDelete, 'onUpdate' => $joinColumnAnnot->onUpdate);
} else {
if ($joinColumnsAnnot = $property->getAnnotation('DoctrineJoinColumns')) {
$joinColumns = $joinColumnsAnnot->value;
}
}
// Field can only be annotated with one of: DoctrineColumn,
// DoctrineOneToOne, DoctrineOneToMany, DoctrineManyToOne, DoctrineManyToMany
if ($columnAnnot = $property->getAnnotation('DoctrineColumn')) {
if ($columnAnnot->type == null) {
throw DoctrineException::updateMe("Missing type on property " . $property->getName());
}
$mapping['type'] = $columnAnnot->type;
$mapping['length'] = $columnAnnot->length;
$mapping['nullable'] = $columnAnnot->nullable;
if ($idAnnot = $property->getAnnotation('DoctrineId')) {
$mapping['id'] = true;
}
if ($generatedValueAnnot = $property->getAnnotation('DoctrineGeneratedValue')) {
$metadata->setIdGeneratorType($generatedValueAnnot->strategy);
}
$metadata->mapField($mapping);
// Check for SequenceGenerator/TableGenerator definition
if ($seqGeneratorAnnot = $property->getAnnotation('DoctrineSequenceGenerator')) {
$metadata->setSequenceGeneratorDefinition(array('sequenceName' => $seqGeneratorAnnot->sequenceName, 'allocationSize' => $seqGeneratorAnnot->allocationSize, 'initialValue' => $seqGeneratorAnnot->initialValue));
} else {
if ($tblGeneratorAnnot = $property->getAnnotation('DoctrineTableGenerator')) {
throw new DoctrineException("DoctrineTableGenerator not yet implemented.");
}
}
} else {
if ($oneToOneAnnot = $property->getAnnotation('DoctrineOneToOne')) {
$mapping['targetEntity'] = $oneToOneAnnot->targetEntity;
$mapping['joinColumns'] = $joinColumns;
$mapping['mappedBy'] = $oneToOneAnnot->mappedBy;
$mapping['cascade'] = $oneToOneAnnot->cascade;
$metadata->mapOneToOne($mapping);
} else {
if ($oneToManyAnnot = $property->getAnnotation('DoctrineOneToMany')) {
$mapping['mappedBy'] = $oneToManyAnnot->mappedBy;
$mapping['targetEntity'] = $oneToManyAnnot->targetEntity;
$mapping['cascade'] = $oneToManyAnnot->cascade;
$metadata->mapOneToMany($mapping);
} else {
if ($manyToOneAnnot = $property->getAnnotation('DoctrineManyToOne')) {
$mapping['joinColumns'] = $joinColumns;
$mapping['cascade'] = $manyToOneAnnot->cascade;
$mapping['targetEntity'] = $manyToOneAnnot->targetEntity;
$metadata->mapManyToOne($mapping);
} else {
if ($manyToManyAnnot = $property->getAnnotation('DoctrineManyToMany')) {
$joinTable = array();
if ($joinTableAnnot = $property->getAnnotation('DoctrineJoinTable')) {
$joinTable = array('name' => $joinTableAnnot->name, 'schema' => $joinTableAnnot->schema, 'catalog' => $joinTableAnnot->catalog, 'joinColumns' => $joinTableAnnot->joinColumns, 'inverseJoinColumns' => $joinTableAnnot->inverseJoinColumns);
}
//.........这里部分代码省略.........
示例15: processNodeTree
/**
* Process the node tree metadata.
*
* @param LoadClassMetadataEventArgs $eventArgs
* @param ClassMetadata $metadata
* @param Tree $annotation
*/
protected function processNodeTree(LoadClassMetadataEventArgs $eventArgs, ClassMetadata $metadata, Tree $annotation)
{
$className = $metadata->name;
// Holds the closure tree data
$closureTreeData = array();
// Check if we should either use the default entity or use the tree name without the 'Tree' suffix
if (!$annotation->nodeEntity) {
$closureTreeData['nodeEntity'] = substr($className, 0, -4);
} elseif (strpos($annotation->nodeEntity, '\\') === false) {
$closureTreeData['nodeEntity'] = $metadata->namespace . '\\' . $annotation->nodeEntity;
}
// Initialize the depth parameter
$closureTreeData['depth'] = false;
// Loop through the properties and find the parent column
foreach ($metadata->fieldMappings as $propertyName => $propertyData) {
$propertyAnnotations = $this->reader->getPropertyAnnotations(new \ReflectionProperty($className, $propertyName));
// Check if this property has the node parent annotation
foreach ($propertyAnnotations as $propertyAnnotation) {
if ($propertyAnnotation instanceof Ancestor) {
// Set the parent data
$closureTreeData['ancestor'] = $propertyData;
break;
} elseif ($propertyAnnotation instanceof Descendant) {
// Set the parent data
$closureTreeData['descendant'] = $propertyData;
break;
} elseif ($propertyAnnotation instanceof Depth) {
// Set the parent data
$closureTreeData['depth'] = $propertyData;
break;
}
}
}
// Check if the ancestor/descendant associations exists
if (!$metadata->isMappedSuperclass && (!$metadata->hasAssociation('ancestor') || !$metadata->hasAssociation('descendant'))) {
// Get the node metadata
$nodeMetadata = $eventArgs->getEntityManager()->getClassMetadata($closureTreeData['nodeEntity']);
// Get the ID column name
$nodeIdColumn = $nodeMetadata->getSingleIdentifierFieldName();
// Check for the ancestor association
if (!$metadata->hasAssociation('ancestor')) {
$ancestorMapping = array('fieldName' => 'ancestor', 'id' => true, 'joinColumns' => array(array('name' => 'ancestor', 'referencedColumnName' => $nodeIdColumn, 'unique' => false, 'nullable' => false, 'onDelete' => 'CASCADE', 'onUpdate' => null, 'columnDefinition' => null)), 'inversedBy' => null, 'targetEntity' => $nodeMetadata->name, 'cascade' => null, 'fetch' => ClassMetadataInfo::FETCH_LAZY);
// Map the many-to-one association
$metadata->mapManyToOne($ancestorMapping);
// Set the ancestor field mapping
$closureTreeData['ancestor'] = $ancestorMapping;
}
// Check for the descendany association
if (!$metadata->hasAssociation('descendant')) {
$descendantMapping = array('fieldName' => 'descendant', 'id' => true, 'joinColumns' => array(array('name' => 'descendant', 'referencedColumnName' => $nodeIdColumn, 'unique' => false, 'nullable' => false, 'onDelete' => 'CASCADE', 'onUpdate' => null, 'columnDefinition' => null)), 'inversedBy' => null, 'targetEntity' => $nodeMetadata->name, 'cascade' => null, 'fetch' => ClassMetadataInfo::FETCH_LAZY);
// Map the many-to-one association
$metadata->mapManyToOne($descendantMapping);
// Set the ancestor field mapping
$closureTreeData['descendant'] = $descendantMapping;
}
}
// Set the closure tree data
$metadata->closureTree = $closureTreeData;
}