Skip to content

Commit

Permalink
Merge pull request #6 from phcorp/master
Browse files Browse the repository at this point in the history
better cardinality display
  • Loading branch information
phcorp authored Mar 20, 2017
2 parents 96a6096 + 8513eb4 commit 217bc38
Show file tree
Hide file tree
Showing 6 changed files with 38 additions and 28 deletions.
Binary file modified example.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
38 changes: 17 additions & 21 deletions src/Command/DoctrinevizCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -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)) {
Expand All @@ -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;
Expand All @@ -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' : '*');
}
}
}
}
Expand All @@ -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.
*
Expand Down
10 changes: 8 additions & 2 deletions src/Graphviz/Edgeable.php
Original file line number Diff line number Diff line change
Expand Up @@ -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));
}

/**
Expand All @@ -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));
}

/**
Expand Down
4 changes: 4 additions & 0 deletions src/Graphviz/Graph.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

/**
Expand Down
12 changes: 8 additions & 4 deletions test/Command/DoctrinevizCommandTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -77,16 +77,20 @@ public function testExecuteDotFormatting()
' label="USER_GROUP|<group_id> group_id : integer\\l|<user_id> 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();
Expand Down
2 changes: 1 addition & 1 deletion test/Entity/User.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down

0 comments on commit 217bc38

Please sign in to comment.