First nett3.0 webpage version
This commit is contained in:
@@ -46,4 +46,41 @@ abstract class AbstractRepository extends EntityRepository
|
||||
}, $qb->getQuery()->getArrayResult());
|
||||
}
|
||||
|
||||
/**
|
||||
* Fetches all records and returns an associative array indexed by key
|
||||
*
|
||||
* @param array $criteria
|
||||
* @param string $key
|
||||
*
|
||||
* @throws \Exception|QueryException
|
||||
* @return array
|
||||
*/
|
||||
public function findAssoc(array $criteria = [],String $key = NULL)
|
||||
{
|
||||
if (!is_array($criteria)) {
|
||||
$key = $criteria;
|
||||
$criteria = [];
|
||||
}
|
||||
|
||||
$qb = $this->createQueryBuilder('e')
|
||||
->resetDQLPart('from')->from($this->getEntityName(), 'e', 'e.' . $key);
|
||||
|
||||
foreach ($criteria as $k => $v) {
|
||||
if (is_array($v)) {
|
||||
$qb->andWhere(sprintf('e.%s IN(:%s)', $k, $k))->setParameter($k, array_values($v));
|
||||
} else {
|
||||
$qb->andWhere(sprintf('e.%s = :%s', $k, $k))->setParameter($k, $v);
|
||||
}
|
||||
}
|
||||
|
||||
try {
|
||||
return $qb->getQuery()->getResult();
|
||||
|
||||
} catch (\Exception $e) {
|
||||
throw $this->handleException($e, $qb);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user