本文整理汇总了PHP中Doctrine\ORM\Mapping\ClassMetadata::getAssociationNames方法的典型用法代码示例。如果您正苦于以下问题:PHP ClassMetadata::getAssociationNames方法的具体用法?PHP ClassMetadata::getAssociationNames怎么用?PHP ClassMetadata::getAssociationNames使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Doctrine\ORM\Mapping\ClassMetadata
的用法示例。
在下文中一共展示了ClassMetadata::getAssociationNames方法的11个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的PHP代码示例。
示例1: guessColumnFormatters
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 ($unique && isset($inserted[$relatedClass])) {
$related = null;
if (isset($inserted[$relatedClass][$index]) || !$optional) {
$related = $inserted[$relatedClass][$index];
}
$index++;
return $related;
} elseif (isset($inserted[$relatedClass])) {
return $inserted[$relatedClass][mt_rand(0, count($inserted[$relatedClass]) - 1)];
}
return null;
};
}
return $formatters;
}
示例2: convertMetadata
/**
* @param ClassMetadata $metadata
* @return JavaScript\Metadata
*/
private function convertMetadata(ClassMetadata $metadata)
{
$meta = new JavaScript\Metadata();
$meta->originalName = $metadata->getName();
$meta->namespace = str_replace('\\', '_', $metadata->getReflectionClass()->getNamespaceName());
$meta->functionName = $metadata->getReflectionClass()->getShortName();
$parent = $metadata->getReflectionClass()->getParentClass();
$meta->superFunctionName = $parent ? $parent->getShortName() : 'DBEntity';
// Convert fields.
foreach ($metadata->getFieldNames() as $fieldName) {
$field = new JavaScript\Field();
$field->name = $fieldName;
$field->methodName = ucfirst($fieldName);
$field->type = $this->convertDoctrineType($metadata->getTypeOfField($fieldName));
$field->isIdentifier = $metadata->isIdentifier($fieldName);
$meta->fields[] = $field;
}
// Convert associations.
foreach ($metadata->getAssociationNames() as $assocName) {
$assoc = new JavaScript\Association();
$assoc->name = $assocName;
$assoc->methodName = ucfirst($assocName);
$assoc->isCollection = $metadata->isCollectionValuedAssociation($assocName);
$assoc->isSingle = $metadata->isSingleValuedAssociation($assocName);
$assoc->singleName = Inflector::singularize($assoc->methodName);
$assoc->invertedField = $metadata->getAssociationMappedByTargetField($assocName);
$targetClass = new \ReflectionClass($metadata->getAssociationTargetClass($assocName));
$assoc->singleType = $targetClass->getShortName();
$assoc->type = $assoc->singleType . ($assoc->isCollection ? '[]' : '');
$meta->associations[] = $assoc;
}
return $meta;
}
示例3: defaultDefsFromMetadata
private function defaultDefsFromMetadata()
{
$defaultEntity = $this->getEntityMetadata()->newInstance();
$allFields = array_merge($this->metadata->getFieldNames(), $this->metadata->getAssociationNames());
foreach ($allFields as $fieldName) {
if (!isset($this->fieldDefs[$fieldName])) {
$defaultFieldValue = $this->metadata->getFieldValue($defaultEntity, $fieldName);
if ($defaultFieldValue !== null) {
$this->fieldDefs[$fieldName] = function () use($defaultFieldValue) {
return $defaultFieldValue;
};
} else {
$this->fieldDefs[$fieldName] = function () {
return null;
};
}
}
}
}
示例4: addExcept
/**
* @param string[]|array list of field and association names
* @param array of pairs [name=>options]. Optional options for non-excluded field/associations
* @return IBuilder[]
*/
public function addExcept($excluded, $options = [])
{
$names = array_merge($this->metadata->getFieldNames(), $this->metadata->getAssociationNames());
$names = array_diff($names, $excluded);
$names = array_filter($names, function ($name) {
return !$this->shouldSkip($name);
});
$builders = [];
foreach ($names as $name) {
$builders[$name] = $this->add($name, isset($options[$name]) ? $options[$name] : []);
}
return $builders;
}
示例5: writeItem
/**
* {@inheritdoc}
*/
public function writeItem(array $item)
{
$this->counter++;
$entity = null;
// If the table was not truncated to begin with, find current entities
// first
if (false === $this->truncate) {
if ($this->index) {
// If the table has a composite key
if (!empty($this->compositeKey) && is_array($this->compositeKey)) {
$composite = '';
foreach ($this->compositeKey as $key => $index) {
$composite .= $item[$index];
}
$value = $composite;
} else {
$value = $item[$this->index];
}
$entity = $this->entityRepository->findOneBy(array($this->index => $value));
} else {
$entity = $this->entityRepository->find(current($item));
}
}
if (!$entity) {
$entity = $this->getNewInstance();
}
$fieldNames = array_merge($this->entityMetadata->getFieldNames(), $this->entityMetadata->getAssociationNames());
foreach ($fieldNames as $fieldName) {
$value = null;
if (isset($item[$fieldName])) {
$value = $item[$fieldName];
} elseif (method_exists($item, 'get' . ucfirst($fieldName))) {
$value = $item->{'get' . ucfirst($fieldName)};
}
if (null === $value) {
continue;
}
if (!$value instanceof \DateTime || $value != $this->entityMetadata->getFieldValue($entity, $fieldName)) {
$setter = 'set' . ucfirst($fieldName);
$this->setValue($entity, $value, $setter);
}
}
$this->entityManager->persist($entity);
if ($this->counter % $this->batchSize == 0) {
$this->entityManager->flush();
$this->entityManager->clear($this->entityName);
}
return $this;
}
示例6: updateEntity
/**
* @param array $item
* @param object $entity
*/
protected function updateEntity(array $item, $entity)
{
$fieldNames = array_merge($this->entityMetadata->getFieldNames(), $this->entityMetadata->getAssociationNames());
foreach ($fieldNames as $fieldName) {
$value = null;
if (isset($item[$fieldName])) {
$value = $item[$fieldName];
} elseif (method_exists($item, 'get' . ucfirst($fieldName))) {
$value = $item->{'get' . ucfirst($fieldName)};
}
if (null === $value) {
continue;
}
if (!$value instanceof \DateTime || $value != $this->entityMetadata->getFieldValue($entity, $fieldName)) {
$setter = 'set' . ucfirst($fieldName);
$this->setValue($entity, $value, $setter);
}
}
}
示例7: getFilteredAssociations
/**
* Get filtered associations
* @param array $fields
* @param ORM\Mapping\ClassMetadata $classMetaData
* @param string $type 'fields' or 'association
* @return array
*/
protected function getFilteredAssociations(array $fields, ORM\Mapping\ClassMetadata $classMetaData, $type = 'fields')
{
// Process relational fields / association with no deeper expose restrictions
if ($type !== 'fields' && $type !== 'associations') {
return [];
}
$names = $type === 'fields' ? $classMetaData->getFieldNames() : $classMetaData->getAssociationNames();
return array_filter($fields, function ($offset) use($names) {
if (!is_array($offset) && in_array($offset, $names)) {
return true;
}
return false;
});
}
示例8: getAssociationNames
/**
* Returns entity association names
*
* @return string[]
*/
public function getAssociationNames()
{
return $this->metadata->getAssociationNames();
}
示例9: 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 = array())
{
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 = array_filter($fields, function ($offset) use($classMetaData) {
if (!is_array($offset) && in_array($offset, $classMetaData->getFieldNames())) {
return true;
}
return false;
});
// 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
$relationalFields = array_filter($fields, function ($offset) use($classMetaData) {
if (!is_array($offset) && in_array($offset, $classMetaData->getAssociationNames())) {
return true;
}
return false;
});
foreach ($relationalFields 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;
}
示例10: addDoctrineAssociations
/**
* @param EntityMetadata $entityMetadata
* @param ClassMetadata $classMetadata
*/
protected function addDoctrineAssociations(EntityMetadata $entityMetadata, ClassMetadata $classMetadata)
{
$associations = array_diff($classMetadata->getAssociationNames(), $this->getDeletedFields($classMetadata->name));
foreach ($associations as $fieldName) {
$fieldMetadata = $this->metadataFactory->createFieldMetadata(['field_name' => $fieldName], $classMetadata->getAssociationMapping($fieldName));
$entityMetadata->addFieldMetadata($fieldMetadata);
}
}
示例11: getIndexedAssociations
/**
* Gets a list of all indexed associations
*
* @param ClassMetadata $metadata
*
* @return array [field name => target field data-type, ...]
*/
public function getIndexedAssociations(ClassMetadata $metadata)
{
$relations = [];
$fieldNames = $metadata->getAssociationNames();
foreach ($fieldNames as $fieldName) {
$mapping = $metadata->getAssociationMapping($fieldName);
if ($mapping['type'] & ClassMetadata::TO_ONE) {
$targetMetadata = $this->getEntityMetadataForClass($mapping['targetEntity']);
$targetIdFieldNames = $targetMetadata->getIdentifierFieldNames();
if (count($targetIdFieldNames) === 1) {
$relations[$fieldName] = $targetMetadata->getTypeOfField(reset($targetIdFieldNames));
}
}
}
return $relations;
}