Skip to content

Commit

Permalink
Merge pull request #246 from tecbot/array-changeset
Browse files Browse the repository at this point in the history
Fixed change sets of array updates
  • Loading branch information
jwage committed Feb 24, 2012
2 parents 94bd45d + 0e67e8f commit 57e7b2d
Show file tree
Hide file tree
Showing 2 changed files with 98 additions and 4 deletions.
8 changes: 5 additions & 3 deletions lib/Doctrine/ODM/MongoDB/UnitOfWork.php
Original file line number Diff line number Diff line change
Expand Up @@ -683,6 +683,8 @@ public function computeChangeSet(ClassMetadata $class, $document)
}
} else if (is_object($orgValue) && $orgValue !== $actualValue) {
$changeSet[$propName] = array($orgValue, $actualValue);
} else if (is_array($orgValue) && is_array($actualValue) && ($diff = array_diff($actualValue, $orgValue))) {
$changeSet[$propName] = array($orgValue, $actualValue);
} else if ($orgValue != $actualValue || ($orgValue === null ^ $actualValue === null)) {
$changeSet[$propName] = array($orgValue, $actualValue);
}
Expand Down Expand Up @@ -2700,11 +2702,11 @@ public function getScheduledCollectionDeletions()
public function getScheduledCollectionUpdates()
{
return $this->collectionUpdates;
}
}

/**
* Helper method to initialize a lazy loading proxy or persistent collection.
*
*
* @param object
* @return void
*/
Expand All @@ -2721,4 +2723,4 @@ private static function objToStr($obj)
{
return method_exists($obj, '__toString') ? (string)$obj : get_class($obj).'@'.spl_object_hash($obj);
}
}
}
94 changes: 93 additions & 1 deletion tests/Doctrine/ODM/MongoDB/Tests/UnitOfWorkTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -257,6 +257,78 @@ public function testPreUpdateTriggeredWithEmptyChangeset()
$dm->flush();
}

/**
* @dataProvider getScheduleForUpdateWithArraysTests
*/
public function testScheduleForUpdateWithArrays($origData, $updateData, $shouldInUpdate)
{
$pb = $this->getMockPersistenceBuilder();
$class = $this->dm->getClassMetadata("Doctrine\ODM\MongoDB\Tests\ArrayTest");
$persister = $this->getMockDocumentPersister($pb, $class);
$this->uow->setDocumentPersister('Doctrine\ODM\MongoDB\Tests\ArrayTest', $persister);

$arrayTest = new ArrayTest($origData);
$this->uow->persist($arrayTest);
$this->uow->computeChangeSets();

$arrayTest->data = $updateData;
$this->uow->persist($arrayTest);
$this->uow->computeChangeSets();

$this->assertEquals($shouldInUpdate, $this->uow->isScheduledForUpdate($arrayTest));
}

public function getScheduleForUpdateWithArraysTests()
{
return array(
array(
null,
array('bar' => 'foo'),
true
),
array(
array('foo' => 'bar'),
null,
true
),
array(
array('foo' => 'bar'),
array('bar' => 'foo'),
true
),
array(
array('foo' => 'bar'),
array('foo' => 'foo'),
true
),
array(
array('foo' => 'bar'),
array('foo' => 'bar'),
false
),
array(
array('foo' => 'bar'),
array('foo' => true),
true
),
array(
array('foo' => 'bar'),
array('foo' => 99),
true
),
array(
array('foo' => 99),
array('foo' => true),
true
),
array(
array('foo' => true),
array('foo' => true),
false
),
);
}

protected function getDocumentManager()
{
return new \Stubs\DocumentManager();
Expand Down Expand Up @@ -406,4 +478,24 @@ public function getOwner() {
public function setOwner($owner) {
$this->owner = $owner;
}
}
}

/**
* @ODM\Document
*/
class ArrayTest
{
/**
* @ODM\Id
*/
private $id;
/**
* @ODM\Hash
*/
public $data;

public function __construct($data)
{
$this->data = $data;
}
}

0 comments on commit 57e7b2d

Please sign in to comment.