PartKeepr\DoctrineReflectionBundle\Services\ReflectionService::getDatabaseAssociationMappings PHP Method

getDatabaseAssociationMappings() protected method

Returns association mapping for a given entity.
protected getDatabaseAssociationMappings ( Doctrine\ORM\Mapping\ClassMetadata $cm, boolean | false $bTree = false ) : array
$cm Doctrine\ORM\Mapping\ClassMetadata
$bTree boolean | false
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;
    }