arango = Instance::ensure($this->arango, \devgroup\arangodb\ArangoDbConnection::className()); if ($this->collection === null) { throw new InvalidConfigException('The "collection" property must be set.'); } } /** * @inheritdoc */ protected function prepareKeys($models) { return array_keys($models); } /** * @inheritdoc */ protected function prepareModels() { // $sql = $this->sql; // $qb = $this->db->getQueryBuilder(); // if (($sort = $this->getSort()) !== false) { // $orderBy = $qb->buildOrderBy($sort->getOrders()); // if (!empty($orderBy)) { // $orderBy = substr($orderBy, 9); // if (preg_match('/\s+order\s+by\s+[\w\s,\.]+$/i', $sql)) { // $sql .= ', ' . $orderBy; // } else { // $sql .= ' ORDER BY ' . $orderBy; // } // } // } // if (($pagination = $this->getPagination()) !== false) { // $pagination->totalCount = $this->getTotalCount(); // $sql .= ' ' . $qb->buildLimit($pagination->getLimit(), $pagination->getOffset()); // } // return $this->db->createCommand($sql, $this->params)->queryAll(); $statement = $this->getBaseStatement(); if (($pagination = $this->getPagination()) !== false) { $pagination->totalCount = $this->getTotalCount(); $statement->setQuery($statement->getQuery() . "\n LIMIT " . $pagination->getOffset() . ", " . $pagination->getLimit()); } $statement->setQuery($statement->getQuery()."\n RETURN a"); $cursor = $statement->execute(); $data = $cursor->getAll(); $result = []; foreach ($data as $doc) { $item = $doc->getAll(); foreach ($item as $k=>$v) { if (is_array($item[$k]) || is_object($item[$k])) { $item[$k] = json_encode($v, true); } } $result[$item['_key']] = $item; } if (is_object($pagination)) { $pagination->totalCount = $cursor->getFullCount(); } return $result; } public function getTotalCount() { $statement = $this->getBaseStatement(); $statement->setQuery($statement->getQuery(). "\n LIMIT 1 \n RETURN a"); $cursor = $statement->execute(); return $cursor->getFullCount(); } private function getBaseStatement() { $query = "FOR a in @@collection\n"; $filter = []; $bindings = ['@collection' => $this->collection]; $counter = 0; foreach ($this->params as $k => $v) { $filter[] = " a.@filter_field_$counter == @filter_value_$counter "; $bindings["filter_field_$counter"] = $k; $bindings["filter_value_$counter"] = $v; $counter++; } if (count($filter)>0){ $query .= "\nFILTER ".implode(" && ", $filter)."\n"; } if ($this->sort) { $query .= "\n SORT a." . $this->sort; } $statement = $this->arango->statement([ 'query' => $query, 'count' => true, 'bindVars' => $bindings, 'fullCount' => true, ]); return $statement; } /** * @inheritdoc */ protected function prepareTotalCount() { return 0; } }