本文整理汇总了PHP中Doctrine\ORM\Mapping\ClassMetadata::getAssociationMappings方法的典型用法代码示例。如果您正苦于以下问题:PHP ClassMetadata::getAssociationMappings方法的具体用法?PHP ClassMetadata::getAssociationMappings怎么用?PHP ClassMetadata::getAssociationMappings使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Doctrine\ORM\Mapping\ClassMetadata
的用法示例。
在下文中一共展示了ClassMetadata::getAssociationMappings方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的PHP代码示例。
示例1: createAssociationFormatters
public function createAssociationFormatters($columnTypeGuesser)
{
$formatters = array();
// $getAssociationNames = $this->class->getAssociationNames();
// foreach ($getAssociationNames AS $assocName) {
// $relatedClass = $this->class->getAssociationTargetClass($assocName);
$associationMappings = $this->class->getAssociationMappings();
foreach ($associationMappings as $associationMapping) {
if ($associationType = $this->getAssociationType($associationMapping)) {
//echo "pola asocjacyjne: " . $associationMapping['fieldName'] . "\n";
$formatter = $columnTypeGuesser->getAssociation($associationMapping['fieldName'], $associationMapping['targetEntity'], $associationType, $this->manager);
if ($formatter) {
$formatters[$associationMapping['fieldName']] = $formatter;
}
}
}
$index = 0;
/* $formatters[$assocName] = function($inserted) use ($relatedClass, &$index, $relation) {
if ($unique && isset($inserted[$relatedClass])) {
return $inserted[$relatedClass][$index++];
} elseif (isset($inserted[$relatedClass])) {
return $inserted[$relatedClass][mt_rand(0, count($inserted[$relatedClass]) - 1)];
}
return null;
}; */
//}
return $formatters;
}
示例2: addDoctrineAssociations
/**
* @param EntityMetadata $entityMetadata
* @param ClassMetadata $classMetadata
*/
protected function addDoctrineAssociations(EntityMetadata $entityMetadata, ClassMetadata $classMetadata)
{
foreach ($classMetadata->getAssociationMappings() as $fieldName => $associationMapping) {
$fieldMetadata = $this->metadataFactory->createFieldMetadata(array('field_name' => $fieldName), $associationMapping);
$entityMetadata->addFieldMetadata($fieldMetadata);
}
}
示例3: processNode
/**
* Process the node metadata.
*
* @param LoadClassMetadataEventArgs $eventArgs
* @param ClassMetadata $metadata
* @param Node $annotation
*/
protected function processNode(LoadClassMetadataEventArgs $eventArgs, ClassMetadata $metadata, Node $annotation)
{
$className = $metadata->name;
// Holds the closure tree data
$closureTreeData = array();
// Check if we should either use the default entity or prepend the node namespace
if (!$annotation->treeEntity) {
$closureTreeData['treeEntity'] = $className . 'Tree';
} elseif (strpos($annotation->treeEntity, '\\') === false) {
$closureTreeData['treeEntity'] = $metadata->namespace . '\\' . $annotation->treeEntity;
}
// Loop through the properties and find the parent column
foreach ($metadata->getAssociationMappings() 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 NodeParent) {
// Set the parent data
$closureTreeData['parent'] = $propertyData;
break 2;
}
}
}
// Make sure a parent was specified
if (!isset($closureTreeData['parent'])) {
throw new \Exception('You must define a parent column for the closure tree entity ' . $className . '.');
}
// Set the closure tree data
$metadata->closureTree = $closureTreeData;
}
示例4: guessColumnFormatters
/**
* @param \Faker\Generator $generator
* @return array
*/
public function guessColumnFormatters(\Faker\Generator $generator)
{
$formatters = array();
$nameGuesser = new \Faker\Guesser\Name($generator);
$columnTypeGuesser = new ColumnTypeGuesser($generator);
foreach ($this->class->getFieldNames() as $fieldName) {
if ($this->class->isIdentifier($fieldName) || !$this->class->hasField($fieldName)) {
continue;
}
$size = isset($this->class->fieldMappings[$fieldName]['length']) ? $this->class->fieldMappings[$fieldName]['length'] : null;
if ($formatter = $nameGuesser->guessFormat($fieldName, $size)) {
$formatters[$fieldName] = $formatter;
continue;
}
if ($formatter = $columnTypeGuesser->guessFormat($fieldName, $this->class)) {
$formatters[$fieldName] = $formatter;
continue;
}
}
foreach ($this->class->getAssociationNames() as $assocName) {
if ($this->class->isCollectionValuedAssociation($assocName)) {
continue;
}
$relatedClass = $this->class->getAssociationTargetClass($assocName);
$unique = $optional = false;
$mappings = $this->class->getAssociationMappings();
foreach ($mappings as $mapping) {
if ($mapping['targetEntity'] == $relatedClass) {
if ($mapping['type'] == ClassMetadata::ONE_TO_ONE) {
$unique = true;
$optional = isset($mapping['joinColumns'][0]['nullable']) ? $mapping['joinColumns'][0]['nullable'] : false;
break;
}
}
}
$index = 0;
$formatters[$assocName] = function ($inserted) use($relatedClass, &$index, $unique, $optional) {
if (isset($inserted[$relatedClass])) {
if ($unique) {
$related = null;
if (isset($inserted[$relatedClass][$index]) || !$optional) {
$related = $inserted[$relatedClass][$index];
}
$index++;
return $related;
}
return $inserted[$relatedClass][mt_rand(0, count($inserted[$relatedClass]) - 1)];
}
return null;
};
}
return $formatters;
}
示例5: getDatabaseAssociationMappings
/**
* Returns association mapping for a given entity.
*
* @param ClassMetadata $cm
* @param bool|false $bTree
*
* @return array
*/
protected function getDatabaseAssociationMappings(ClassMetadata $cm, $bTree = false)
{
$associations = $cm->getAssociationMappings();
$byReferenceMappings = $this->getByReferenceMappings($cm);
$associationMappings = [];
foreach ($associations as $association) {
$getterPlural = false;
$associationType = $association['type'];
switch ($association['type']) {
case ClassMetadataInfo::MANY_TO_MANY:
$associationType = 'MANY_TO_MANY';
$getterPlural = true;
break;
case ClassMetadataInfo::MANY_TO_ONE:
$associationType = 'MANY_TO_ONE';
$getterPlural = false;
break;
case ClassMetadataInfo::ONE_TO_MANY:
$associationType = 'ONE_TO_MANY';
$getterPlural = true;
break;
case ClassMetadataInfo::ONE_TO_ONE:
$associationType = 'ONE_TO_ONE';
$getterPlural = false;
break;
}
$getter = 'get' . ucfirst($association['fieldName']);
$getterField = lcfirst($cm->getReflectionClass()->getShortName()) . str_replace('.', '', $this->convertPHPToExtJSClassName($association['targetEntity']));
if ($getterPlural) {
$getterField .= 's';
}
$propertyAnnotations = $this->reader->getPropertyAnnotations($cm->getReflectionProperty($association['fieldName']));
$nullable = true;
foreach ($propertyAnnotations as $propertyAnnotation) {
$filter = "Symfony\\Component\\Validator\\Constraints\\NotNull";
if (substr(get_class($propertyAnnotation), 0, strlen($filter)) === $filter) {
$nullable = false;
}
}
// The self-referencing association may not be written for trees, because ExtJS can't load all nodes
// in one go.
if (!($bTree && $association['targetEntity'] == $cm->getName())) {
$byReference = false;
if (in_array($association['fieldName'], $byReferenceMappings)) {
$byReference = true;
}
$associationMappings[$associationType][] = ['name' => $association['fieldName'], 'nullable' => $nullable, 'target' => $this->convertPHPToExtJSClassName($association['targetEntity']), 'byReference' => $byReference, 'getter' => $getter, 'getterField' => $getterField];
}
}
return $associationMappings;
}
示例6: loadAssociationObjectsToEntity
/**
* Add the associated objects in case the item have for persist its relation
*
* @param array $item
* @param object $entity
*/
protected function loadAssociationObjectsToEntity(array $item, $entity)
{
foreach ($this->entityMetadata->getAssociationMappings() as $associationMapping) {
$value = null;
if (isset($item[$associationMapping['fieldName']]) && !is_object($item[$associationMapping['fieldName']])) {
$value = $this->entityManager->getReference($associationMapping['targetEntity'], $item[$associationMapping['fieldName']]);
}
if (null === $value) {
continue;
}
$setter = 'set' . ucfirst($associationMapping['fieldName']);
$this->setValue($entity, $value, $setter);
}
}
示例7: processRelation
/**
* @param ClassMetadata $metadata
* @param object $relation
* @param mixed $value
*/
protected function processRelation($metadata, $relation, $value)
{
foreach ($metadata->getAssociationMappings() as $mapping) {
if (!is_array($mapping) || !isset($mapping['targetEntity'], $mapping['type'], $mapping['mappedBy'])) {
continue;
}
if ($mapping['targetEntity'] !== ClassUtils::getClass($relation)) {
continue;
}
if ($mapping['type'] !== ClassMetadata::ONE_TO_MANY) {
continue;
}
$mappedBy = $mapping['mappedBy'];
$setter = $this->getSetterName($mappedBy);
$relation->{$setter}($value);
}
}
示例8: getAssociations
/**
* {@inheritdoc}
*/
protected function getAssociations(array $definition, ClassMetadata $metadata, $version, array $requestType, array $extras)
{
$associations = $metadata->getAssociationMappings();
foreach ($associations as $fieldName => $mapping) {
if (!$this->isAssociationCompletionRequired($fieldName, $definition)) {
continue;
}
$config = $this->relationConfigProvider->getRelationConfig($mapping['targetEntity'], $version, $requestType, $extras);
if (isset($definition[$fieldName]) && is_array($definition[$fieldName])) {
$config = array_merge_recursive($config, [ConfigUtil::DEFINITION => $definition[$fieldName]]);
}
if ($this->exclusionProvider->isIgnoredRelation($metadata, $fieldName)) {
$config[ConfigUtil::DEFINITION][ConfigUtil::EXCLUDE] = true;
}
$definition[$fieldName] = $config;
}
return $definition;
}
示例9: getDatabaseAssociationMappings
/**
* Returns association mapping for a given entity
*
* @param ClassMetadata $cm
* @param bool|false $bTree
*
* @return array
*/
protected function getDatabaseAssociationMappings(ClassMetadata $cm, $bTree = false)
{
$associations = $cm->getAssociationMappings();
$associationMappings = array();
foreach ($associations as $association) {
$getterPlural = false;
$associationType = $association["type"];
switch ($association["type"]) {
case ClassMetadataInfo::MANY_TO_MANY:
$associationType = "MANY_TO_MANY";
$getterPlural = true;
break;
case ClassMetadataInfo::MANY_TO_ONE:
$associationType = "MANY_TO_ONE";
$getterPlural = false;
break;
case ClassMetadataInfo::ONE_TO_MANY:
$associationType = "ONE_TO_MANY";
$getterPlural = true;
break;
case ClassMetadataInfo::ONE_TO_ONE:
$associationType = "ONE_TO_ONE";
$getterPlural = false;
break;
}
$getter = "get" . ucfirst($association["fieldName"]);
$getterField = lcfirst($cm->getReflectionClass()->getShortName()) . str_replace(".", "", $this->convertPHPToExtJSClassName($association["targetEntity"]));
if ($getterPlural) {
$getterField .= "s";
}
// The self-referencing association may not be written for trees, because ExtJS can't load all nodes
// in one go.
if (!($bTree && $association["targetEntity"] == $cm->getName())) {
$associationMappings[$associationType][] = array("name" => $association["fieldName"], "target" => $this->convertPHPToExtJSClassName($association["targetEntity"]), "getter" => $getter, "getterField" => $getterField);
}
}
return $associationMappings;
}
示例10: collectNestedDataTags
/**
* @param mixed $data
* @param ClassMetadata $metadata
*
* @return array
*/
protected function collectNestedDataTags($data, ClassMetadata $metadata)
{
$tags = [];
foreach ($metadata->getAssociationMappings() as $fieldName => $mapping) {
$value = $metadata->reflFields[$fieldName]->getValue($data);
if (null !== $value) {
// skip DoctrineTagGenerator#supports() call due to doctrine association mapping always contain entities
$unwrappedValue = $mapping['type'] & ClassMetadata::TO_ONE ? [$value] : $value->unwrap();
foreach ($unwrappedValue as $entity) {
// allowed one nested level
$tags = array_merge($tags, $this->generate($entity));
}
}
}
return $tags;
}
示例11: registerExpose
/**
* A recursive function to process the specified expose fields for a fetch request (GET)
* @param array $fields - expose fields to process
* @param ORM\QueryBuilder $qb
* @param ORM\Mapping\ClassMetadata $classMetaData
* @param $rootAlias - table alias to be used on SQL query
* @param array $addedKeyFields
* @return ORM\QueryBuilder
*/
protected function registerExpose($fields, ORM\QueryBuilder $qb, ORM\Mapping\ClassMetadata $classMetaData, $rootAlias = null, &$addedKeyFields = [])
{
if (empty($fields)) {
return $qb;
}
$rootAlias = is_null($rootAlias) ? self::getAlias($classMetaData->getName()) : $rootAlias;
$addedKeyFields = (array) $addedKeyFields;
$ormAssociationMappings = $classMetaData->getAssociationMappings();
// Process single fields into a partial set - Filter fields not available on class meta data
$selectFields = $this->getFilteredAssociations($fields, $classMetaData, 'fields');
// merge required identifier fields with select fields
$keyFieldDiff = array_diff($classMetaData->getIdentifierFieldNames(), $selectFields);
if (!empty($keyFieldDiff)) {
$addedKeyFields = $keyFieldDiff;
$selectFields = array_merge($selectFields, $keyFieldDiff);
}
if (!empty($selectFields)) {
$qb->addSelect('partial ' . $rootAlias . '.{' . implode(', ', $selectFields) . '}');
}
// Process relational field with no deeper expose restrictions
foreach ($this->getFilteredAssociations($fields, $classMetaData, 'association') as $relationalField) {
$alias = self::getAlias($ormAssociationMappings[$relationalField]['targetEntity'], $relationalField);
$qb->leftJoin($rootAlias . '.' . $relationalField, $alias);
$qb->addSelect($alias);
}
foreach ($fields as $key => $value) {
if (is_array($value) && isset($ormAssociationMappings[$key])) {
$alias = self::getAlias($ormAssociationMappings[$key]['targetEntity'], $key);
$qb->leftJoin($rootAlias . '.' . $key, $alias);
$qb = $this->registerExpose($value, $qb, $this->getEntityManager()->getClassMetadata($ormAssociationMappings[$key]['targetEntity']), $alias, $addedKeyFields[$key]);
}
}
$this->addedKeyFields = $addedKeyFields;
return $qb;
}
示例12: getAssociationFields
/**
* @param ClassMetadata $classMetadata
* @return ForestField[]
*/
public function getAssociationFields(ClassMetadata $classMetadata)
{
$fields = array();
if (count($classMetadata->getAssociationMappings())) {
foreach ($classMetadata->getAssociationMappings() as $associationMapping) {
$association = $this->getFieldForAssociation($associationMapping);
$fields = array_merge($fields, $association);
}
}
return $fields;
}
示例13: getAssociations
/**
* @param array $definition
* @param ClassMetadata $metadata
* @param string $version
* @param string[] $requestType
* @param array $extras
*
* @return array
*/
protected function getAssociations(array $definition, ClassMetadata $metadata, $version, array $requestType, array $extras)
{
$associations = $metadata->getAssociationMappings();
foreach ($associations as $fieldName => $mapping) {
if (!$this->isAssociationCompletionRequired($fieldName, $definition)) {
continue;
}
$identifierFieldNames = $this->doctrineHelper->getEntityIdentifierFieldNamesForClass($mapping['targetEntity']);
$config = [ConfigUtil::DEFINITION => [ConfigUtil::EXCLUSION_POLICY => ConfigUtil::EXCLUSION_POLICY_ALL, ConfigUtil::FIELDS => count($identifierFieldNames) === 1 ? reset($identifierFieldNames) : array_fill_keys($identifierFieldNames, null)]];
if (isset($definition[$fieldName]) && is_array($definition[$fieldName])) {
$config = array_merge_recursive($config, [ConfigUtil::DEFINITION => $definition[$fieldName]]);
}
if ($this->exclusionProvider->isIgnoredRelation($metadata, $fieldName)) {
$config[ConfigUtil::DEFINITION][ConfigUtil::EXCLUDE] = true;
}
$definition[$fieldName] = $config;
}
return $definition;
}
示例14: createProxyFile
private function createProxyFile(ClassMetadata $classMetadata)
{
$fileName = str_replace('\\', '_', $classMetadata->getName());
$reflection = $classMetadata->getReflectionClass();
$filecontent = $this->getFileContent($classMetadata->getName());
foreach ($reflection->getProperties() as $property) {
/* @var $property \ReflectionProperty */
$annotation = $this->getPropertyAnnotation($property);
if (!is_null($annotation)) {
$filecontent .= ' $obj->' . $property->getName() . ' = ' . $annotation->getFixture() . "\n";
}
}
foreach ($classMetadata->getAssociationMappings() as $association) {
$filecontent .= ' $obj->' . $association['fieldName'] . ' = array_key_exists("' . $association['fieldName'] . '", $dpdc) ? $dpdc["' . $association['fieldName'] . '"] : null;' . "\n";
}
$filecontent .= ' return $obj;' . "\n" . ' }' . "\n" . '}';
file_put_contents($this->_cacheDir . DIRECTORY_SEPARATOR . $fileName, $filecontent);
}
示例15: buildSelectForUpdateQuery
/**
* @param EntityRepository $repository
* @param ORMMetadata $class
* @return \Doctrine\ORM\QueryBuilder
*/
protected function buildSelectForUpdateQuery(EntityRepository $repository, ORMMetadata $class)
{
$qb = $repository->createQueryBuilder('e', 'e.id');
$i = 0;
foreach ($class->getAssociationMappings() as $assocMapping) {
if (!$class->isSingleValuedAssociation($assocMapping['fieldName'])) {
continue;
}
$targetClass = $this->entityManager->getClassMetadata($assocMapping['targetEntity']);
$alias = substr($assocMapping['fieldName'], 0, 1) . $i++;
$qb->leftJoin('e.' . $assocMapping['fieldName'], $alias)->addSelect($alias);
// todo: deeper!
}
return $qb;
}