Returns the pattern of the search.
public getSearchPattern ( ) : mixed | ||
Résultat | mixed |
public function testGetFields() { $request = new Request(['fields' => 'field1,field2,field3', 'sortBy' => 'id', 'sortOrder' => 'desc', 'search' => 'test', 'searchFields' => 'title', 'limit' => 10, 'page' => 3]); $helper = new ListRestHelper($request, $this->em); $this->assertEquals(['field1', 'field2', 'field3'], $helper->getFields()); $this->assertEquals(['id' => 'desc'], $helper->getSorting()); $this->assertEquals('test', $helper->getSearchPattern()); $this->assertEquals(['title'], $helper->getSearchFields()); $this->assertEquals(10, $helper->getLimit()); $this->assertEquals(20, $helper->getOffset()); }
/** * Find list with parameter. * * @param array $where * @param string $prefix * @param bool $justCount Defines, if find should just return the total number of results * @param array $joinConditions optionally specify conditions on join * * @return array|object|int */ public function find($where = [], $prefix = 'u', $justCount = false, $joinConditions = []) { $searchPattern = $this->helper->getSearchPattern(); $searchFields = $this->helper->getSearchFields(); // if search string is set, but search fields are not, take all fields into account if (!is_null($searchPattern) && $searchPattern != '' && (is_null($searchFields) || count($searchFields) == 0)) { $searchFields = $this->getEntityManager()->getClassMetadata($this->getEntityName())->getFieldNames(); } $textFields = $this->getFieldsWitTypes(['text', 'string', 'guid'], $searchFields); if (is_numeric($searchPattern)) { $numberFields = $this->getFieldsWitTypes(['integer', 'float', 'decimal'], $searchFields); } else { $numberFields = []; } $queryBuilder = new ListQueryBuilder($this->getClassMetadata()->getAssociationNames(), $this->getClassMetadata()->getFieldNames(), $this->getEntityName(), $this->helper->getFields(), $this->helper->getSorting(), $where, $textFields, $numberFields, $joinConditions); if ($justCount) { $queryBuilder->justCount('u.id'); } $dql = $queryBuilder->find($prefix); $query = $this->getEntityManager()->createQuery($dql); if (!$justCount) { $query->setFirstResult($this->helper->getOffset())->setMaxResults($this->helper->getLimit()); } if ($searchPattern != null && $searchPattern != '') { if (count($searchFields) > 0) { if (count($textFields) > 0) { $query->setParameter('search', '%' . $searchPattern . '%'); } if (count($numberFields) > 0) { $query->setParameter('strictSearch', $searchPattern); } } } // if just used for counting if ($justCount) { return intval($query->getSingleResult()['totalcount']); } $results = $query->getArrayResult(); // check if relational filter was set ( e.g. emails[0]_email) // and filter result if (count($filters = $queryBuilder->getRelationalFilters()) > 0) { $filteredResults = []; // check if fields do contain id, else skip if (count($fields = $this->helper->getFields()) > 0 && array_search('id', $fields) !== false) { $ids = []; foreach ($results as $result) { $id = $result['id']; // check if result already in resultset if (!array_key_exists($id, $ids)) { $ids[$id] = -1; $filteredResults[] = $result; } ++$ids[$id]; // check filters foreach ($filters as $filter => $key) { // check if we are at the specified index if ($key == $ids[$id]) { $index = $this->getArrayIndexByKeyValue($filteredResults, $id); // set to current key $filteredResults[$index][$filter] = $result[$filter]; } } } $results = $filteredResults; } } return $results; }