From ac1ffaf7e9b9833acb19785e21beb4e0cf838afe Mon Sep 17 00:00:00 2001
From: Klein Florian <florian.klein@free.fr>
Date: Wed, 2 Nov 2011 12:34:28 +0100
Subject: [PATCH] make merge copy non persited properties too

---
 lib/Doctrine/ORM/UnitOfWork.php               |  4 ++-
 tests/Doctrine/Tests/Models/CMS/CmsUser.php   |  4 +++
 .../ORM/Functional/BasicFunctionalTest.php    | 27 +++++++++++++++++++
 3 files changed, 34 insertions(+), 1 deletion(-)

diff --git a/lib/Doctrine/ORM/UnitOfWork.php b/lib/Doctrine/ORM/UnitOfWork.php
index 0e55cfa80..fa0178c34 100644
--- a/lib/Doctrine/ORM/UnitOfWork.php
+++ b/lib/Doctrine/ORM/UnitOfWork.php
@@ -1733,7 +1733,9 @@ class UnitOfWork implements PropertyChangedListener
             }
 
             // Merge state of $entity into existing (managed) entity
-            foreach ($class->reflFields as $name => $prop) {
+            foreach ($class->reflClass->getProperties() as $prop) {
+                $name = $prop->name;
+                $prop->setAccessible(true);
                 if ( ! isset($class->associationMappings[$name])) {
                     if ( ! $class->isIdentifier($name)) {
                         $prop->setValue($managedCopy, $prop->getValue($entity));
diff --git a/tests/Doctrine/Tests/Models/CMS/CmsUser.php b/tests/Doctrine/Tests/Models/CMS/CmsUser.php
index 05fefb590..1e5465784 100644
--- a/tests/Doctrine/Tests/Models/CMS/CmsUser.php
+++ b/tests/Doctrine/Tests/Models/CMS/CmsUser.php
@@ -56,6 +56,10 @@ class CmsUser
      */
     public $groups;
 
+    public $nonPersistedProperty;
+
+    public $nonPersistedPropertyObject;
+
     public function __construct() {
         $this->phonenumbers = new ArrayCollection;
         $this->articles = new ArrayCollection;
diff --git a/tests/Doctrine/Tests/ORM/Functional/BasicFunctionalTest.php b/tests/Doctrine/Tests/ORM/Functional/BasicFunctionalTest.php
index 695e10c55..75005acb1 100644
--- a/tests/Doctrine/Tests/ORM/Functional/BasicFunctionalTest.php
+++ b/tests/Doctrine/Tests/ORM/Functional/BasicFunctionalTest.php
@@ -905,6 +905,33 @@ class BasicFunctionalTest extends \Doctrine\Tests\OrmFunctionalTestCase
         $this->assertInstanceOf('Doctrine\Tests\Models\CMS\CmsUser', $user2);
     }
 
+    public function testMergeNonPersistedProperties()
+    {
+        $user = new CmsUser();
+        $user->username = "beberlei";
+        $user->name = "Benjamin E.";
+        $user->status = 'active';
+        $user->nonPersistedProperty = 'test';
+        $user->nonPersistedPropertyObject = new CmsPhonenumber();
+
+        $managedUser = $this->_em->merge($user);
+        $this->assertEquals('test', $managedUser->nonPersistedProperty);
+        $this->assertSame($user->nonPersistedProperty, $managedUser->nonPersistedProperty);
+        $this->assertSame($user->nonPersistedPropertyObject, $managedUser->nonPersistedPropertyObject);
+
+        $this->assertTrue($user !== $managedUser);
+        $this->assertTrue($this->_em->contains($managedUser));
+
+        $this->_em->flush();
+        $userId = $managedUser->id;
+        $this->_em->clear();
+
+        $user2 = $this->_em->find(get_class($managedUser), $userId);
+        $this->assertNull($user2->nonPersistedProperty);
+        $this->assertNull($user2->nonPersistedPropertyObject);
+        $this->assertEquals('active', $user2->status);
+    }
+
     public function testMergeThrowsExceptionIfEntityWithGeneratedIdentifierDoesNotExist()
     {
         $user = new CmsUser();