Doctrine MongoDB ODM
  1. Doctrine MongoDB ODM
  2. MODM-97

FindAndModify does not return an object

    Details

    • Type: Bug Bug
    • Status: Resolved
    • Priority: Major Major
    • Resolution: Fixed
    • Affects Version/s: 1.0.0BETA1
    • Fix Version/s: 1.0.0BETA2
    • Component/s: Document Manager
    • 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

        Activity

        Hide
        Jonathan H. Wage added a comment -

        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();
        
        Show
        Jonathan H. Wage added a comment - 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();
        Hide
        Başar Aykut added a comment -

        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)
          }
        }
        
        Show
        Başar Aykut added a comment - 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) } }
        Hide
        Jonathan H. Wage added a comment -

        Are you using the latest version of everything?

        Show
        Jonathan H. Wage added a comment - Are you using the latest version of everything?
        Hide
        Başar Aykut added a comment -

        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();
        
        Show
        Başar Aykut added a comment - 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();
        Hide
        Jonathan H. Wage added a comment -

        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.

        Show
        Jonathan H. Wage added a comment - 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.
        Hide
        Başar Aykut added a comment -

        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 }
        
        Show
        Başar Aykut added a comment - 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 }
        Hide
        Jonathan H. Wage added a comment -

        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();
        
        Show
        Jonathan H. Wage added a comment - 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();
        Hide
        Başar Aykut added a comment -

        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.

        Show
        Başar Aykut added a comment - 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.
        Hide
        Jonathan H. Wage added a comment -

        I added it already to the latest version in git.

        Show
        Jonathan H. Wage added a comment - I added it already to the latest version in git.

          People

          • Assignee:
            Jonathan H. Wage
            Reporter:
            Başar Aykut
          • Votes:
            0 Vote for this issue
            Watchers:
            0 Start watching this issue

            Dates

            • Created:
              Updated:
              Resolved: