Commit 8a261d03 authored by Hannes Magnusson's avatar Hannes Magnusson

Implement write helper

parent 02db0fc8
<?php
require __DIR__ . "/../src/Collection.php";
$manager = new MongoDB\Manager("mongodb://localhost:27017");
$collection = new MongoDB\Collection($manager, "crud.examples");
$hannes = array(
"name" => "Hannes",
"nick" => "bjori",
"citizen" => "Iceland",
);
$hayley = array(
"name" => "Hayley",
"nick" => "Alien Ninja",
"citizen" => "USA",
);
$jonpall = array(
"name" => "Jon Pall",
"nick" => "unknown",
"citizen" => "Iceland",
);
try {
$hannes_id = $collection->insertOne($hannes);
} catch(Exception $e) {
echo $e->getMessage(), "\n";
exit;
}
try {
$results = $collection->insertMany(array($hayley, $jonpall));
} catch(Exception $e) {
echo $e->getMessage(), "\n";
exit;
}
...@@ -4,6 +4,7 @@ namespace MongoDB; ...@@ -4,6 +4,7 @@ namespace MongoDB;
use MongoDB\Manager; use MongoDB\Manager;
use MongoDB\Query; use MongoDB\Query;
use MongoDB\ReadPreference; use MongoDB\ReadPreference;
use MongoDB\WriteBatch;
class QueryFlags { class QueryFlags {
const TAILABLE_CURSOR = 0x02; const TAILABLE_CURSOR = 0x02;
...@@ -22,6 +23,10 @@ class CursorType { ...@@ -22,6 +23,10 @@ class CursorType {
} }
class Collection { class Collection {
const INSERT = 0x01;
const UPDATE = 0x02;
const DELETE = 0x04;
protected $manager; protected $manager;
protected $rp; protected $rp;
protected $wc; protected $wc;
...@@ -167,6 +172,31 @@ class Collection { ...@@ -167,6 +172,31 @@ class Collection {
return $query; return $query;
} /* }}} */ } /* }}} */
/* }}} */ /* }}} */
}
protected function _writeSingle($filter, $type, array $options = array(), $newobj = array()) { /* {{{ */
$options = array_merge($this->getWriteOptions(), $options);
$batch = new WriteBatch($options["ordered"]);
switch($type) {
case self::INSERT:
$batch->insert($filter);
break;
case self::DELETE:
$batch->delete($filter, $options);
break;
case self::UPDATE:
$batch->update($filter, $newobj, $options);
break;
}
return $this->manager->executeWriteBatch($this->ns, $batch, $this->wc);
} /* }}} */
function getWriteOptions() { /* {{{ */
return array(
"ordered" => false,
);
} /* }}} */
} }
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment