Commit 6e50783e by Klimov Paul

`yii\mongodb\Query::select` now allows excluding fields

parent e5e48cd4
...@@ -4,7 +4,7 @@ Yii Framework 2 mongodb extension Change Log ...@@ -4,7 +4,7 @@ Yii Framework 2 mongodb extension Change Log
2.0.3 under development 2.0.3 under development
----------------------- -----------------------
- no changes in this release. - Bug #7010: Fixed `yii\mongodb\Query::select` now allows excluding fields (Sammaye, klimov-paul)
2.0.2 January 11, 2015 2.0.2 January 11, 2015
......
...@@ -41,8 +41,8 @@ class Query extends Component implements QueryInterface ...@@ -41,8 +41,8 @@ class Query extends Component implements QueryInterface
use QueryTrait; use QueryTrait;
/** /**
* @var array the fields of the results to return. For example, `['name', 'group_id']`. * @var array the fields of the results to return. For example: `['name', 'group_id']`, `['name' => true, '_id' => false]`.
* The "_id" field is always returned. If not set, if means selecting all columns. * Unless directly excluded, the "_id" field is always returned. If not set, it means selecting all columns.
* @see select() * @see select()
*/ */
public $select = []; public $select = [];
...@@ -377,8 +377,12 @@ class Query extends Component implements QueryInterface ...@@ -377,8 +377,12 @@ class Query extends Component implements QueryInterface
{ {
$selectFields = []; $selectFields = [];
if (!empty($this->select)) { if (!empty($this->select)) {
foreach ($this->select as $fieldName) { foreach ($this->select as $key => $value) {
$selectFields[$fieldName] = true; if (is_numeric($key)) {
$selectFields[$value] = true;
} else {
$selectFields[$key] = $value;
}
} }
} }
return $selectFields; return $selectFields;
......
...@@ -262,4 +262,21 @@ class QueryRunTest extends MongoDbTestCase ...@@ -262,4 +262,21 @@ class QueryRunTest extends MongoDbTestCase
$this->assertEquals('name1', $rows[0]['name']); $this->assertEquals('name1', $rows[0]['name']);
$this->assertEquals('name5', $rows[1]['name']); $this->assertEquals('name5', $rows[1]['name']);
} }
/**
* @see https://github.com/yiisoft/yii2/issues/7010
*/
public function testSelect()
{
$connection = $this->getConnection();
$query = new Query;
$rows = $query->from('customer')
->select(['name' => true, '_id' => false])
->limit(1)
->all($connection);
$row = array_pop($rows);
$this->assertArrayHasKey('name', $row);
$this->assertArrayNotHasKey('address', $row);
$this->assertArrayNotHasKey('_id', $row);
}
} }
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