本文整理汇总了PHP中Doctrine\ORM\Mapping\ClassMetadata::isCollectionValuedAssociation方法的典型用法代码示例。如果您正苦于以下问题:PHP ClassMetadata::isCollectionValuedAssociation方法的具体用法?PHP ClassMetadata::isCollectionValuedAssociation怎么用?PHP ClassMetadata::isCollectionValuedAssociation使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Doctrine\ORM\Mapping\ClassMetadata
的用法示例。
在下文中一共展示了ClassMetadata::isCollectionValuedAssociation方法的8个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的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: setFieldLimit
/**
* @param array $fieldConfig
* @param ClassMetadata $metadata
* @param string $fieldName
* @param int $limit
*/
protected function setFieldLimit(array &$fieldConfig, ClassMetadata $metadata, $fieldName, $limit)
{
if ($metadata->hasAssociation($fieldName)) {
if (!array_key_exists(ConfigUtil::MAX_RESULTS, $fieldConfig) && $metadata->isCollectionValuedAssociation($fieldName)) {
$fieldConfig[ConfigUtil::MAX_RESULTS] = $limit;
}
$this->setLimits($fieldConfig, $metadata->name, $limit);
}
}
示例3: createAssociationMetadata
/**
* @param ClassMetadata $classMetadata
* @param string $associationName
*
* @return AssociationMetadata
*/
public function createAssociationMetadata(ClassMetadata $classMetadata, $associationName)
{
$targetClass = $classMetadata->getAssociationTargetClass($associationName);
$associationMetadata = new AssociationMetadata();
$associationMetadata->setName($associationName);
$associationMetadata->setTargetClassName($targetClass);
$associationMetadata->setIsCollection($classMetadata->isCollectionValuedAssociation($associationName));
$targetMetadata = $this->doctrineHelper->getEntityMetadataForClass($targetClass);
$targetIdFields = $targetMetadata->getIdentifierFieldNames();
if (count($targetIdFields) === 1) {
$associationMetadata->setDataType($targetMetadata->getTypeOfField(reset($targetIdFields)));
} else {
$associationMetadata->setDataType(DataType::STRING);
}
return $associationMetadata;
}
示例4: extendLoadMetadataForClass
/**
* Merge DoctrineClassMetadata and DataAuditClassMetadata
* @param DoctrineClassMetadata $doctrineClassMetadata
* @return null|ClassMetadata
* @throws \InvalidArgumentException
*/
public function extendLoadMetadataForClass(DoctrineClassMetadata $doctrineClassMetadata)
{
if ($doctrineClassMetadata->isMappedSuperclass || !($classMetadata = $this->loadMetadataForClass($doctrineClassMetadata->getReflectionClass()))) {
return null;
}
/** @var $property PropertyMetadata */
foreach ($classMetadata->propertyMetadata as $name => $property) {
if ($doctrineClassMetadata->isInheritedField($name) || isset($doctrineClassMetadata->associationMappings[$property->name]['inherited'])) {
unset($classMetadata->propertyMetadata[$name]);
continue;
}
if ($doctrineClassMetadata->isCollectionValuedAssociation($name)) {
$property->isCollection = true;
$targetMapping = $doctrineClassMetadata->getAssociationMapping($name);
if (!method_exists($targetMapping['targetEntity'], $property->method)) {
throw new \InvalidArgumentException(sprintf("Method %s in Class %s is not defined. Class must implement " . "a method '__toString' or configure getMethod with Versioned annotation", $property->method, $targetMapping['targetEntity']));
}
}
}
return $classMetadata;
}
示例5: createAssociationMetadata
/**
* @param ClassMetadata $classMetadata
* @param string $associationName
*
* @return AssociationMetadata
*/
public function createAssociationMetadata(ClassMetadata $classMetadata, $associationName)
{
$targetClass = $classMetadata->getAssociationTargetClass($associationName);
$associationMetadata = new AssociationMetadata();
$associationMetadata->setName($associationName);
$associationMetadata->setTargetClassName($targetClass);
$associationMetadata->setIsCollection($classMetadata->isCollectionValuedAssociation($associationName));
$targetMetadata = $this->doctrineHelper->getEntityMetadataForClass($targetClass);
$targetIdFields = $targetMetadata->getIdentifierFieldNames();
if (count($targetIdFields) === 1) {
$associationMetadata->setDataType($targetMetadata->getTypeOfField(reset($targetIdFields)));
} else {
$associationMetadata->setDataType(DataType::STRING);
}
if ($targetMetadata->inheritanceType !== ClassMetadata::INHERITANCE_TYPE_NONE) {
$associationMetadata->setAcceptableTargetClassNames($targetMetadata->subClasses);
} else {
$associationMetadata->addAcceptableTargetClassName($targetClass);
}
return $associationMetadata;
}
示例6: createAssociationField
/**
* @param FormBuilder $form
* @param ClassMetadata $metaData
* @param string $fieldName
*/
public function createAssociationField($form, $metaData, $fieldName)
{
$formField = new SelectField(array());
$formField->setLabel($this->translateName($fieldName));
$formField->setName($fieldName);
$mapping = $metaData->getAssociationMapping($fieldName);
$required = false;
$targetEntityName = $metaData->getAssociationTargetClass($fieldName);
$targetEntity = $this->doctrineService->getRepository($targetEntityName);
if ($metaData->isCollectionValuedAssociation($fieldName)) {
$formField->setMultiple(true);
$formField->setRequired(false);
} else {
foreach ($mapping['joinColumns'] as $joinColumn) {
if (!$joinColumn['nullable']) {
$required = true;
break;
}
}
$formField->setRequired($required);
}
$collection = static::entityToCollection($targetEntity->findAll(), !$formField->isMultiple());
$formField->setCollection($collection);
$form->addField($formField);
}
示例7: 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;
}
示例8: save
/**
* @param \Doctrine\ORM\Mapping\ClassMetadata $meta
* @param \Nette\ComponentModel\Component $component
* @param mixed $entity
* @return boolean
*/
public function save(ClassMetadata $meta, Component $component, $entity)
{
if (!$component instanceof BaseControl) {
return false;
}
$name = $component->getOption(self::FIELD_NAME, $component->getName());
$value = $component->getValue();
if ($this->accessor->isWritable($entity, $name) && !$meta->hasAssociation($name)) {
try {
$this->accessor->setValue($entity, $name, $value);
return true;
} catch (\Kdyby\Doctrine\MemberAccessException $e) {
}
}
if (!$meta->hasAssociation($name)) {
return false;
}
$value = $component->getValue();
$entityClass = $this->relatedMetadata($entity, $name)->getName();
$repository = $this->entityManager->getRepository($entityClass);
if ($meta->isCollectionValuedAssociation($name)) {
$property = \Doctrine\Common\Util\Inflector::singularize($name);
foreach ($repository->findAll() as $associatedEntity) {
if (in_array($associatedEntity->id, $value)) {
$hasMethod = 'has' . ucfirst($property);
if (!$entity->{$hasMethod}($associatedEntity)) {
$addMethod = 'add' . ucfirst($property);
$entity->{$addMethod}($associatedEntity);
}
} else {
$removeMethod = 'remove' . ucfirst($property);
$entity->{$removeMethod}($associatedEntity);
}
}
} elseif ($value === null || ($value = $repository->find($value))) {
if ($this->accessor->isWritable($entity, $name)) {
try {
$this->accessor->setValue($entity, $name, $value);
} catch (\Kdyby\Doctrine\MemberAccessException $e) {
return false;
}
}
}
return true;
}