/** * * @param type $parameters key value pairs, parameter name and value * @return type */ public function findUsersByPropertyUsingLike($parameters, $firstResult, $maxResults) { $qb = $this->createQueryBuilder('u'); $params = array(); $andx = array(); $orderParams = isset($parameters['order']) ? $parameters['order'] : array(); // Build the query from posted search parameters Utils::buildDoctrineQuery($qb, $parameters['search'], $params, $andx); // Only apply where if parameters are given if (count($andx) > 0) { $qb->where(call_user_func_array(array($qb->expr(), "andX"), $andx))->setParameters($params); } if (isset($orderParams['field']) && $orderParams['field'] && isset($orderParams['dir']) && $orderParams['dir']) { $qb->orderBy($orderParams['field'], $orderParams['dir']); } $qb->setFirstResult($firstResult); $qb->setMaxResults($maxResults); return new Paginator($qb->getQuery(), true); }
public function testBuildDoctrineQuery() { $eb = $this->getMockBuilder('\\Doctrine\\DBAL\\Query\\Expression\\ExpressionBuilder')->disableOriginalConstructor()->getMock(); $eb->expects($this->any())->method('andX')->will($this->returnSelf()); $eb->expects($this->any())->method('like')->will($this->returnSelf()); $qb = $this->getMockBuilder('\\Doctrine\\ORM\\QueryBuilder')->disableOriginalConstructor()->getMock(); $qb->expects($this->any())->method('leftJoin')->will($this->returnSelf()); $qb->expects($this->any())->method('expr')->will($this->returnValue($eb)); $qb->expects($this->once())->method('getRootAliases')->will($this->returnValue(array('u'))); $input = array('id' => '1', 'username' => 'test', 'employee' => array('name' => 'Test Employee')); $params = $andx = array(); Utils::buildDoctrineQuery($qb, $input, $params, $andx); $this->assertCount(3, $params, 'Utils::buildDoctrineQuery: Count does not match.'); $this->assertCount(count($andx), $params, 'Utils::buildDoctrineQuery: Andx count does not match params.'); $this->assertJsonStringEqualsJsonString('{"id":"%1%","username":"******","name":"%Test Employee%"}', json_encode($params), 'Utils::buildDoctrineQuery: Json result does not match.'); }