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
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
......
......@@ -41,8 +41,8 @@ class Query extends Component implements QueryInterface
use QueryTrait;
/**
* @var array the fields of the results to return. For example, `['name', 'group_id']`.
* The "_id" field is always returned. If not set, if means selecting all columns.
* @var array the fields of the results to return. For example: `['name', 'group_id']`, `['name' => true, '_id' => false]`.
* Unless directly excluded, the "_id" field is always returned. If not set, it means selecting all columns.
* @see select()
*/
public $select = [];
......@@ -377,8 +377,12 @@ class Query extends Component implements QueryInterface
{
$selectFields = [];
if (!empty($this->select)) {
foreach ($this->select as $fieldName) {
$selectFields[$fieldName] = true;
foreach ($this->select as $key => $value) {
if (is_numeric($key)) {
$selectFields[$value] = true;
} else {
$selectFields[$key] = $value;
}
}
}
return $selectFields;
......
......@@ -262,4 +262,21 @@ class QueryRunTest extends MongoDbTestCase
$this->assertEquals('name1', $rows[0]['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