From a5eb0f2e820b1c2836a18868b76799ae65a729cc Mon Sep 17 00:00:00 2001
From: Thomas Ploch <thomas.ploch@meinfernbus.de>
Date: Tue, 7 Jun 2016 17:33:29 +0200
Subject: [PATCH] Exporters should only inspect `joinColumns` for owning side
 in bi-directional OneToOne

rebased commits:

- Added test case for bi-directional OneToOne in YamlExporter
- Only inspect joinColumns for owning side in bi-directional OneToOne in YamlExporter
- Adding bi-directional test case without joinColumn to XmlExporter test
- Same testcase also applied to PhpExporter
- Fixing bi-directional issue in PhpExporter when inspecting joinColumns index
- Implemented @Ocramius suggestions
---
 lib/Doctrine/ORM/Tools/Export/Driver/PhpExporter.php |  2 +-
 .../ORM/Tools/Export/Driver/YamlExporter.php         |  2 +-
 .../php/Doctrine.Tests.ORM.Tools.Export.User.php     | 12 ++++++++++++
 .../xml/Doctrine.Tests.ORM.Tools.Export.User.dcm.xml |  6 ++++++
 .../Doctrine.Tests.ORM.Tools.Export.User.dcm.yml     |  4 ++++
 5 files changed, 24 insertions(+), 2 deletions(-)

diff --git a/lib/Doctrine/ORM/Tools/Export/Driver/PhpExporter.php b/lib/Doctrine/ORM/Tools/Export/Driver/PhpExporter.php
index 29eb37afc..5d88901a5 100644
--- a/lib/Doctrine/ORM/Tools/Export/Driver/PhpExporter.php
+++ b/lib/Doctrine/ORM/Tools/Export/Driver/PhpExporter.php
@@ -117,7 +117,7 @@ class PhpExporter extends AbstractExporter
                 $oneToOneMappingArray = array(
                     'mappedBy'      => $associationMapping['mappedBy'],
                     'inversedBy'    => $associationMapping['inversedBy'],
-                    'joinColumns'   => $associationMapping['joinColumns'],
+                    'joinColumns'   => $associationMapping['isOwningSide'] ? $associationMapping['joinColumns'] : [],
                     'orphanRemoval' => $associationMapping['orphanRemoval'],
                 );
 
diff --git a/lib/Doctrine/ORM/Tools/Export/Driver/YamlExporter.php b/lib/Doctrine/ORM/Tools/Export/Driver/YamlExporter.php
index 177cebfab..7677ad950 100644
--- a/lib/Doctrine/ORM/Tools/Export/Driver/YamlExporter.php
+++ b/lib/Doctrine/ORM/Tools/Export/Driver/YamlExporter.php
@@ -163,7 +163,7 @@ class YamlExporter extends AbstractExporter
             }
 
             if ($associationMapping['type'] & ClassMetadataInfo::TO_ONE) {
-                $joinColumns = $associationMapping['joinColumns'];
+                $joinColumns = $associationMapping['isOwningSide'] ? $associationMapping['joinColumns'] : [];
                 $newJoinColumns = array();
 
                 foreach ($joinColumns as $joinColumn) {
diff --git a/tests/Doctrine/Tests/ORM/Tools/Export/php/Doctrine.Tests.ORM.Tools.Export.User.php b/tests/Doctrine/Tests/ORM/Tools/Export/php/Doctrine.Tests.ORM.Tools.Export.User.php
index 061934b9b..78ceb4e7e 100644
--- a/tests/Doctrine/Tests/ORM/Tools/Export/php/Doctrine.Tests.ORM.Tools.Export.User.php
+++ b/tests/Doctrine/Tests/ORM/Tools/Export/php/Doctrine.Tests.ORM.Tools.Export.User.php
@@ -57,6 +57,18 @@ $metadata->mapOneToOne(array(
    'orphanRemoval' => true,
    'fetch' => ClassMetadataInfo::FETCH_EAGER,
   ));
+$metadata->mapOneToOne(array(
+    'fieldName' => 'cart',
+    'targetEntity' => 'Doctrine\\Tests\\ORM\\Tools\\Export\\Cart',
+    'mappedBy' => 'user',
+    'cascade' =>
+        array(
+            0 => 'persist',
+        ),
+    'inversedBy' => NULL,
+    'orphanRemoval' => false,
+    'fetch' => ClassMetadataInfo::FETCH_EAGER,
+));
 $metadata->mapOneToMany(array(
    'fieldName' => 'phonenumbers',
    'targetEntity' => 'Doctrine\\Tests\\ORM\\Tools\\Export\\Phonenumber',
diff --git a/tests/Doctrine/Tests/ORM/Tools/Export/xml/Doctrine.Tests.ORM.Tools.Export.User.dcm.xml b/tests/Doctrine/Tests/ORM/Tools/Export/xml/Doctrine.Tests.ORM.Tools.Export.User.dcm.xml
index 8e30948e8..a84fc553a 100644
--- a/tests/Doctrine/Tests/ORM/Tools/Export/xml/Doctrine.Tests.ORM.Tools.Export.User.dcm.xml
+++ b/tests/Doctrine/Tests/ORM/Tools/Export/xml/Doctrine.Tests.ORM.Tools.Export.User.dcm.xml
@@ -30,6 +30,12 @@
             <join-column name="address_id" referenced-column-name="id" on-delete="CASCADE" on-update="CASCADE"/>
         </one-to-one>
 
+        <one-to-one field="cart" target-entity="Doctrine\Tests\ORM\Tools\Export\Cart" mapped-by="user">
+            <cascade>
+                <cascade-remove/>
+            </cascade>
+        </one-to-one>
+
         <many-to-one field="mainGroup" target-entity="Doctrine\Tests\ORM\Tools\Export\Group" />
 
         <one-to-many field="phonenumbers" target-entity="Doctrine\Tests\ORM\Tools\Export\Phonenumber" mapped-by="user" orphan-removal="true" fetch="LAZY">
diff --git a/tests/Doctrine/Tests/ORM/Tools/Export/yaml/Doctrine.Tests.ORM.Tools.Export.User.dcm.yml b/tests/Doctrine/Tests/ORM/Tools/Export/yaml/Doctrine.Tests.ORM.Tools.Export.User.dcm.yml
index 1a5b77609..02ecab2e0 100644
--- a/tests/Doctrine/Tests/ORM/Tools/Export/yaml/Doctrine.Tests.ORM.Tools.Export.User.dcm.yml
+++ b/tests/Doctrine/Tests/ORM/Tools/Export/yaml/Doctrine.Tests.ORM.Tools.Export.User.dcm.yml
@@ -30,6 +30,10 @@ Doctrine\Tests\ORM\Tools\Export\User:
       inversedBy: user
       orphanRemoval: true
       fetch: EAGER
+    cart:
+      targetEntity: Doctrine\Tests\ORM\Tools\Export\Cart
+      mappedBy: user
+      cascade: [ remove ]
   manyToOne:
     mainGroup:
       targetEntity: Doctrine\Tests\ORM\Tools\Export\Group