Skip to content

Fixed change sets of array updates #246

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Feb 24, 2012
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions lib/Doctrine/ODM/MongoDB/UnitOfWork.php
Original file line number Diff line number Diff line change
Expand Up @@ -653,6 +653,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))) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this the best possible check we can do here? Are their any other situations we need to account for?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

maybe we can check with !==.
The problem is if i have a array with 'foo' => 'bar' and i will change this to 'foo' => true the != operator doesn't work.

$changeSet[$propName] = array($orgValue, $actualValue);
} else if ($orgValue != $actualValue || ($orgValue === null ^ $actualValue === null)) {
$changeSet[$propName] = array($orgValue, $actualValue);
}
Expand Down Expand Up @@ -2629,11 +2631,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 Down
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;
}
}