public function testAll() { $query = new Query(); $query->from('yiitest', 'user'); $results = $query->all($this->getConnection()); $this->assertEquals(4, count($results)); $result = reset($results); $this->assertEquals(3, count($result['_source'])); $this->assertArrayHasKey('status', $result['_source']); $this->assertArrayHasKey('email', $result['_source']); $this->assertArrayHasKey('name', $result['_source']); $this->assertArrayHasKey('_id', $result); $query = new Query(); $query->from('yiitest', 'user'); $results = $query->where(['name' => 'user1'])->all($this->getConnection()); $this->assertEquals(1, count($results)); $result = reset($results); $this->assertEquals(3, count($result['_source'])); $this->assertArrayHasKey('status', $result['_source']); $this->assertArrayHasKey('email', $result['_source']); $this->assertArrayHasKey('name', $result['_source']); $this->assertArrayHasKey('_id', $result); $this->assertEquals(1, $result['_id']); // indexBy $query = new Query(); $query->from('yiitest', 'user'); $results = $query->indexBy('name')->all($this->getConnection()); $this->assertEquals(4, count($results)); ksort($results); $this->assertEquals(['user1', 'user2', 'user3', 'user4'], array_keys($results)); }
/** * Executes query and returns all results as an array. * @param Connection $db the DB connection used to create the DB command. * If null, the DB connection returned by [[modelClass]] will be used. * @return array the query results. If the query results in nothing, an empty array will be returned. */ public function all($db = null) { if ($this->asArray) { // TODO implement with return parent::all($db); } $result = $this->createCommand($db)->search(); if (empty($result['hits']['hits'])) { return []; } $models = $this->createModels($result['hits']['hits']); if (!empty($this->with)) { $this->findWith($this->with, $models); } foreach ($models as $model) { $model->afterFind(); } return $models; }
/** * Executes query and returns all results as an array. * @param Connection $db the DB connection used to create the DB command. * If null, the DB connection returned by [[modelClass]] will be used. * @return array the query results. If the query results in nothing, an empty array will be returned. */ public function all($db = null) { return parent::all($db); }