Skip to content

Commit

Permalink
fixed relationship issues
Browse files Browse the repository at this point in the history
  • Loading branch information
mmadariaga committed Jan 23, 2024
1 parent 8cfb15d commit 84a6d3e
Show file tree
Hide file tree
Showing 4 changed files with 438 additions and 76 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,15 @@
namespace IvozDevTools\EntityGeneratorBundle\Doctrine\Mapping\Generator;

use IvozDevTools\EntityGeneratorBundle\Doctrine\Mapping\MappedEntityRelation;
use IvozDevTools\EntityGeneratorBundle\Doctrine\Mapping\MappingGenerator;
use IvozDevTools\EntityGeneratorBundle\Doctrine\Mapping\RequestedProperty;

class RelationsGenerator
{
public function __construct()
{
public function __construct(
private MappingGenerator $generator,
private string $mappingName
) {
}

/**
Expand All @@ -20,17 +23,21 @@ public function execute(\SimpleXMLElement $xml, array $data): \SimpleXMLElement
foreach ($data as $field) {
$relation = $field->getRelation();

$inverseClass = $relation->getInverseClass();
$inverseClassSegments = explode("\\", $inverseClass);
$className = end($inverseClassSegments);

$inverseRelation = sprintf(
'%s\\%sInterface',
$inverseClass,
$className
);

switch ($relation->getType()) {
case MappedEntityRelation::MANY_TO_ONE:
/** @var \SimpleXMLElement $manyToOne */
$manyToOne = $mappedSuperClass->addChild('many-to-one');

$inversedEntityName = ucfirst($relation->getOwningProperty());
$inverseRelation = sprintf(
'%s\\%sInterface',
$relation->getInverseClass(),
$inversedEntityName
);
$manyToOne->addAttribute('field', $field->getFieldName());
$manyToOne->addAttribute('target-entity', $inverseRelation);
$manyToOne->addAttribute('fetch', $field->getFetch());
Expand All @@ -57,12 +64,6 @@ public function execute(\SimpleXMLElement $xml, array $data): \SimpleXMLElement
/** @var \SimpleXMLElement $oneToMany */
$oneToMany = $mappedSuperClass->addChild('one-to-many');

$inversedEntityName = ucfirst($relation->getOwningProperty());
$inverseRelation = sprintf(
'%s\\%sInterface',
$relation->getInverseClass(),
$inversedEntityName
);
$oneToMany->addAttribute('field', $field->getFieldName());
$oneToMany->addAttribute('target-entity', $inverseRelation);
$oneToMany->addAttribute('fetch', $field->getFetch());
Expand All @@ -89,13 +90,6 @@ public function execute(\SimpleXMLElement $xml, array $data): \SimpleXMLElement
/** @var \SimpleXMLElement $oneToOne */
$oneToOne = $mappedSuperClass->addChild('one-to-one');

$inversedEntityName = ucfirst($relation->getOwningProperty());
$inverseRelation = sprintf(
'%s\\%sInterface',
$relation->getInverseClass(),
$inversedEntityName
);

$oneToOne->addAttribute('field', $field->getFieldName());
$oneToOne->addAttribute('target-entity', $inverseRelation);
$oneToOne->addAttribute('inversed-by', strtolower($relation->getOwningClass()));
Expand Down Expand Up @@ -129,40 +123,51 @@ private function generateInversedRelation(
MappedEntityRelation $relation,
string $fetch
) {
throw new \Exception("TODO");
// $mappingPaths = $this->generator->getMappingsPath();
// $owningClass = $relation->getOwningClass();
// $inverseProperty = $relation->getInverseProperty();
// $owningProperty = $relation->getOwningProperty();
//
// $output = sprintf(
// '%s/%s.%s.orm.xml',
// $mappingPaths[$relation->getInversedProjectName()],
// ucfirst($owningProperty),
// ucfirst($owningProperty)
// );
//
// $xml = $this->generator
// ->readXmlFile($output);
// $aliasMappingPaths = $this->generator->getAliasMappingPaths();
// $targetEntity = sprintf(
// '%s\\%s\\%sInterface',
// $aliasMappingPaths[$this->mappingName],
// $owningClass,
// $owningClass
// );
//
// $entity = $xml->{'entity'};
// $oneToMany = $entity->addChild('one-to-many');
// $oneToMany->addAttribute('field', $inverseProperty);
// $oneToMany->addAttribute('target-entity', $targetEntity);
// $oneToMany->addAttribute('mapped-by', $owningProperty);
// $oneToMany->addAttribute('fetch', $fetch);
//
// $this->generator->generateXml(
// $xml,
// $output
// );
$mappingPaths = $this->generator->getMappingsPath();
$owningClass = $relation->getOwningClass();
$inverseClass = $relation->getInverseClass();
$inverseProperty = $relation->getInverseProperty();
$owningProperty = $relation->getOwningProperty();

$inverseClassSegments = explode(
"\\",
$inverseClass
);

$output = sprintf(
'%s/%s.%s.orm.xml',
$mappingPaths[$relation->getInversedProjectName()],
end($inverseClassSegments),
end($inverseClassSegments),
);

$xml = $this
->generator
->readXmlFile($output);

if (!$xml) {
throw new \Exception("File not found " . $output);
}

$aliasMappingPaths = $this->generator->getAliasMappingPaths();
$targetEntity = sprintf(
'%s\\%s\\%sInterface',
$aliasMappingPaths[$this->mappingName],
$owningClass,
$owningClass
);

$entity = $xml->{'entity'};
$oneToMany = $entity->addChild('one-to-many');
$oneToMany->addAttribute('field', $inverseProperty);
$oneToMany->addAttribute('target-entity', $targetEntity);
$oneToMany->addAttribute('mapped-by', $owningProperty);
$oneToMany->addAttribute('fetch', $fetch);

$this->generator->generateXml(
$xml,
$output
);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -15,22 +15,23 @@

class DumpXmlAttributes
{
private $updateFields;
private $updateIndexes;
private $updateRelations;
private $constraintsGenerator;
private $relationsGenerator;
private $fieldsGenerator;
private UpdateFields $updateFields;
private UpdateIndexes $updateIndexes;
private UpdateRelations $updateRelations;
private ConstraintsGenerator$constraintsGenerator;
private RelationsGenerator $relationsGenerator;
private FieldsGenerator $fieldsGenerator;

public function __construct(
private MappingGenerator $generator,
private MappedPaths $paths
private MappedPaths $paths,
string $mappingName
) {
$this->updateFields = new UpdateFields();
$this->updateIndexes = new UpdateIndexes();
$this->updateRelations = new UpdateRelations();
$this->constraintsGenerator = new ConstraintsGenerator();
$this->relationsGenerator = new RelationsGenerator();
$this->relationsGenerator = new RelationsGenerator($generator, $mappingName);
$this->fieldsGenerator = new FieldsGenerator();
}

Expand Down
Loading

0 comments on commit 84a6d3e

Please sign in to comment.