-
Notifications
You must be signed in to change notification settings - Fork 28
Expand file tree
/
Copy pathTestCountryDao.php
More file actions
88 lines (76 loc) · 1.8 KB
/
TestCountryDao.php
File metadata and controls
88 lines (76 loc) · 1.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
<?php
declare(strict_types=1);
namespace TheCodingMachine\TDBM\Dao;
use Porpaginas\Result;
use TheCodingMachine\TDBM\Test\Dao\Bean\CountryBean;
use TheCodingMachine\TDBM\Test\Dao\Generated\CountryBaseDao;
/**
* The CountryDao class will maintain the persistence of CountryBean class into the country table.
*/
class TestCountryDao extends CountryBaseDao
{
/**
* @return CountryBean[]|Result
*/
public function getCountriesByUserCount()
{
$sql = <<<SQL
SELECT country.*
FROM country
LEFT JOIN users ON users.country_id = country.id
GROUP BY country.id, country.label
ORDER BY COUNT(users.id) DESC
SQL;
return $this->findFromRawSql($sql);
}
/**
* @return CountryBean[]|Result
*/
public function getCountriesUsingUnion()
{
$sql = <<<SQL
SELECT country.*
FROM country
WHERE country.id = 1
UNION
SELECT country.*
FROM country
WHERE country.id = 2
SQL;
return $this->findFromRawSql($sql);
}
/**
* @return CountryBean[]|Result
*/
public function getCountriesUsingSimpleQuery()
{
$sql = <<<SQL
SELECT country.*
FROM country
WHERE country.id = 1
SQL;
return $this->findFromRawSql($sql);
}
/**
* @return CountryBean[]|Result
*/
public function getCountriesUsingDistinctQuery()
{
// Note: there are many users whose country is ID 2 (UK).
// But the distinct should return only one country (including the count() call)
$sql = <<<SQL
SELECT DISTINCT country.*
FROM country
LEFT JOIN users ON users.country_id = country.id
WHERE country_id=2
SQL;
return $this->findFromRawSql($sql);
}
/**
* @return CountryBean[]|Result
*/
public function findByIds(array $ids)
{
return $this->find('id IN (:ids)', ['ids' => $ids]);
}
}