|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace LoginCidadao\CoreBundle\Entity; |
|
4
|
|
|
|
|
5
|
|
|
use Doctrine\ORM\EntityRepository; |
|
6
|
|
|
|
|
7
|
|
|
class CityRepository extends EntityRepository |
|
8
|
|
|
{ |
|
9
|
|
|
|
|
10
|
|
|
public function findByString($string, $countryId = null, $stateId = null) |
|
|
|
|
|
|
11
|
|
|
{ |
|
12
|
|
|
$qb = $this->getEntityManager()->createQueryBuilder(); |
|
13
|
|
|
|
|
14
|
|
|
$qb |
|
15
|
|
|
->select('c') |
|
16
|
|
|
->from('LoginCidadaoCoreBundle:City', 'c') |
|
17
|
|
|
->join( |
|
18
|
|
|
'LoginCidadaoCoreBundle:State', |
|
19
|
|
|
's', |
|
20
|
|
|
'WITH', |
|
21
|
|
|
'c.state = s' |
|
22
|
|
|
) |
|
23
|
|
|
->join( |
|
24
|
|
|
'LoginCidadaoCoreBundle:Country', |
|
25
|
|
|
'co', |
|
26
|
|
|
'WITH', |
|
27
|
|
|
's.country = co' |
|
28
|
|
|
) |
|
29
|
|
|
->where('c.name LIKE :string OR LOWER(c.name) LIKE :string') |
|
30
|
|
|
->addOrderBy('s.preference', 'DESC') |
|
31
|
|
|
->addOrderBy('c.name', 'ASC') |
|
32
|
|
|
->setParameter('string', "$string%"); |
|
33
|
|
|
|
|
34
|
|
|
if ($stateId > 0) { |
|
35
|
|
|
$qb->andWhere('s.id = :stateId') |
|
36
|
|
|
->setParameter('stateId', $stateId); |
|
37
|
|
|
} |
|
38
|
|
|
if ($countryId > 0) { |
|
39
|
|
|
$qb->andWhere('co.id = :countryId') |
|
40
|
|
|
->setParameter('countryId', $countryId); |
|
41
|
|
|
} |
|
42
|
|
|
|
|
43
|
|
|
return $qb->getQuery()->getResult(); |
|
44
|
|
|
} |
|
45
|
|
|
|
|
46
|
|
|
public function findByPreferedState() |
|
|
|
|
|
|
47
|
|
|
{ |
|
48
|
|
|
$em = $this->getEntityManager(); |
|
49
|
|
|
$states = $em->getRepository('LoginCidadaoCoreBundle:State') |
|
50
|
|
|
->createQueryBuilder('s') |
|
51
|
|
|
->orderBy('s.preference', 'DESC') |
|
52
|
|
|
->setMaxResults(1) |
|
53
|
|
|
->getQuery()->getResult(); |
|
54
|
|
|
$state = reset($states); |
|
55
|
|
|
$cities = $state->getCities(); |
|
56
|
|
|
|
|
57
|
|
|
return $cities; |
|
58
|
|
|
} |
|
59
|
|
|
|
|
60
|
|
|
/** |
|
61
|
|
|
* @return mixed |
|
62
|
|
|
*/ |
|
63
|
|
|
public function countCities() |
|
64
|
|
|
{ |
|
65
|
|
|
return $this->createQueryBuilder('c') |
|
66
|
|
|
->select('COUNT(c)') |
|
67
|
|
|
->getQuery()->getSingleScalarResult(); |
|
68
|
|
|
} |
|
69
|
|
|
} |
|
70
|
|
|
|
Our type inference engine in quite powerful, but sometimes the code does not provide enough clues to go by. In these cases we request you to add a
@returnannotation as described here.