Query::prepareResult

This commit is contained in:
evgen-d 2014-08-05 12:43:24 +04:00
parent 2e208b6f88
commit c0dbd87f7a
2 changed files with 28 additions and 14 deletions

View File

@ -133,12 +133,18 @@ abstract class ActiveRecord extends BaseActiveRecord
/**
* @param ActiveRecord $record
* @param Document $row
* @param Document|array $row
*/
public static function populateRecord($record, $row)
{
$record->document = $row;
parent::populateRecord($record, $record->document->getAll());
if (is_array($row)) {
$document = Document::createFromArray($row);
} else {
$document = $row;
$row = $row->getAll();
}
$record->document = $document;
parent::populateRecord($record, $row);
}
public function attributes()

View File

@ -2,6 +2,7 @@
namespace devgroup\arangodb;
use triagens\ArangoDb\Document;
use Yii;
use yii\base\Component;
use yii\base\InvalidParamException;
@ -500,7 +501,7 @@ class Query extends Component implements QueryInterface
Yii::endProfile($token, 'devgroup\arangodb\Query::query');
throw new \Exception($ex->getMessage(), (int) $ex->getCode(), $ex);
}
return $cursor->getAll();
return $this->prepareResult($cursor->getAll());
}
public function one($db = null)
@ -517,23 +518,30 @@ class Query extends Component implements QueryInterface
Yii::endProfile($token, 'devgroup\arangodb\Query::query');
throw new \Exception($ex->getMessage(), (int) $ex->getCode(), $ex);
}
$result = $cursor->getAll();
$result = $this->prepareResult($cursor->getAll());
return empty($result) ? false : $result[0];
}
/**
* @param Document[] $rows
* @return array
*/
public function prepareResult($rows)
{
if ($this->indexBy === null) {
return $rows;
}
$result = [];
foreach ($rows as $row) {
if (is_string($this->indexBy)) {
$key = $row[$this->indexBy];
} else {
$key = call_user_func($this->indexBy, $row);
if ($this->indexBy === null) {
foreach ($rows as $row) {
$result[] = $row->getAll();
}
} else {
foreach ($rows as $row) {
if (is_string($this->indexBy)) {
$key = $row->{$this->indexBy};
} else {
$key = call_user_func($this->indexBy, $row);
}
$result[$key] = $row->getAll();
}
$result[$key] = $row;
}
return $result;
}