added count() method to MongoModel
This commit is contained in:
@ -12,6 +12,8 @@ class MongoCommandBuilder
|
||||
|
||||
const FIND = 'Find';
|
||||
|
||||
const COUNT = 'Count';
|
||||
|
||||
const INSERT = 'Insert';
|
||||
|
||||
const UPDATE = 'Update';
|
||||
@ -68,9 +70,9 @@ abstract class MongoDbCommand
|
||||
|
||||
class FindMongoCommand extends MongoDbCommand
|
||||
{
|
||||
protected $condition;
|
||||
protected $condition = array();
|
||||
|
||||
protected $fields;
|
||||
protected $fields = array();
|
||||
|
||||
protected $multiple = true;
|
||||
|
||||
@ -100,6 +102,7 @@ class FindMongoCommand extends MongoDbCommand
|
||||
var_dump($this->condition);
|
||||
$condition = ob_get_clean();
|
||||
$result .= 'Condition: ' . $condition . PHP_EOL;
|
||||
$result .= 'Type: FIND' . PHP_EOL;
|
||||
ob_start();
|
||||
var_dump($this->fields);
|
||||
$fields = ob_get_clean();
|
||||
@ -113,6 +116,48 @@ class FindMongoCommand extends MongoDbCommand
|
||||
}
|
||||
}
|
||||
|
||||
class CountMongoCommand extends MongoDbCommand
|
||||
{
|
||||
protected $condition = array();
|
||||
|
||||
protected $limit = 0;
|
||||
|
||||
protected $skip = 0;
|
||||
|
||||
protected $multiple = true;
|
||||
|
||||
protected function concreteExecute()
|
||||
{
|
||||
return $this->collection->count($this->condition, $this->limit, $this->skip);
|
||||
}
|
||||
|
||||
protected function checkParams()
|
||||
{
|
||||
if (isset($this->collection) && isset($this->condition) && isset($this->limit) && isset($this->skip)) {
|
||||
return true;
|
||||
} else {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
public function __toString()
|
||||
{
|
||||
if ($this->checkParams()) {
|
||||
$result = 'Collection: ' . $this->collection . PHP_EOL;
|
||||
$result .= 'Type: COUNT' . PHP_EOL;
|
||||
ob_start();
|
||||
var_dump($this->condition);
|
||||
$condition = ob_get_clean();
|
||||
$result .= 'Condition: ' . $condition . PHP_EOL;
|
||||
$result .= 'Limit: ' . $this->limit . PHP_EOL;
|
||||
$result .= 'Skip: ' . $this->skip . PHP_EOL;
|
||||
return $result;
|
||||
} else {
|
||||
return 'Command properties not set';
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
class InsertMongoCommand extends MongoDbCommand
|
||||
{
|
||||
protected $data;
|
||||
@ -155,6 +200,7 @@ class InsertMongoCommand extends MongoDbCommand
|
||||
{
|
||||
if ($this->checkParams()) {
|
||||
$result = 'Collection: ' . $this->collection . PHP_EOL;
|
||||
$result .= 'Type: INSERT' . PHP_EOL;
|
||||
ob_start();
|
||||
var_dump($this->data);
|
||||
$data = ob_get_clean();
|
||||
@ -203,6 +249,7 @@ class UpdateMongoCommand extends MongoDbCommand
|
||||
{
|
||||
if ($this->checkParams()) {
|
||||
$result = 'Collection: ' . $this->collection . PHP_EOL;
|
||||
$result .= 'Type: UPDATE' . PHP_EOL;
|
||||
ob_start();
|
||||
var_dump($this->condition);
|
||||
$condition = ob_get_clean();
|
||||
@ -248,6 +295,7 @@ class RemoveMongoCommand extends MongoDbCommand
|
||||
{
|
||||
if ($this->checkParams()) {
|
||||
$result = 'Collection: ' . $this->collection . PHP_EOL;
|
||||
$result .= 'Type: REMOVE' . PHP_EOL;
|
||||
ob_start();
|
||||
var_dump($this->condition);
|
||||
$condition = ob_get_clean();
|
||||
@ -288,6 +336,7 @@ class CommandMongoCommand extends MongoDbCommand
|
||||
{
|
||||
if ($this->checkParams()) {
|
||||
$result = 'Collection: ' . $this->collection . PHP_EOL;
|
||||
$result .= 'Type: COMMAND' . PHP_EOL;
|
||||
ob_start();
|
||||
var_dump($this->command);
|
||||
$command = ob_get_clean();
|
||||
|
@ -28,7 +28,14 @@ class MongoDriver extends NoSqlDbDriver
|
||||
|
||||
public function count($collection, $query = array(), $limit = 0, $skip = 0)
|
||||
{
|
||||
return $this->getCollection($collection)->count($query, $limit, $skip);
|
||||
$command = MongoCommandBuilder::factory(MongoCommandBuilder::COUNT, $this->getCollection($collection));
|
||||
$params = array(
|
||||
'condition' => $query,
|
||||
'limit' => $limit,
|
||||
'skip' => $skip
|
||||
);
|
||||
|
||||
return $this->query($command, $params)->affectedRows();
|
||||
}
|
||||
|
||||
public function find($collection, $condition = array(), $fields = array())
|
||||
|
@ -112,6 +112,8 @@ class MongoStatement extends DbStatement
|
||||
} else {
|
||||
return false;
|
||||
}
|
||||
} elseif (is_int($this->result)) {
|
||||
return $this->result;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
@ -151,6 +153,8 @@ class MongoStatement extends DbStatement
|
||||
if (is_array($result) && isset($result['values'])) {
|
||||
$this->result = $result['values'];
|
||||
}
|
||||
} elseif (is_int($result)) {
|
||||
$this->result = $result;
|
||||
}
|
||||
if ($request instanceof InsertMongoCommand) {
|
||||
$this->insertId = $request->getInsertId();
|
||||
|
Reference in New Issue
Block a user