/** * @param array $configuration * @param ClassFileDefinition $entityFile * @return bool */ protected function entityHasChild(array $configuration, ClassFileDefinition $entityFile) { foreach ($configuration['entities'] as $entityName => $config) { if (true === array_key_exists('parent', $config) && $entityFile->getSourceName() === $config['parent']) { return true; } } return false; }
private function generateRelationAnnotation(ClassFileDefinition $entityFile, PropertyDefinition $property) { $definition = $property->getSourceConfiguration(); $cardinality = $this->getCardinality($this->configuration, $property); switch ($cardinality) { //One to One case Cardinality::CARDINALITY_0_1: case Cardinality::CARDINALITY_1_0: case Cardinality::CARDINALITY_1_1: $annotationName = 'OneToOne'; break; //One To Many //One To Many case Cardinality::CARDINALITY_0_N: case Cardinality::CARDINALITY_1_N: $annotationName = 'OneToMany'; break; //Many To One //Many To One case Cardinality::CARDINALITY_N_0: case Cardinality::CARDINALITY_N_1: $annotationName = 'ManyToOne'; break; //Many to Many //Many to Many case Cardinality::CARDINALITY_N_N: $annotationName = 'ManyToMany'; break; default: throw new \Exception(sprintf('%s is not a valid cardinality', $cardinality)); } $relationConfiguration = ['targetEntity' => '"' . $this->createEntityFqcn($this->configuration, $definition['entity'])->getFullQualifiedClassName() . '"']; $reversePropertyName = $this->getReversePropertyName($this->configuration, $property); if ("ManyToOne" === $annotationName && null !== $reversePropertyName && true === array_key_exists('inverse', $definition)) { throw new \Exception('Doctrine do not accept Inversed relation ManyToOne, Switch Cardinality to OneToMany'); } if ("OneToMany" === $annotationName && null === $reversePropertyName) { throw new \Exception('Doctrine do not accept OneToMany, without reverse side'); } if (null !== $reversePropertyName) { if (true === array_key_exists('inverse', $definition)) { $relationConfiguration['mappedBy'] = '"' . $reversePropertyName->toLowerCamelCase() . '"'; } else { $relationConfiguration['inversedBy'] = '"' . $reversePropertyName->toLowerCamelCase() . '"'; } } $formattedConf = []; foreach ($relationConfiguration as $key => $value) { $formattedConf[] = sprintf('%s=%s', $key, $value); } $property->addAnnotation(sprintf('@ORM\\%s(%s)', $annotationName, implode(', ', $formattedConf))); $inverseJoinColumns = $this->generateJoinColumn($definition['entity'], $property, $cardinality); if ($cardinality == Cardinality::CARDINALITY_N_N) { $entityName = $entityFile->getSourceName(); $joinTableName = new PhpVariableName($entityName . '_' . $definition['entity']); $joinColumns = $this->generateJoinColumn($entityName, $property, $cardinality); $joinTableTemplate = <<<'ANNOT' @ORM\JoinTable(name="%s", * joinColumns={%s}, * inverseJoinColumns={%s} * ) ANNOT; $property->addAnnotation(sprintf($joinTableTemplate, $joinTableName->toLowerSnakeCase(), implode(', ', $joinColumns), implode(', ', $inverseJoinColumns))); } else { if (1 === count($inverseJoinColumns)) { $property->addAnnotation(reset($inverseJoinColumns)); } elseif (1 < count($inverseJoinColumns)) { $property->addAnnotation(sprintf('@ORM\\JoinColumns({%s})', implode(', ', $inverseJoinColumns))); } } }