We have moved our forum to GitHub Discussions. For questions about Phalcon v3/v4/v5 you can visit here and for Phalcon v6 here.

Invalid Object ID when deleting document with MongoDB

I am having a problem deleting a document in the ORM using MongoDB. I am getting "Invalid object ID " error when the document I am deleting is the result of a find(). Can anyone point out what I am missing?

TIA

here is the code

//database

> db.token.find();
{ "_id" : "2086393e8f97719b487b998c854ba1e1e6030a8e", "lastModified" : "1359673539", "userid" : null }
{ "_id" : "8118d6ab012afdd2c5c544de0ca7c7675921b9de", "lastModified" : "1359673580", "userid" : "17" }
{ "_id" : "1c50fee11df5161eb5e9b51f3dfe1a8d3494ad77", "lastModified" : "1359673847", "userid" : "19" }
{ "_id" : "7e834c85fc00d09e586213d3d3871e1d3d0458bd", "lastModified" : "1359726778", "userid" : "20" }
{ "_id" : "5c448b1cfc7af02997f0f9878fc599a1b6bb6b6e", "lastModified" : "1360008367", "userid" : "21" }
{ "_id" : "0fe50f39de3db3f7e13255132fb4e6dc7e8565fc", "lastModified" : "1360164251", "userid" : "23" }
{ "_id" : "2bc8d34ac271f7a0a0a870b2d1aaa0be51185cd7", "lastModified" : "1360164278", "userid" : "24" }
{ "_id" : "f7e77d41ac0303120c1e0a22be5b3ea48cdd269c", "lastModified" : "1360183590", "userid" : "25" }
{ "_id" : "d74660cd6741755313e12863c55e1267f5c81dac", "lastModified" : "1360184019", "userid" : "18" }
{ "_id" : "bf3a56662738c93aa0423c93d62020fab49e16dc", "lastModified" : "1362753663", "userid" : "22" }
{ "_id" : "d303f6031afe28cfbe1300e31d206b758c54802f", "lastModified" : "1362754057", "userid" : "26" }
{ "_id" : "3c748b11dbddefde458e53ea70ec44391c3dd523", "lastModified" : "1363892146", "userid" : "27" }
{ "_id" : "5840c74d4054a30d75b286d4e0307da3762cb887", "lastModified" : "1364229333", "userid" : "29" }
{ "_id" : "c5ba0092d0bd251245fc1d70748af421eb5ddf7f", "lastModified" : "1364401818", "userid" : "28" }

//index.php

    $mongoClient = new \MongoClient($config->mongo->server);
    $mongo = new MongoDB($mongoClient , $config->mongo->db);
    $di->set('mongo' , $mongo);

//model Token.php

class Token extends Collection { 

... 
     */
    public function deleteToken($token){

        $ret = false ;
        $arr = array( 'conditions' => array('_id' => $token ));

        try {
        $token = $this->findFirst($arr);
        $token->delete();
        $ret = true ;
        }
        catch (Exception $e){
            var_dump($e->getTrace());
            var_dump($e->getCode());
            var_dump($e->getMessage());
            $ret = false; 
        }

        return $ret ;

    }

//IndexController.php


class IndexController extends AbstractController  { 

    public function indexAction(){

        $token = new Token();
        var_dump($token->deleteToken("2086393e8f97719b487b998c854ba1e1e6030a8e"));

    }

//results

object(Token)[60]
  public '_id' => string '2086393e8f97719b487b998c854ba1e1e6030a8e' (length=40)
  protected '_dependencyInjector' => 
    object(Phalcon\DI\FactoryDefault)[7]
      protected '_services' => 
        array (size=25)
          'router' => 
            object(Phalcon\DI\Service)[42]
              ...
          'dispatcher' => 
            object(Phalcon\DI\Service)[9]
              ...
          'url' => 
            object(Phalcon\DI\Service)[10]
              ...
          'modelsManager' => 
            object(Phalcon\DI\Service)[11]
              ...
          'modelsMetadata' => 
            object(Phalcon\DI\Service)[12]
              ...
          'response' => 
            object(Phalcon\DI\Service)[13]
              ...
          'cookies' => 
            object(Phalcon\DI\Service)[14]
              ...
          'request' => 
            object(Phalcon\DI\Service)[15]
              ...
          'filter' => 
            object(Phalcon\DI\Service)[16]
              ...
          'escaper' => 
            object(Phalcon\DI\Service)[17]
              ...
          'security' => 
            object(Phalcon\DI\Service)[19]
              ...
          'crypt' => 
            object(Phalcon\DI\Service)[20]
              ...
          'annotations' => 
            object(Phalcon\DI\Service)[18]
              ...
          'flash' => 
            object(Phalcon\DI\Service)[21]
              ...
          'flashSession' => 
            object(Phalcon\DI\Service)[22]
              ...
          'session' => 
            object(Phalcon\DI\Service)[31]
              ...
          'sessionBag' => 
            object(Phalcon\DI\Service)[24]
              ...
          'eventsManager' => 
            object(Phalcon\DI\Service)[25]
              ...
          'transactionManager' => 
            object(Phalcon\DI\Service)[26]
              ...
          'assets' => 
            object(Phalcon\DI\Service)[27]
              ...
          'view' => 
            object(Phalcon\DI\Service)[29]
              ...
          'config' => 
            object(Phalcon\DI\Service)[23]
              ...
          'db' => 
            object(Phalcon\DI\Service)[35]
              ...
          'mongo' => 
            object(Phalcon\DI\Service)[38]
              ...
          'collectionManager' => 
            object(Phalcon\DI\Service)[40]
              ...
      protected '_sharedInstances' => 
        array (size=6)
          'router' => 
            object(Phalcon\Mvc\Router)[43]
              ...
          'view' => 
            object(Phalcon\Mvc\View)[28]
              ...
          'dispatcher' => 
            object(Phalcon\Mvc\Dispatcher)[51]
              ...
          'IndexController' => 
            object(IndexController)[52]
              ...
          'collectionManager' => 
            object(Phalcon\Mvc\Collection\Manager)[55]
              ...
          'mongo' => 
            object(MongoDB)[37]
              ...
      protected '_freshInstance' => boolean true
  protected '_modelsManager' => 
    object(Phalcon\Mvc\Collection\Manager)[55]
      protected '_dependencyInjector' => 
        object(Phalcon\DI\FactoryDefault)[7]
          protected '_services' => 
            array (size=25)
              ...
          protected '_sharedInstances' => 
            array (size=6)
              ...
          protected '_freshInstance' => boolean true
      protected '_initialized' => 
        array (size=1)
          'token' => 
            object(Token)[53]
              ...
      protected '_lastInitialized' => 
        object(Token)[53]
          public '_id' => null
          protected '_dependencyInjector' => 
            object(Phalcon\DI\FactoryDefault)[7]
              ...
          protected '_modelsManager' => 
            &object(Phalcon\Mvc\Collection\Manager)[55]
          protected '_source' => null
          protected '_operationMade' => int 0
          protected '_connection' => null
          protected '_errorMessages' => null
      protected '_eventsManager' => 
        object(Phalcon\Events\Manager)[54]
          protected '_events' => null
          protected '_collect' => boolean false
          protected '_enablePriorities' => boolean false
          protected '_responses' => null
      protected '_customEventsManager' => null
      protected '_connectionServices' => null
      protected '_implicitObjectsIds' => null
  protected '_source' => string 'token' (length=5)
  protected '_operationMade' => int 0
  protected '_connection' => 
    object(MongoDB)[37]
      public 'w' => int 1
      public 'wtimeout' => int 10000
  protected '_errorMessages' => null
  public 'lastModified' => string '1359673539' (length=10)
  public 'userid' => null
array (size=6)
  0 => 
    array (size=4)
      'function' => string '__construct' (length=11)
      'class' => string 'MongoId' (length=7)
      'type' => string '->' (length=2)
      'args' => 
        array (size=1)
          0 => string '2086393e8f97719b487b998c854ba1e1e6030a8e' (length=40)
  1 => 
    array (size=6)
      'file' => string '/home/rwilbert/workspace/kd7/application_server/phalcon/models/Token.php' (length=72)
      'line' => int 73
      'function' => string 'delete' (length=6)
      'class' => string 'Phalcon\Mvc\Collection' (length=22)
      'type' => string '->' (length=2)
      'args' => 
        array (size=0)
          empty
  2 => 
    array (size=6)
      'file' => string '/home/rwilbert/workspace/kd7/application_server/phalcon/controllers/IndexController.php' (length=87)
      'line' => int 9
      'function' => string 'deleteToken' (length=11)
      'class' => string 'Token' (length=5)
      'type' => string '->' (length=2)
      'args' => 
        array (size=1)
          0 => &string '2086393e8f97719b487b998c854ba1e1e6030a8e' (length=40)
  3 => 
    array (size=4)
      'function' => string 'indexAction' (length=11)
      'class' => string 'IndexController' (length=15)
      'type' => string '->' (length=2)
      'args' => 
        array (size=0)
          empty
  4 => 
    array (size=4)
      'function' => string 'dispatch' (length=8)
      'class' => string 'Phalcon\Dispatcher' (length=18)
      'type' => string '->' (length=2)
      'args' => 
        array (size=0)
          empty
  5 => 
    array (size=6)
      'file' => string '/home/rwilbert/workspace/kd7/application_server/public/index.php' (length=64)
      'line' => int 89
      'function' => string 'handle' (length=6)
      'class' => string 'Phalcon\Mvc\Application' (length=23)
      'type' => string '->' (length=2)
      'args' => 
        array (size=0)
          empty
int 19
string 'Invalid object ID' (length=17)
boolean false


98.9k

Try using a MongoId instead of an id as string:

$arr = array( 'conditions' => array('_id' => new MongoId($token)));


6.9k

Thanks for the reply.

I think I see this may be an issue with the Mongo ORM. But, I am not sure that the problem is with Phalcon or the PECL::MongoDB library.

I think there is a couple of things I left out: I am using custom _id values This is a small existing ZF2 app that uses the PECL::MongoDB libraries.

As to your reply: First I don't understand how setting the type of the _id value to MongoId in the query would affect the _id in the return set. I think you should be able to query on any field and call delete or am I misunderstanding the docs?

But I did find out that calling new MongoId('any string that is not 24 hex characters') with the PECL::MongoDB library throws an "Invalid Object Id" exception. This happens for 1.4.1 and with 1.3.3 it will just generate a ObjectId Object with different value.

So I am thinking that when I call ->delete() on the result object somewhere in the ORM it is converting the value of _id to an ObjectId hence giving me the error.

If am right.

How can I use the ORM with custom ids?



6.9k

Yes that is it. Sorry for asking a simple question in a complicated way.