improved MongoId processing and $fields to MongoModel::find()
This commit is contained in:
@ -13,23 +13,40 @@
|
|||||||
abstract class MongoModel extends Model
|
abstract class MongoModel extends Model
|
||||||
{
|
{
|
||||||
|
|
||||||
|
protected $useMongoId = true;
|
||||||
|
|
||||||
|
public function __construct($connection = 'default')
|
||||||
|
{
|
||||||
|
parent::__construct($connection);
|
||||||
|
}
|
||||||
|
|
||||||
public function count($query = array(), $limit = 0, $skip = 0)
|
public function count($query = array(), $limit = 0, $skip = 0)
|
||||||
{
|
{
|
||||||
return $this->db->count($this->table(), $query, $limit, $skip);
|
return $this->db->count($this->table(), $query, $limit, $skip);
|
||||||
}
|
}
|
||||||
|
|
||||||
public function find($condition = array())
|
public function find($condition = array(), $fields = array())
|
||||||
{
|
{
|
||||||
return $this->db->find($this->table(), $condition);
|
return $this->db->find($this->table(), $condition, $fields);
|
||||||
}
|
}
|
||||||
|
|
||||||
public function get($id)
|
public function get($id)
|
||||||
{
|
{
|
||||||
|
if($this->useMongoId) {
|
||||||
|
if(! $id instanceof MongoId) {
|
||||||
|
$id = new MongoId($id);
|
||||||
|
}
|
||||||
|
}
|
||||||
return $this->db->get($this->table(), array('_id' => $id))->fetch();
|
return $this->db->get($this->table(), array('_id' => $id))->fetch();
|
||||||
}
|
}
|
||||||
|
|
||||||
public function delete($id)
|
public function delete($id)
|
||||||
{
|
{
|
||||||
|
if($this->useMongoId) {
|
||||||
|
if(! $id instanceof MongoId) {
|
||||||
|
$id = new MongoId($id);
|
||||||
|
}
|
||||||
|
}
|
||||||
return $this->db->delete($this->table(), array('_id' => $id));
|
return $this->db->delete($this->table(), array('_id' => $id));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -71,6 +71,10 @@ class MongoModelTest extends PHPUnit_Framework_TestCase
|
|||||||
$this->assertInstanceOf('MongoStatement', $result);
|
$this->assertInstanceOf('MongoStatement', $result);
|
||||||
$this->assertEquals('milk', $result->limit(2)->order(array('name' => -1))->fetch()->name);
|
$this->assertEquals('milk', $result->limit(2)->order(array('name' => -1))->fetch()->name);
|
||||||
$this->assertEquals('fish', $result->fetch()->name);
|
$this->assertEquals('fish', $result->fetch()->name);
|
||||||
|
$result = $this->model->find(array(), array('name'))->fetch();
|
||||||
|
$this->assertSame('bread', $result->name);
|
||||||
|
$this->setExpectedException('PHPUnit_Framework_Error');
|
||||||
|
$this->assertNull($result->price);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -173,11 +177,66 @@ class MongoModelTest extends PHPUnit_Framework_TestCase
|
|||||||
$this->assertEquals('eggs', $result[0]->name);
|
$this->assertEquals('eggs', $result[0]->name);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @runInSeparateProcess
|
||||||
|
* @group Mongo
|
||||||
|
*/
|
||||||
|
public function testUseMongoId()
|
||||||
|
{
|
||||||
|
if (!defined('DEBUG')) {
|
||||||
|
define('DEBUG', false);
|
||||||
|
}
|
||||||
|
|
||||||
|
$this->assertAttributeEquals(true, 'useMongoId', $this->model);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @runInSeparateProcess
|
||||||
|
* @group Mongo
|
||||||
|
*/
|
||||||
|
public function testId()
|
||||||
|
{
|
||||||
|
if (!defined('DEBUG')) {
|
||||||
|
define('DEBUG', false);
|
||||||
|
}
|
||||||
|
$class = new ReflectionClass('MongoModel');
|
||||||
|
$prop = $class->getProperty('useMongoId');
|
||||||
|
$prop->setAccessible(true);
|
||||||
|
|
||||||
|
$this->model->insert(array('_id' => 1, 'name' => 'testbread', 'price' => 3.2, 'quantity' => 10));
|
||||||
|
$this->model->insert(array('_id' => 2, 'name' => 'testbread', 'price' => 12, 'quantity' => 2));
|
||||||
|
$this->assertSame(2, $this->model->count(array('name' => 'testbread')));
|
||||||
|
|
||||||
|
$prop->setValue($this->model, false);
|
||||||
|
$this->model->delete(1);
|
||||||
|
$this->assertSame(1, $this->model->count(array('name' => 'testbread')));
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @runInSeparateProcess
|
||||||
|
* @group Mongo
|
||||||
|
*/
|
||||||
|
public function testIdToMongoId()
|
||||||
|
{
|
||||||
|
if (!defined('DEBUG')) {
|
||||||
|
define('DEBUG', false);
|
||||||
|
}
|
||||||
|
|
||||||
|
$this->model->insert(array('name' => 'testbread', 'price' => 3.2, 'quantity' => 10));
|
||||||
|
$this->model->insert(array('name' => 'testbread', 'price' => 12, 'quantity' => 2));
|
||||||
|
$this->assertSame(2, $this->model->count(array('name' => 'testbread')));
|
||||||
|
$id = $this->model->find(array('name' => 'testbread'))->limit(1)->fetch()->_id->__toString();
|
||||||
|
$this->assertInternalType('string', $id);
|
||||||
|
$item = $this->model->get($id);
|
||||||
|
$this->assertSame('testbread', $item->name);
|
||||||
|
$this->model->delete($id);
|
||||||
|
$this->assertSame(1, $this->model->count(array('name' => 'testbread')));
|
||||||
|
}
|
||||||
|
|
||||||
public function tearDown()
|
public function tearDown()
|
||||||
{
|
{
|
||||||
$conf = array('driver' => 'MongoDriver', 'hostname' => 'localhost', 'database' => 'test', 'username' => 'test', 'password' => '1234', 'port' => 27017);
|
$conf = array('driver' => 'MongoDriver', 'hostname' => 'localhost', 'database' => 'test', 'username' => 'test', 'password' => '1234', 'port' => 27017);
|
||||||
|
|
||||||
|
|
||||||
$connection = new Mongo('mongodb://' . $conf['hostname'] . ':' . $conf['port']);
|
$connection = new Mongo('mongodb://' . $conf['hostname'] . ':' . $conf['port']);
|
||||||
$db = $connection->selectDB($conf['database']);
|
$db = $connection->selectDB($conf['database']);
|
||||||
$db->authenticate($conf['username'], $conf['password']);
|
$db->authenticate($conf['username'], $conf['password']);
|
||||||
@ -234,5 +293,4 @@ class MongoModelTest extends PHPUnit_Framework_TestCase
|
|||||||
$collection->insert($document);
|
$collection->insert($document);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user