Skip to content

Commit

Permalink
[2.0] Finishing the AnnotationExporter to export relationships properly
Browse files Browse the repository at this point in the history
  • Loading branch information
jwage committed Oct 8, 2009
1 parent aba096c commit 204b6d7
Show file tree
Hide file tree
Showing 5 changed files with 139 additions and 32 deletions.
139 changes: 113 additions & 26 deletions lib/Doctrine/ORM/Tools/Export/Driver/AnnotationExporter.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,10 +40,17 @@ class AnnotationExporter extends AbstractExporter

private $_isNew = false;
private $_outputPath;
private $_numSpaces = 4;
private $_numSpaces;
private $_spaces;
private $_classToExtend;
private $_currentCode;

public function __construct($dir = null)
{
parent::__construct($dir);
$this->setNumSpaces(4);
}

/**
* Converts a single ClassMetadata instance to the exported format
* and returns it
Expand Down Expand Up @@ -102,6 +109,7 @@ public function exportClassMetadata(ClassMetadataInfo $metadata)
*/
public function setNumSpaces($numSpaces)
{
$this->_spaces = str_repeat(' ', $numSpaces);
$this->_numSpaces = $numSpaces;
}

Expand Down Expand Up @@ -212,34 +220,34 @@ private function _addMethod($type, $fieldName, $metadata, array &$methods)
}

$method = array();
$method[] = str_repeat(' ', $this->_numSpaces) . '/**';
$method[] = $this->_spaces . '/**';
if ($type == 'get') {
$method[] = str_repeat(' ', $this->_numSpaces) . ' * Get ' . $fieldName;
$method[] = $this->_spaces . ' * Get ' . $fieldName;
} else if ($type == 'set') {
$method[] = str_repeat(' ', $this->_numSpaces) . ' * Set ' . $fieldName;
$method[] = $this->_spaces . ' * Set ' . $fieldName;
} else if ($type == 'add') {
$method[] = str_repeat(' ', $this->_numSpaces) . ' * Add ' . $fieldName;
$method[] = $this->_spaces . ' * Add ' . $fieldName;
}
$method[] = str_repeat(' ', $this->_numSpaces) . ' */';
$method[] = $this->_spaces . ' */';

if ($type == 'get') {
$method[] = str_repeat(' ', $this->_numSpaces) . 'public function ' . $methodName . '()';
$method[] = $this->_spaces . 'public function ' . $methodName . '()';
} else if ($type == 'set') {
$method[] = str_repeat(' ', $this->_numSpaces) . 'public function ' . $methodName . '($value)';
$method[] = $this->_spaces . 'public function ' . $methodName . '($value)';
} else if ($type == 'add') {
$method[] = str_repeat(' ', $this->_numSpaces) . 'public function ' . $methodName . '($value)';
$method[] = $this->_spaces . 'public function ' . $methodName . '($value)';
}

$method[] = str_repeat(' ', $this->_numSpaces) . '{';
$method[] = $this->_spaces . '{';
if ($type == 'get') {
$method[] = str_repeat(' ', $this->_numSpaces) . str_repeat(' ', $this->_numSpaces) . 'return $this->' . $fieldName . ';';
$method[] = $this->_spaces . $this->_spaces . 'return $this->' . $fieldName . ';';
} else if ($type == 'set') {
$method[] = str_repeat(' ', $this->_numSpaces) . str_repeat(' ', $this->_numSpaces) . '$this->' . $fieldName . ' = $value;';
$method[] = $this->_spaces . $this->_spaces . '$this->' . $fieldName . ' = $value;';
} else if ($type == 'add') {
$method[] = str_repeat(' ', $this->_numSpaces) . str_repeat(' ', $this->_numSpaces) . '$this->' . $fieldName . '[] = $value;';
$method[] = $this->_spaces . $this->_spaces . '$this->' . $fieldName . '[] = $value;';
}

$method[] = str_repeat(' ', $this->_numSpaces) . '}';
$method[] = $this->_spaces . '}';
$method[] = "\n";

$methods[] = implode("\n", $method);
Expand Down Expand Up @@ -288,21 +296,98 @@ private function _getTableAnnotation($metadata)
return '@Table(' . implode(', ', $table) . ')';
}

private function _getJoinColumnAnnotation(array $joinColumn)
{
$joinColumnAnnot = array();
if (isset($joinColumn['name'])) {
$joinColumnAnnot[] = 'name="' . $joinColumn['name'] . '"';
}
if (isset($joinColumn['referencedColumnName'])) {
$joinColumnAnnot[] = 'referencedColumnName="' . $joinColumn['referencedColumnName'] . '"';
}
if (isset($joinColumn['unique']) && $joinColumn['unique']) {
$joinColumnAnnot[] = 'unique=' . ($joinColumn['unique'] ? 'true' : 'false');
}
if (isset($joinColumn['nullable'])) {
$joinColumnAnnot[] = 'nullable=' . ($joinColumn['nullable'] ? 'true' : 'false');
}
if (isset($joinColumn['onDelete'])) {
$joinColumnAnnot[] = 'onDelete=' . ($joinColumn['onDelete'] ? 'true' : 'false');
}
if (isset($joinColumn['onUpdate'])) {
$joinColumnAnnot[] = 'onUpdate=' . ($joinColumn['onUpdate'] ? 'true' : 'false');
}
return '@JoinColumn(' . implode(', ', $joinColumnAnnot) . ')';
}

private function _getAssociationMappingAnnotation(AssociationMapping $associationMapping, ClassMetadataInfo $metadata)
{
// TODO: This function still needs to be written :)
$e = explode('\\', get_class($associationMapping));
$type = str_replace('Mapping', '', end($e));
$typeOptions = array();
if (isset($associationMapping->targetEntityName)) {
$typeOptions[] = 'targetEntity="' . $associationMapping->targetEntityName . '"';
}
if (isset($associationMapping->mappedByFieldName)) {
$typeOptions[] = 'mappedBy="' . $associationMapping->mappedByFieldName . '"';
}
if (isset($associationMapping->cascades) && $associationMapping->cascades) {
$typeOptions[] = 'cascade={"' . implode('"', $associationMapping->cascades) . '"}';
}
if (isset($associationMapping->orphanRemoval) && $associationMapping->orphanRemoval) {
$typeOptions[] = 'orphanRemoval=' . ($associationMapping->orphanRemoval ? 'true' : 'false');
}

$lines = array();
$lines[] = str_repeat(' ', $this->_numSpaces) . '/**';
$lines[] = str_repeat(' ', $this->_numSpaces) . ' *';
$lines[] = str_repeat(' ', $this->_numSpaces) . ' */';
$lines[] = $this->_spaces . '/**';
$lines[] = $this->_spaces . ' * @' . $type . '(' . implode(', ', $typeOptions) . ')';

if (isset($associationMapping->joinColumns) && $associationMapping->joinColumns) {
$lines[] = $this->_spaces . ' * @JoinColumns({';

$joinColumnsLines = array();
foreach ($associationMapping->joinColumns as $joinColumn) {
if ($joinColumnAnnot = $this->_getJoinColumnAnnotation($joinColumn)) {
$joinColumnsLines[] = $this->_spaces . ' * ' . $joinColumnAnnot;
}
}
$lines[] = implode(",\n", $joinColumnsLines);
$lines[] = $this->_spaces . ' * })';
}

if (isset($associationMapping->joinTable) && $associationMapping->joinTable) {
$joinTable = array();
$joinTable[] = 'name="' . $associationMapping->joinTable['name'] . '"';
if (isset($associationMapping->joinTable['schema'])) {
$joinTable[] = 'schema="' . $associationMapping->joinTable['schema'] . '"';
}

$lines[] = $this->_spaces . ' * @JoinTable(' . implode(', ', $joinTable) . ',';

$lines[] = $this->_spaces . ' * joinColumns={';
foreach ($associationMapping->joinTable['joinColumns'] as $joinColumn) {
$lines[] = $this->_spaces . ' * ' . $this->_getJoinColumnAnnotation($joinColumn);
}
$lines[] = $this->_spaces . ' * },';

$lines[] = $this->_spaces . ' * inverseJoinColumns={';
foreach ($associationMapping->joinTable['inverseJoinColumns'] as $joinColumn) {
$lines[] = $this->_spaces . ' * ' . $this->_getJoinColumnAnnotation($joinColumn);
}
$lines[] = $this->_spaces . ' * }';

$lines[] = $this->_spaces . ' * )';
}

$lines[] = $this->_spaces . ' */';

return implode("\n", $lines);
}

private function _getFieldMappingAnnotation(array $fieldMapping, ClassMetadataInfo $metadata)
{
$lines = array();
$lines[] = str_repeat(' ', $this->_numSpaces) . '/**';
$lines[] = $this->_spaces . '/**';

$column = array();
if (isset($fieldMapping['columnName'])) {
Expand Down Expand Up @@ -330,16 +415,18 @@ private function _getFieldMappingAnnotation(array $fieldMapping, ClassMetadataIn
$value = str_replace("'", '"', $value);
$options[] = ! is_numeric($key) ? $key . '=' . $value:$value;
}
$column[] = 'options={' . implode(', ', $options) . '}';
if ($options) {
$column[] = 'options={' . implode(', ', $options) . '}';
}
}
if (isset($fieldMapping['unique'])) {
$column[] = 'unique=' . var_export($fieldMapping['unique'], true);
}
$lines[] = str_repeat(' ', $this->_numSpaces) . ' * @Column(' . implode(', ', $column) . ')';
$lines[] = $this->_spaces . ' * @Column(' . implode(', ', $column) . ')';
if (isset($fieldMapping['id']) && $fieldMapping['id']) {
$lines[] = str_repeat(' ', $this->_numSpaces) . ' * @Id';
$lines[] = $this->_spaces . ' * @Id';
if ($generatorType = $this->_getIdGeneratorTypeString($metadata->generatorType)) {
$lines[] = str_repeat(' ', $this->_numSpaces).' * @GeneratedValue(strategy="' . $generatorType . '")';
$lines[] = $this->_spaces.' * @GeneratedValue(strategy="' . $generatorType . '")';
}
if ($metadata->sequenceGeneratorDefinition) {
$sequenceGenerator = array();
Expand All @@ -352,13 +439,13 @@ private function _getFieldMappingAnnotation(array $fieldMapping, ClassMetadataIn
if (isset($metadata->sequenceGeneratorDefinition['initialValue'])) {
$sequenceGenerator[] = 'initialValue="' . $metadata->sequenceGeneratorDefinition['initialValue'] . '"';
}
$lines[] = str_repeat(' ', $this->_numSpaces) . ' * @SequenceGenerator(' . implode(', ', $sequenceGenerator) . ')';
$lines[] = $this->_spaces . ' * @SequenceGenerator(' . implode(', ', $sequenceGenerator) . ')';
}
}
if (isset($fieldMapping['version']) && $fieldMapping['version']) {
$lines[] = str_repeat(' ', $this->_numSpaces) . ' * @Version';
$lines[] = $this->_spaces . ' * @Version';
}
$lines[] = str_repeat(' ', $this->_numSpaces) . ' */';
$lines[] = $this->_spaces . ' */';

return implode("\n", $lines);
}
Expand Down
2 changes: 1 addition & 1 deletion tools/sandbox/Entities/Address.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ class Address {
private $id;
/** @Column(type="string", length=255) */
private $street;
/** @OneToOne(targetEntity="User", mappedBy="address") */
/** @OneToOne(targetEntity="User", mappedBy="address", cascade={"persist"}) */
private $user;

public function getId() {
Expand Down
13 changes: 12 additions & 1 deletion tools/sandbox/Entities/User.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,20 @@ class User {
private $test;
/**
* @OneToOne(targetEntity="Address")
* @JoinColumn(name="address_id", referencedColumnName="id")
* @JoinColumns({
* @JoinColumn(name="address_id", referencedColumnName="id"),
* @JoinColumn(name="address2_id", referencedColumnName="id")
* })
*/
private $address;
/**
* @ManyToMany(targetEntity="Group")
* @JoinTable(name="user_group",
* joinColumns={@JoinColumn(name="user_id", referencedColumnName="id")},
* inverseJoinColumns={@JoinColumn(name="group_id", referencedColumnName="id")
* })
*/
private $groups;

public function getId() {
return $this->id;
Expand Down
6 changes: 4 additions & 2 deletions tools/sandbox/cli-config.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,10 @@
$config->setMetadataCacheImpl(new \Doctrine\Common\Cache\ArrayCache);

$connectionOptions = array(
'driver' => 'pdo_sqlite',
'path' => 'database.sqlite'
'driver' => 'pdo_mysql',
'user' => 'root',
'password' => '',
'dbname' => 'doctrine2'
);

$em = \Doctrine\ORM\EntityManager::create($connectionOptions, $config);
Expand Down
11 changes: 9 additions & 2 deletions tools/sandbox/index.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@

// Set up caches
$config = new \Doctrine\ORM\Configuration;
$cache = new \Doctrine\Common\Cache\ApcCache;
$cache = new \Doctrine\Common\Cache\ArrayCache;
$config->setMetadataCacheImpl($cache);
$config->setQueryCacheImpl($cache);

Expand All @@ -30,6 +30,13 @@
## PUT YOUR TEST CODE BELOW

$user = new User;
$user->setName('jwage');

$address = new Address;
$address->setStreet('6512 Mercomatic Court');
$address->setUser($user);
$user->setAddress($address);

echo "Hello World!";
$em->persist($user);
$em->persist($address);
$em->flush();

0 comments on commit 204b6d7

Please sign in to comment.