Skip to content

Commit

Permalink
Merge pull request #1715 from malarzm/remove-single-arg-flush
Browse files Browse the repository at this point in the history
[2.0] Drop support for UnitOfWork::flush($document)
  • Loading branch information
malarzm committed Jan 9, 2018
2 parents f078de3 + f876a77 commit f2e43e9
Show file tree
Hide file tree
Showing 11 changed files with 11 additions and 347 deletions.
8 changes: 2 additions & 6 deletions lib/Doctrine/ODM/MongoDB/DocumentManager.php
Expand Up @@ -508,17 +508,13 @@ public function getRepository($documentName)
* This effectively synchronizes the in-memory state of managed objects with the
* database.
*
* @param object $document
* @param array $options Array of options to be used with batchInsert(), update() and remove()
* @throws \InvalidArgumentException
*/
public function flush($document = null, array $options = array())
public function flush(array $options = array())
{
if (null !== $document && ! is_object($document) && ! is_array($document)) {
throw new \InvalidArgumentException(gettype($document));
}
$this->errorIfClosed();
$this->unitOfWork->commit($document, $options);
$this->unitOfWork->commit($options);
}

/**
Expand Down
59 changes: 2 additions & 57 deletions lib/Doctrine/ODM/MongoDB/UnitOfWork.php
Expand Up @@ -360,26 +360,17 @@ public function setDocumentPersister($documentName, Persisters\DocumentPersister
* 2) All document updates
* 3) All document deletions
*
* @param object $document
* @param array $options Array of options to be used with batchInsert(), update() and remove()
*/
public function commit($document = null, array $options = array())
public function commit(array $options = array())
{
// Raise preFlush
if ($this->evm->hasListeners(Events::preFlush)) {
$this->evm->dispatchEvent(Events::preFlush, new Event\PreFlushEventArgs($this->dm));
}

// Compute changes done since last commit.
if ($document === null) {
$this->computeChangeSets();
} elseif (is_object($document)) {
$this->computeSingleDocumentChangeSet($document);
} elseif (is_array($document)) {
foreach ($document as $object) {
$this->computeSingleDocumentChangeSet($object);
}
}
$this->computeChangeSets();

if ( ! ($this->documentInsertions ||
$this->documentUpserts ||
Expand Down Expand Up @@ -517,52 +508,6 @@ private function computeScheduleUpsertsChangeSets()
}
}

/**
* Only flush the given document according to a ruleset that keeps the UoW consistent.
*
* 1. All documents scheduled for insertion and (orphan) removals are processed as well!
* 2. Proxies are skipped.
* 3. Only if document is properly managed.
*
* @param object $document
* @throws \InvalidArgumentException If the document is not STATE_MANAGED
* @return void
*/
private function computeSingleDocumentChangeSet($document)
{
$state = $this->getDocumentState($document);

if ($state !== self::STATE_MANAGED && $state !== self::STATE_REMOVED) {
throw new \InvalidArgumentException('Document has to be managed or scheduled for removal for single computation ' . $this->objToStr($document));
}

$class = $this->dm->getClassMetadata(get_class($document));

if ($state === self::STATE_MANAGED && $class->isChangeTrackingDeferredImplicit()) {
$this->persist($document);
}

// Compute changes for INSERTed and UPSERTed documents first. This must always happen even in this case.
$this->computeScheduleInsertsChangeSets();
$this->computeScheduleUpsertsChangeSets();

// Ignore uninitialized proxy objects
if ($document instanceof Proxy && ! $document->__isInitialized__) {
return;
}

// Only MANAGED documents that are NOT SCHEDULED FOR INSERTION, UPSERT OR DELETION are processed here.
$oid = spl_object_hash($document);

if ( ! isset($this->documentInsertions[$oid])
&& ! isset($this->documentUpserts[$oid])
&& ! isset($this->documentDeletions[$oid])
&& isset($this->documentStates[$oid])
) {
$this->computeChangeSet($class, $document);
}
}

/**
* Gets the changeset for a document.
*
Expand Down
Expand Up @@ -143,7 +143,7 @@ private function getTestUser($username)
$user->categories[0]->children[1]->children[1] = new CollectionPersisterCategory('Child of Category1_1 2');

$this->dm->persist($user);
$this->dm->flush(null, array());
$this->dm->flush();
return $user;
}

Expand Down
120 changes: 0 additions & 120 deletions tests/Doctrine/ODM/MongoDB/Tests/Functional/FlushTest.php
Expand Up @@ -53,126 +53,6 @@ public function testFlush()
$this->assertSize(1);
}

public function testFlushManyExplicitDocuments()
{
$userA = new FriendUser('userA');
$userB = new FriendUser('userB');
$userC = new FriendUser('userC');

$this->dm->persist($userA);
$this->dm->persist($userB);
$this->dm->persist($userC);

$this->dm->flush(array($userA, $userB, $userC));

$this->assertNotNull($userA->id);
$this->assertNotNull($userB->id);
$this->assertNotNull($userC->id);
}

public function testFlushSingleManagedDocument()
{
$user = new CmsUser;
$user->name = 'Dominik';
$user->username = 'domnikl';
$user->status = 'developer';

$this->dm->persist($user);
$this->dm->flush();

$user->status = 'administrator';
$this->dm->flush($user);
$this->dm->clear();

$user = $this->dm->find(get_class($user), $user->id);
$this->assertEquals('administrator', $user->status);
}

public function testFlushSingleUnmanagedDocument()
{
$user = new CmsUser;
$user->name = 'Dominik';
$user->username = 'domnikl';
$user->status = 'developer';

$this->expectException(\InvalidArgumentException::class);
$this->expectExceptionMessage('Document has to be managed or scheduled for removal for single computation');
$this->dm->flush($user);
}

public function testFlushSingleAndNewDocument()
{
$user = new CmsUser;
$user->name = 'Dominik';
$user->username = 'domnikl';
$user->status = 'developer';

$this->dm->persist($user);
$this->dm->flush();

$otherUser = new CmsUser;
$otherUser->name = 'Dominik2';
$otherUser->username = 'domnikl2';
$otherUser->status = 'developer';

$user->status = 'administrator';

$this->dm->persist($otherUser);
$this->dm->flush($user);

$this->assertTrue($this->dm->contains($otherUser), "Other user is contained in DocumentManager");
$this->assertGreaterThan(0, $otherUser->id, "other user has an id");
}

public function testFlushAndCascadePersist()
{
$user = new CmsUser;
$user->name = 'Dominik';
$user->username = 'domnikl';
$user->status = 'developer';

$this->dm->persist($user);
$this->dm->flush();

$address = new CmsAddress();
$address->city = "Springfield";
$address->zip = "12354";
$address->country = "Germany";
$address->street = "Foo Street";
$address->user = $user;
$user->address = $address;

$this->dm->flush($user);

$this->assertTrue($this->dm->contains($address), "Other user is contained in DocumentManager");
$this->assertGreaterThan(0, $address->id, "other user has an id");
}

public function testProxyIsIgnored()
{
$user = new CmsUser;
$user->name = 'Dominik';
$user->username = 'domnikl';
$user->status = 'developer';

$this->dm->persist($user);
$this->dm->flush();
$this->dm->clear();

$user = $this->dm->getReference(get_class($user), $user->id);

$otherUser = new CmsUser;
$otherUser->name = 'Dominik2';
$otherUser->username = 'domnikl2';
$otherUser->status = 'developer';

$this->dm->persist($otherUser);
$this->dm->flush($user);

$this->assertTrue($this->dm->contains($otherUser), "Other user is contained in DocumentManager");
$this->assertGreaterThan(0, $otherUser->id, "other user has an id");
}

protected function assertSize($size)
{
$this->assertEquals($size, $this->dm->getUnitOfWork()->size());
Expand Down
21 changes: 0 additions & 21 deletions tests/Doctrine/ODM/MongoDB/Tests/Functional/FunctionalTest.php
Expand Up @@ -108,27 +108,6 @@ public function testInheritedAssociationMappings()
$this->assertTrue(isset($class->associationMappings['groups']));
}

public function testFlushSingleDocument()
{
$user1 = new \Documents\ForumUser();
$user1->username = 'romanb';
$user2 = new \Documents\ForumUser();
$user2->username = 'jwage';
$this->dm->persist($user1);
$this->dm->persist($user2);
$this->dm->flush();

$user1->username = 'changed';
$user2->username = 'changed';
$this->dm->flush($user1);

$check = $this->dm->getDocumentCollection('Documents\ForumUser')->find(array('username' => 'jwage'));
$this->assertNotNull($check);

$check = $this->dm->getDocumentCollection('Documents\ForumUser')->find(array('username' => 'changed'));
$this->assertNotNull($check);
}

public function testNestedCategories()
{
$root = new \Documents\Category('Root');
Expand Down
Expand Up @@ -46,7 +46,7 @@ public function testUpdateWithIncrement()
$document->name = 'test';

$this->dm->persist($document);
$this->dm->flush($document);
$this->dm->flush();
$this->dm->clear();

$document = $this->dm->getRepository(GH1435DocumentIncrement::class)->findOneBy([]);
Expand Down
44 changes: 0 additions & 44 deletions tests/Doctrine/ODM/MongoDB/Tests/Functional/Ticket/GH301Test.php

This file was deleted.

45 changes: 0 additions & 45 deletions tests/Doctrine/ODM/MongoDB/Tests/Functional/Ticket/GH567Test.php

This file was deleted.

0 comments on commit f2e43e9

Please sign in to comment.