/** * Search for one document/vertex/edge using a full-text search on an attribute of the documents with filtering. If no results are found, null is returned. * A LIMIT 1 is automatically set, so you do not need to set the LIMIT yourself: doc.name = "john" * @param string $type The collection to search in. For graphs, only "vertex" and "edge" are allowed. * @param string $attribute The attribute to search on. * @param string $query The full-text query. * @param string $aql An AQL fragment that will be inserted after the FILTER keyword. * @param array $params An optional associative array containing parameters to bind to the query. * @param string $placeholder Set this to something else if you do not wish to use "doc" to refer to documents in your query. * @return null|AModel */ public function searchForOne($type, $attribute, $query, $aql, $params = array(), $placeholder = "doc") { $collectionParameter = $this->_toolbox->generateBindingParameter('@collection', $params); $attributeParameter = $this->_toolbox->generateBindingParameter('attribute', $params); $queryParameter = $this->_toolbox->generateBindingParameter('query', $params); $aqlStatement = "FOR {$placeholder} in FULLTEXT(@{$collectionParameter}, @{$attributeParameter}, @{$queryParameter}) FILTER " . $aql . " LIMIT 1 return {$placeholder}"; $params[$collectionParameter] = $this->getCollectionName($type); $params[$attributeParameter] = $attribute; $params[$queryParameter] = $query; if ($this->_toolbox->getTransactionManager()->hasTransaction()) { $this->_toolbox->getTransactionManager()->addReadCollection($type); $statement = json_encode(array('query' => $aqlStatement, 'bindVars' => $params), JSON_FORCE_OBJECT); $this->_toolbox->getTransactionManager()->addCommand("function () {var elements = db._createStatement({$statement}).execute().elements(); return elements[0] ? elements[0] : null}();", "Finder:searchForOne", null, false, array('type' => $type)); } else { try { $result = $this->_toolbox->getQuery()->getOne($aqlStatement, $params); } catch (\Exception $e) { $normalised = $this->_toolbox->normaliseDriverExceptions($e); throw new FinderException($normalised['message'], $normalised['code']); } if (!$result) { return null; } else { $converted = $this->convertToPods($type, array($result)); return reset($converted); } } }
/** * Get the neighbour vertices connected to this vertex via some edge. The vertices and their connecting edges can be filtered by AQL. * @param Document|AModel|string A vertex pod, model or string of the vertex id. * @param string $direction "in" for inbound neighbours, "out" for outbound neighbours and "any" for all neighbours. * @param string|array $labels A string representing one label or an array of labels we want the inbound edges to have. * @param string $aql An optional AQL fragment if we want to filter the edges or vertices, for example: * FILTER doc.edge.someproperty == "somevalue" && doc.vertex.anotherproperty == "anothervalue" * @param array $params An optional associative array containing parameters to bind to the query. * @param string $placeholder Set this to something else if you do not wish to use "doc" to refer to documents in your query. * @return array */ public function getNeighbours($model, $direction = "any", $labels = null, $aql = "", $params = array(), $placeholder = "doc") { $id = $this->getVertexId($model); if (!$id) { return array(); } if (strtolower($direction) == "in") { $direction = "inbound"; } if (strtolower($direction) == "out") { $direction = "outbound"; } if ($labels && !is_array($labels)) { $labels = array($labels); } $vertexCollection = $this->_toolbox->generateBindingParameter('@vertexCollection', $params); $edgeCollection = $this->_toolbox->generateBindingParameter('@edgeCollection', $params); $vertexParameter = $this->_toolbox->generateBindingParameter('vertexid', $params); $directionParameter = $this->_toolbox->generateBindingParameter('direction', $params); if (!$labels) { $query = "FOR {$placeholder} in NEIGHBORS(@{$vertexCollection}, @{$edgeCollection}, @{$vertexParameter}, @{$directionParameter}) " . $aql . " return {$placeholder}.vertex"; } else { $count = 0; $labelString = ''; foreach ($labels as $label) { $labelParameter = $this->_toolbox->generateBindingParameter('label', $params); if ($count > 0) { $labelString .= ", "; } $labelString .= "{'\$label': @{$labelParameter}}"; $params[$labelParameter] = $label; $count++; } $query = "FOR {$placeholder} in NEIGHBORS(@{$vertexCollection}, @{$edgeCollection}, @{$vertexParameter}, @{$directionParameter}, [{$labelString}]) " . $aql . " return {$placeholder}.vertex"; } $params[$vertexCollection] = $this->_toolbox->getVertexCollectionName(); $params[$edgeCollection] = $this->_toolbox->getEdgeCollectionName(); $params[$vertexParameter] = $id; $params[$directionParameter] = $direction; if ($this->_toolbox->getTransactionManager()->hasTransaction()) { $this->_toolbox->getTransactionManager()->addReadCollection($this->_toolbox->getEdgeCollectionName()); $this->_toolbox->getTransactionManager()->addReadCollection($this->_toolbox->getVertexCollectionName()); $statement = json_encode(array('query' => $query, 'bindVars' => $params), JSON_FORCE_OBJECT); $this->_toolbox->getTransactionManager()->addCommand("db._createStatement({$statement}).execute().elements();", "GraphManager:getNeighbours", null, true); } else { try { $result = $this->_toolbox->getQuery()->getAll($query, $params); } catch (\Exception $e) { throw new GraphManagerException($e->getMessage(), $e->getCode()); } if (empty($result)) { return array(); } return $this->convertToPods("vertex", $result); } }
/** * @covers Paradox\Toolbox::getQuery */ public function testGetQuery() { $this->assertInstanceOf('Paradox\\toolbox\\Query', $this->toolbox->getQuery(), 'Getting the query helper did not return a Paradox\\toolbox\\Query'); }