1. Jon Langevin
  2. ActiveDocument

Commits

Jon Langevin  committed 085ce7e

Changes to Mongo driver, to allow for non-string PKs, and ensuring that MongoId values are handled properly

  • Participants
  • Parent commits 137b6d9
  • Branches default

Comments (0)

Files changed (3)

File drivers/mongo/Adapter.php

View file
 
         $objects = array();
         if ($info = $cursor->info()) {
+            \Yii::trace('Mongo Find query: ' . \CVarDumper::dumpAsString($info), 'ext.activedocument.drivers.mongo.Adapter');
             $container = array_pop(explode('.', $info['ns']));
             iterator_apply($cursor, array($this, 'iterateObjects'), array($cursor, &$objects, $container));
         }
     }
 
     protected function iterateObjects(\MongoCursor $cursor, &$objects, $container) {
-        array_push($objects, $this->populateObject($container, $cursor->key(), $cursor->current()));
+        $current = $cursor->current();
+        array_push($objects, $this->populateObject($container, $current['_id'], $current));
         return true;
     }
 
                 if ($input['container'] == $collection && !empty($input['key'])) {
                     if (!isset($query['_id']))
                         $query['_id'] = array();
-                    $query['_id'][] = $input['key'];
+                    $query['_id'][] = Object::properId($input['key']);
                 }
             }
         }

File drivers/mongo/Container.php

View file
 
     public function deleteKeys(array $keys) {
         try {
-            $this->_containerInstance->remove(array('_id'=>array('$in'=>$keys)), array('safe'=>true));
+            $this->_containerInstance->remove(array('_id'=>array('$in'=>array_map(array('Object', 'properId'), $keys))), array('safe'=>true));
         }catch(\MongoException $e) {
             /**
              * @todo Throw custom exception

File drivers/mongo/Object.php

View file
 
     protected function loadObjectInstance($new = true) {
         $data = null;
-        if ($this->getKey() !== null && !$new)
+        if ($this->getKey() !== null && !$new) {
+            \Yii::trace('Mongo FindByPk query: ' . \CVarDumper::dumpAsString($this->getKey()), 'ext.activedocument.drivers.mongo.Object');
             $data = $this->_container->getContainerInstance()->findOne(array('_id' => $this->getKey()));
+        }
         if ($data == null)
             $data = array();
         return new \ArrayObject($data, \ArrayObject::ARRAY_AS_PROPS);
             $key = $this->_objectInstance->_id;
         else
             $key = parent::getKey();
-        $key = $this->properId($key);
+        $key = self::properId($key);
+        \Yii::trace('Mongo getKey(): ' . \CVarDumper::dumpAsString($key), 'ext.activedocument.drivers.mongo.Object');
 
         return $key;
     }
      * @param string|\MongoId $value
      */
     public function setKey($value) {
-        $value = $this->properId($value);
+        $value = self::properId($value);
+        \Yii::trace('Mongo setKey(): ' . \CVarDumper::dumpAsString($value), 'ext.activedocument.drivers.mongo.Object');
         if($this->_objectInstance instanceof \ArrayObject) {
             $this->_objectInstance->_id = $value;
         }
      * @param mixed $id
      * @return \MongoId|mixed
      */
-    protected function properId($id) {
-        if (is_string($id) && ($mId = new \MongoId($id)) && $id === (string) $mId)
+    public static function properId($id) {
+        if ($id === null)
+            return $id;
+        if (is_array($id) && isset($id['$id']))
+            return new \MongoId($id['$id']);
+        elseif (is_string($id) && ($mId = new \MongoId($id)) && $id === (string) $mId)
             return $mId;
         return $id;
     }