[MODM-97] FindAndModify does not return an object Created: 04/Dec/10  Updated: 20/Dec/10  Resolved: 20/Dec/10

Status: Resolved
Project: Doctrine MongoDB ODM
Component/s: Document Manager
Affects Version/s: 1.0.0BETA1
Fix Version/s: 1.0.0BETA2

Type: Bug Priority: Major
Reporter: Başar Aykut Assignee: Jonathan H. Wage
Resolution: Fixed Votes: 0
Labels: None
Environment:

Windows xp sp3, apache



 Description   

Boolean true is returned instead of an object after a FindAndModify command is successfully executed.

$c = $dm->createQuery('downloads')
->findAndModify(array('new' => true))
>field('key')>equals('file1')
->update()
>field('counter')>inc(1)
->execute();

echo gettype($c); // boolean



 Comments   
Comment by Jonathan H. Wage [ 05/Dec/10 ]

The syntax using the latest version of mongodb odm in git is:

$c = $dm->createQuery('downloads')
->findAndUpdate()
->returnNew()
->field('key')->equals('file1')
->field('counter')>inc(1)
->execute();
Comment by Başar Aykut [ 06/Dec/10 ]

findAndUpdate does not return an object also however it returns an array which includes the object. Is it an expected behavior? And it does not update the record?

array(1) {
  ["4cf9163aff2e00000000154e"]=>
  object(downloads)#125 (3) {
    ["id"]=>string(24) "4cf9163aff2e00000000154e"
    ["key"] => string(6) "file1"
    ["counter"] => int(10391)
  }
}
Comment by Jonathan H. Wage [ 06/Dec/10 ]

Are you using the latest version of everything?

Comment by Başar Aykut [ 07/Dec/10 ]

I have updated to the latest version in the git. Below query works and returns the updated object as expected. However this is true only for the first call. Repeated calls return the first object .

		$pid = $dm->createQueryBuilder('downloads')
				  ->findAndUpdate()
				  ->returnNew(true)
				  ->field('key')->equals('file1')
				  ->field('counter')->inc(1)
				  ->getQuery()->execute();
Comment by Jonathan H. Wage [ 07/Dec/10 ]

Hi, I don't follow. Can you show a test of what you are doing? I did a simple test executing a findAndUpdate 2 times in a row and it worked.

Comment by Başar Aykut [ 07/Dec/10 ]

It works but it doesn't return the updated object on subsequent calls.

$pid = $dm->createQueryBuilder('downloads')
				  ->findAndUpdate()
				  ->returnNew(true)
				  ->field('key')->equals('file1')
				  ->field('counter')->inc(1)
				  ->getQuery()->execute();

var_dump($pid);

$pid = $dm->createQueryBuilder('downloads')
				  ->findAndUpdate()
				  ->returnNew(true)
				  ->field('key')->equals('file1')
				  ->field('counter')->inc(1)
				  ->getQuery()->execute();

var_dump($pid);

Outputs

object(downloads)#112 (3) {
  ["id"]=>
  string(24) "4cfadede55242b0084000003"
  ["key"]=>
  string(6) "file1"
  ["counter"]=>
  int(1)
}

object(downloads)#112 (3) {
  ["id"]=>
  string(24) "4cfadede55242b0084000003"
  ["key"]=>
  string(6) "file1"
  ["counter"]=>
  int(1)
}
// document at the beginning
{ "_id": "4cfadede55242b0084000003","key": "file1", "counter": 0 }

// after the calls
{ "_id": "4cfadede55242b0084000003","key": "file1", "counter": 2 }
Comment by Jonathan H. Wage [ 07/Dec/10 ]

Since Doctrine maintains an identity map, the 2nd time it is not refreshing the documents data. Since it already exists in the identity map it simply returns that document. You can tell Doctrine to refresh the data by doing this:

$pid = $dm->createQueryBuilder('downloads')
    ->findAndUpdate()
    ->returnNew(true)
    ->refresh(true)
    ->field('key')->equals('file1')
    ->field('counter')->inc(1)
    ->getQuery()->execute();
Comment by Başar Aykut [ 09/Dec/10 ]

I don't see any function called refresh? Are you going to add it? I think it would be better to return refreshed data automatically for the findAndUpdate command.

Comment by Jonathan H. Wage [ 13/Dec/10 ]

I added it already to the latest version in git.

Generated at Wed Dec 17 20:03:04 UTC 2014 using JIRA 6.2.3#6260-sha1:63ef1d6dac3f4f4d7db4c1effd405ba38ccdc558.