/** * load field data * */ function doLoad() { $collection = $this->_mongodb->selectCollection($this->collection); $id = xn("id"); $field = xn("field"); $type = "integer"; $data = null; if ($id) { $one = $collection->findOne(array("_id" => rock_real_id($id))); //not select field, because there is a bug in list, such as "list.0" $data = rock_array_get($one, $field); switch (gettype($data)) { case "boolean": $type = "boolean"; break; case "integer": $type = "integer"; break; case "long": $type = "long"; break; case "float": case "double": $type = "double"; break; case "string": $type = "string"; break; case "array": $type = "mixed"; break; case "object": // int64 is returned as object (Kyryl Bilokurov <*****@*****.**>) if (get_class($data) == "MongoInt64") { $type = "long"; } else { $type = "mixed"; } break; case "resource": $type = "mixed"; break; case "NULL": $type = "null"; break; } } $exporter = new VarExportor($this->_mongodb, $data); $format = rock_cookie("rock_format", "json"); $represent = $exporter->export($format); if ($format == "json") { $represent = json_unicode_to_utf8($represent); } $this->_outputJson(array("code" => 200, "type" => $type, "value" => $type == "long" ? $data->__toString() : $data, "represent" => $represent, "format" => $format)); }
/** modify one row **/ public function doModifyRow() { $this->db = xn("db"); $this->collection = xn("collection"); $id = rock_real_id(xn("id")); //selected format last time $this->last_format = rock_cookie("rock_format", "json"); import("lib.mongo.RQuery"); $query = new RQuery($this->_mongo, $this->db, $this->collection); $this->row = $query->id($id)->findOne(); if (empty($this->row)) { $this->error = "Record is not found."; $this->display(); return; } $this->data = $this->row; unset($this->data["_id"]); import("classes.VarExportor"); $export = new VarExportor($query->db(), $this->data); $this->data = $export->export($this->last_format); if ($this->last_format == "json") { $this->data = json_unicode_to_utf8($this->data); } if ($this->isPost()) { $this->data = xn("data"); $format = x("format"); $this->last_format = $format; $row = null; $eval = new VarEval($this->data, $format, $this->_mongo->selectDb($this->db)); $row = $eval->execute(); if ($row === false || !is_array($row)) { $this->error = "Only valid {$format} is accepted."; $this->display(); return; } $query = new RQuery($this->_mongo, $this->db, $this->collection); $obj = $query->id($this->row["_id"])->find(); $oldAttrs = $obj->attrs(); $obj->setAttrs($row); foreach ($oldAttrs as $oldAttr => $oldValue) { if ($oldAttr == "_id") { continue; } if (!array_key_exists($oldAttr, $row)) { $obj->remove($oldAttr); } } try { $obj->save(); } catch (Exception $e) { $this->error = $e->getMessage(); $this->display(); return; } //remember format choice $this->_rememberFormat($format); $this->message = "Updated successfully."; } $this->display(); }