modified model classes with tests

This commit is contained in:
Anton Grebnev
2011-11-17 15:12:47 +04:00
parent e740ae7000
commit 7e8fe0f94d
10 changed files with 212 additions and 85 deletions

View File

@ -92,9 +92,13 @@ class InsertMongoCommand extends MongoDbCommand
protected $safe = true;
protected $insertId = false;
protected function concreteExecute()
{
return $this->collection->insert($this->data, array('safe' => $this->safe));
$result = $this->collection->insert($this->data, array('safe' => $this->safe));
$this->insertId = $this->data['_id'];
return $result;
}
protected function checkParams()
@ -105,6 +109,11 @@ class InsertMongoCommand extends MongoDbCommand
return false;
}
}
public function getInsertId()
{
return $this->insertId;
}
}
class UpdateMongoCommand extends MongoDbCommand

View File

@ -55,8 +55,9 @@ class MongoDriver extends NoSqlDbDriver
'data' => $data,
'safe' => $safe
);
return $this->query($command, $params)->affectedRows();
$result = $this->query($command, $params);
$this->last_insert_id = $result->getInsertId();
return $result->affectedRows();
}
public function update($collection, $data, $condition = array(), $multiple = true, $upsert = false, $safe = true)

View File

@ -13,26 +13,11 @@
abstract class MongoModel extends Model
{
public function find($condition)
public function find($condition = array())
{
return $this->db->find($this->table(), $condition);
}
protected function order(MongoStatement $cursor, $sort = array())
{
return $cursor->sort($sort);
}
protected function skip(MongoStatement $cursor, $skip = 0)
{
return $cursor->skip($skip);
}
protected function limit(MongoStatement $cursor, $limit = 0)
{
return $cursor->limit($limit);
}
public function get($id)
{
return $this->db->get($this->table(), array('_id' => $id))->fetch();
@ -40,7 +25,7 @@ abstract class MongoModel extends Model
public function delete($id)
{
return $this->db->delete($this->table(), array('id' => $id));
return $this->db->delete($this->table(), array('_id' => $id));
}
protected function fetchField($data, $params = array(), $field, $cache_key = null)

View File

@ -14,6 +14,8 @@
class MongoStatement extends DbStatement
{
protected $insertId = false;
public function order($sort = array())
{
if ($this->result instanceof MongoCursor) {
@ -133,6 +135,9 @@ class MongoStatement extends DbStatement
if ($result instanceof MongoCursor || is_array($result)) {
$this->result = $result;
}
if($request instanceof InsertMongoCommand) {
$this->insertId = $request->getInsertId();
}
return true;
} else {
throw new Exception('No connection to MongoDB server.');
@ -148,4 +153,9 @@ class MongoStatement extends DbStatement
{
return $this->request;
}
public function getInsertId()
{
return $this->insertId;
}
}