Browse Source

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

tags/2.0.3
Klimov Paul 10 years ago
parent
commit
6e50783ea4
  1. 2
      extensions/mongodb/CHANGELOG.md
  2. 12
      extensions/mongodb/Query.php
  3. 17
      tests/unit/extensions/mongodb/QueryRunTest.php

2
extensions/mongodb/CHANGELOG.md

@ -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

12
extensions/mongodb/Query.php

@ -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
{ {
$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;

17
tests/unit/extensions/mongodb/QueryRunTest.php

@ -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);
}
} }

Loading…
Cancel
Save