/** * @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; }
/** * @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; }
/** * Adds metadata of an association. * * @param AssociationMetadata $association */ public function addAssociation(AssociationMetadata $association) { $this->associations[$association->getName()] = $association; }
/** * @param mixed $object * @param AssociationMetadata $associationMetadata * * @return array The resource identifier */ protected function processRelatedObject($object, AssociationMetadata $associationMetadata) { $targetMetadata = $associationMetadata->getTargetMetadata(); $preparedValue = $this->prepareRelatedValue($object, $associationMetadata->getTargetClassName(), $targetMetadata); if ($preparedValue['idOnly']) { $resourceId = $this->getResourceIdObject($preparedValue['entityType'], $this->entityIdTransformer->transform($preparedValue['value'])); } else { $resourceId = $this->getResourceIdObject($preparedValue['entityType'], $this->entityIdAccessor->getEntityId($preparedValue['value'], $targetMetadata)); $this->addRelatedObject($preparedValue['value'], $targetMetadata); } return $resourceId; }