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 ActiveRecord $record
* @param Document $row * @param Document|array $row
*/ */
public static function populateRecord($record, $row) public static function populateRecord($record, $row)
{ {
$record->document = $row; if (is_array($row)) {
parent::populateRecord($record, $record->document->getAll()); $document = Document::createFromArray($row);
} else {
$document = $row;
$row = $row->getAll();
}
$record->document = $document;
parent::populateRecord($record, $row);
} }
public function attributes() public function attributes()

View File

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