diff --git a/example.png b/example.png index 770c24c..0374bf5 100644 Binary files a/example.png and b/example.png differ diff --git a/src/Command/DoctrinevizCommand.php b/src/Command/DoctrinevizCommand.php index 91cad2e..c13466f 100644 --- a/src/Command/DoctrinevizCommand.php +++ b/src/Command/DoctrinevizCommand.php @@ -96,15 +96,19 @@ public function execute(InputInterface $input, OutputInterface $output) foreach ($joinColumns as $joinColumn) { $record = new Record($this->getFieldMappingDisplayName($joinColumn, 'name')); $table->addRecord($record); + $nullable = false; if (array_key_exists($sourceEntity, $tables)) { $name = $this->getFieldMappingDisplayName($joinColumn, 'referencedColumnName'); + $nullable = $joinColumn['nullable']; $to = $graph->getVertex($tables[$sourceEntity]->getId())->getRecord($name); if (!$to) { $to = new Record($name); $tables[$sourceEntity]->addRecord($to); } } - $record->addEdgeTo($to, '* 1'); + $edge = $record->addEdgeTo($to); + $edge->createAttribute('headlabel', $nullable ? '0..1' : '1'); + $edge->createAttribute('taillabel', '*'); } } if (array_key_exists('inverseJoinColumns', $joinTable)) { @@ -113,15 +117,19 @@ public function execute(InputInterface $input, OutputInterface $output) foreach ($inverseJoinColumns as $inverseJoinColumn) { $record = new Record($this->getFieldMappingDisplayName($inverseJoinColumn, 'name')); $table->addRecord($record); + $nullable = false; if (array_key_exists($targetEntity, $tables)) { $name = $this->getFieldMappingDisplayName($inverseJoinColumn, 'referencedColumnName'); + $nullable = $inverseJoinColumn['nullable']; $to = $graph->getVertex($tables[$targetEntity]->getId())->getRecord($name); if (!$to) { $to = new Record($name); $tables[$targetEntity]->addRecord($to); } } - $record->addEdgeTo($to, '* 1'); + $edge = $record->addEdgeTo($to); + $edge->createAttribute('headlabel', $nullable ? '0..1' : '1'); + $edge->createAttribute('taillabel', '*'); } } $tables[$table->getId()] = $table; @@ -145,7 +153,13 @@ public function execute(InputInterface $input, OutputInterface $output) ])); $tables[$entity]->addRecord($from); } - $from->addEdgeTo($to, $this->getCardinality($associationMapping)); + $joinColumn = $associationMapping['joinColumns'][0]; + $nullable = !array_key_exists('nullable', $joinColumn) || $joinColumn['nullable']; + $edge = $from->addEdgeTo($to); + $edge->createAttribute('headlabel', $nullable ? '0..1' : '1'); + if (array_key_exists('type', $associationMapping)) { + $edge->createAttribute('taillabel', ClassMetadataInfo::ONE_TO_ONE === $associationMapping['type'] ? '1' : '*'); + } } } } @@ -167,24 +181,6 @@ public function execute(InputInterface $input, OutputInterface $output) } } - protected function getCardinality(array $fieldMapping) - { - if (!array_key_exists('type', $fieldMapping)) { - return null; - } - - switch ($fieldMapping['type']) { - case ClassMetadataInfo::ONE_TO_ONE: - return '1 1'; - case ClassMetadataInfo::ONE_TO_MANY: - return '1 *'; - case ClassMetadataInfo::MANY_TO_ONE: - return '* 1'; - case ClassMetadataInfo::MANY_TO_MANY: - return '* *'; - } - } - /** * Get field mapping display name. * diff --git a/src/Graphviz/Edgeable.php b/src/Graphviz/Edgeable.php index c62c21f..e246feb 100644 --- a/src/Graphviz/Edgeable.php +++ b/src/Graphviz/Edgeable.php @@ -45,13 +45,16 @@ public function setGraph($graph) * * @param Vertex|Record $element * @param null|string $label + * + * @return Edge */ public function addEdgeTo($element, $label = null) { if (!$this->graph) { throw new \RuntimeException('Graph is not defined'); } - $this->graph->addEdge(new Edge($this, $element, $label)); + + return $this->graph->addEdge(new Edge($this, $element, $label)); } /** @@ -73,13 +76,16 @@ public function removeEdgeTo($element, $label = null) * * @param Vertex|Record $element * @param null|string $label + * + * @return Edge */ public function addEdgeFrom($element, $label = null) { if (!$this->graph) { throw new \RuntimeException('Graph is not defined'); } - $this->graph->addEdge(new Edge($element, $this, $label)); + + return $this->graph->addEdge(new Edge($element, $this, $label)); } /** diff --git a/src/Graphviz/Graph.php b/src/Graphviz/Graph.php index 90f8347..3c84cac 100644 --- a/src/Graphviz/Graph.php +++ b/src/Graphviz/Graph.php @@ -152,10 +152,14 @@ public function getEdge($id) * Add edge. * * @param Edge $edge + * + * @return Edge */ public function addEdge(Edge $edge) { $this->edges[(string) $edge] = $edge; + + return $edge; } /** diff --git a/test/Command/DoctrinevizCommandTest.php b/test/Command/DoctrinevizCommandTest.php index 547216d..e58880c 100644 --- a/test/Command/DoctrinevizCommandTest.php +++ b/test/Command/DoctrinevizCommandTest.php @@ -77,16 +77,20 @@ public function testExecuteDotFormatting() ' label="USER_GROUP| group_id : integer\\l| user_id : integer\\l"'.PHP_EOL. ' ]'.PHP_EOL. ' user:address_id : integer -> address:id : integer ['.PHP_EOL. - ' label="* 1"'.PHP_EOL. + ' headlabel="0..1"'.PHP_EOL. + ' taillabel="*"'.PHP_EOL. ' ];'.PHP_EOL. ' user:email_id : integer -> email:id : integer ['.PHP_EOL. - ' label="1 1"'.PHP_EOL. + ' headlabel="1"'.PHP_EOL. + ' taillabel="1"'.PHP_EOL. ' ];'.PHP_EOL. ' user_group:group_id : integer -> group:id : integer ['.PHP_EOL. - ' label="* 1"'.PHP_EOL. + ' headlabel="0..1"'.PHP_EOL. + ' taillabel="*"'.PHP_EOL. ' ];'.PHP_EOL. ' user_group:user_id : integer -> user:id : integer ['.PHP_EOL. - ' label="* 1"'.PHP_EOL. + ' headlabel="0..1"'.PHP_EOL. + ' taillabel="*"'.PHP_EOL. ' ];'.PHP_EOL. '}'.PHP_EOL; $client = static::createClient(); diff --git a/test/Entity/User.php b/test/Entity/User.php index 9dd3416..81abaad 100644 --- a/test/Entity/User.php +++ b/test/Entity/User.php @@ -47,7 +47,7 @@ class User /** * @ORM\OneToOne(targetEntity="Email") - * @ORM\JoinColumn(name="email_id", referencedColumnName="id") + * @ORM\JoinColumn(name="email_id", nullable=false, referencedColumnName="id") */ protected $email;