diff --git a/lib/Doctrine/ORM/Mapping/ClassMetadataInfo.php b/lib/Doctrine/ORM/Mapping/ClassMetadataInfo.php index a52bdb8a1..4efdaf44f 100644 --- a/lib/Doctrine/ORM/Mapping/ClassMetadataInfo.php +++ b/lib/Doctrine/ORM/Mapping/ClassMetadataInfo.php @@ -1224,7 +1224,8 @@ class ClassMetadataInfo implements ClassMetadata foreach ($mapping['joinColumns'] as $key => &$joinColumn) { if ($mapping['type'] === self::ONE_TO_ONE && ! $this->isInheritanceTypeSingleTable()) { if (count($mapping['joinColumns']) == 1) { - $joinColumn['unique'] = true; + if(! isset($mapping['id']) || ! $mapping['id']) + $joinColumn['unique'] = true; } else { $uniqueContraintColumns[] = $joinColumn['name']; } diff --git a/lib/Doctrine/ORM/Mapping/Driver/DatabaseDriver.php b/lib/Doctrine/ORM/Mapping/Driver/DatabaseDriver.php index 8d6136ac5..49f9fcf6c 100644 --- a/lib/Doctrine/ORM/Mapping/Driver/DatabaseDriver.php +++ b/lib/Doctrine/ORM/Mapping/Driver/DatabaseDriver.php @@ -192,12 +192,14 @@ class DatabaseDriver implements Driver $fieldMappings = array(); foreach ($columns as $column) { $fieldMapping = array(); - if ($primaryKeyColumns && in_array($column->getName(), $primaryKeyColumns)) { - $fieldMapping['id'] = true; - } else if (in_array($column->getName(), $allForeignKeyColumns)) { + + + if (in_array($column->getName(), $allForeignKeyColumns)) { continue; + } else if ($primaryKeyColumns && in_array($column->getName(), $primaryKeyColumns)) { + $fieldMapping['id'] = true; } - + $fieldMapping['fieldName'] = $this->getFieldNameForColumn($tableName, $column->getName(), false); $fieldMapping['columnName'] = $column->getName(); $fieldMapping['type'] = strtolower((string) $column->getType()); @@ -297,6 +299,11 @@ class DatabaseDriver implements Driver $associationMapping = array(); $associationMapping['fieldName'] = $this->getFieldNameForColumn($tableName, $localColumn, true); $associationMapping['targetEntity'] = $this->getClassNameForTable($foreignTable); + + if ($primaryKeyColumns && in_array($localColumn, $primaryKeyColumns)) + { + $associationMapping['id'] = true; + } for ($i = 0; $i < count($cols); $i++) { $associationMapping['joinColumns'][] = array( @@ -304,7 +311,16 @@ class DatabaseDriver implements Driver 'referencedColumnName' => $fkCols[$i], ); } - $metadata->mapManyToOne($associationMapping); + + + //Here we need to check if $cols are the same as $primaryKeyColums + if(!array_diff($cols,$primaryKeyColumns)) + $metadata->mapOneToOne($associationMapping); + else + $metadata->mapManyToOne($associationMapping); + + + } } diff --git a/lib/Doctrine/ORM/Tools/EntityGenerator.php b/lib/Doctrine/ORM/Tools/EntityGenerator.php index 681892818..324165b8c 100644 --- a/lib/Doctrine/ORM/Tools/EntityGenerator.php +++ b/lib/Doctrine/ORM/Tools/EntityGenerator.php @@ -889,6 +889,16 @@ public function () if ($this->_generateAnnotations) { $lines[] = $this->_spaces . ' *'; + + if (isset($associationMapping['id']) && $associationMapping['id']) { + $lines[] = $this->_spaces . ' * @' . $this->_annotationsPrefix . 'Id'; + + if ($generatorType = $this->_getIdGeneratorTypeString($metadata->generatorType)) { + $lines[] = $this->_spaces.' * @' . $this->_annotationsPrefix . 'GeneratedValue(strategy="' . $generatorType . '")'; + } + + + } $type = null; switch ($associationMapping['type']) {