From c0dbd87f7a5650921154165c5541440c5b5c082c Mon Sep 17 00:00:00 2001 From: evgen-d Date: Tue, 5 Aug 2014 12:43:24 +0400 Subject: [PATCH] Query::prepareResult --- ActiveRecord.php | 12 +++++++++--- Query.php | 30 +++++++++++++++++++----------- 2 files changed, 28 insertions(+), 14 deletions(-) diff --git a/ActiveRecord.php b/ActiveRecord.php index 5a15646..07797f3 100644 --- a/ActiveRecord.php +++ b/ActiveRecord.php @@ -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() diff --git a/Query.php b/Query.php index ed62e36..5ba099e 100644 --- a/Query.php +++ b/Query.php @@ -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; }