Skip to content

Commit

Permalink
Stop passing changeset by reference to PreUpdate event
Browse files Browse the repository at this point in the history
  • Loading branch information
malarzm committed Jan 2, 2016
1 parent 5e483d1 commit cd58ce3
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 2 deletions.
5 changes: 3 additions & 2 deletions lib/Doctrine/ODM/MongoDB/Event/PreUpdateEventArgs.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,10 +40,10 @@ class PreUpdateEventArgs extends LifecycleEventArgs
* @param DocumentManager $dm
* @param array $changeSet
*/
public function __construct($document, DocumentManager $dm, array &$changeSet)
public function __construct($document, DocumentManager $dm, array $changeSet)
{
parent::__construct($document, $dm);
$this->documentChangeSet = &$changeSet;
$this->documentChangeSet = $changeSet;
}

/**
Expand Down Expand Up @@ -105,6 +105,7 @@ public function setNewValue($field, $value)
$this->assertValidField($field);

$this->documentChangeSet[$field][1] = $value;
$this->getDocumentManager()->getUnitOfWork()->setDocumentChangeSet($this->getDocument(), $this->documentChangeSet);
}

/**
Expand Down
12 changes: 12 additions & 0 deletions lib/Doctrine/ODM/MongoDB/UnitOfWork.php
Original file line number Diff line number Diff line change
Expand Up @@ -585,6 +585,18 @@ public function getDocumentChangeSet($document)
return array();
}

/**
* INTERNAL:
* Sets the changeset for a document.
*
* @param object $document
* @param array $changeset
*/
public function setDocumentChangeSet($document, $changeset)
{
$this->documentChangeSets[spl_object_hash($document)] = $changeset;
}

/**
* Get a documents actual data, flattening all the objects to arrays.
*
Expand Down
34 changes: 34 additions & 0 deletions tests/Doctrine/ODM/MongoDB/Tests/Events/PreUpdateEventArgsTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<?php

namespace Doctrine\ODM\MongoDB\Tests\Events;

use Doctrine\ODM\MongoDB\Event\PreUpdateEventArgs;
use Doctrine\ODM\MongoDB\Events;
use Documents\Article;

class PreUpdateEventArgsTest extends \Doctrine\ODM\MongoDB\Tests\BaseTest
{
public function setUp()
{
parent::setUp();
$this->dm->getEventManager()->addEventListener(Events::preUpdate, $this);
}

public function testChangeSetIsUpdated()
{
$a = new Article();
$a->setTitle('Title');
$this->dm->persist($a);
$this->dm->flush();
$a->setBody('Body');
$this->dm->flush();
$this->dm->clear();
$a = $this->dm->find(Article::class, $a->getId());
$this->assertEquals('Changed', $a->getBody());
}

public function preUpdate(PreUpdateEventArgs $e)
{
$e->setNewValue('body', 'Changed');
}
}

0 comments on commit cd58ce3

Please sign in to comment.