1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace TheCodingMachine\TDBM\Dao; |
4
|
|
|
|
5
|
|
|
use TheCodingMachine\TDBM\Test\Dao\Generated\UserBaseDao; |
6
|
|
|
use TheCodingMachine\TDBM\UncheckedOrderBy; |
7
|
|
|
|
8
|
|
|
/** |
9
|
|
|
* The UserDao class will maintain the persistence of UserBean class into the users table. |
10
|
|
|
*/ |
11
|
|
|
class TestUserDao extends UserBaseDao |
12
|
|
|
{ |
13
|
|
|
/** |
14
|
|
|
* Returns the list of users by alphabetical order. |
15
|
|
|
* |
16
|
|
|
* @return UserBean[] |
17
|
|
|
*/ |
18
|
|
|
public function getUsersByAlphabeticalOrder() |
19
|
|
|
{ |
20
|
|
|
// The third parameter will be used in the "ORDER BY" clause of the SQL query. |
21
|
|
|
return $this->find(null, [], 'login ASC'); |
22
|
|
|
} |
23
|
|
|
/** |
24
|
|
|
* Returns the list of users by alphabetical order. |
25
|
|
|
* |
26
|
|
|
* @return UserBean[] |
27
|
|
|
*/ |
28
|
|
|
public function getUsersFromSqlByAlphabeticalOrder() |
29
|
|
|
{ |
30
|
|
|
// The third parameter will be used in the "ORDER BY" clause of the SQL query. |
31
|
|
|
return $this->findFromSql('users', null, [], 'login ASC'); |
32
|
|
|
} |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* Returns the list of users by alphabetical order. |
36
|
|
|
* |
37
|
|
|
* @return UserBean[] |
38
|
|
|
*/ |
39
|
|
|
public function getUsersByCountryOrder() |
40
|
|
|
{ |
41
|
|
|
// The third parameter will be used in the "ORDER BY" clause of the SQL query. |
42
|
|
|
return $this->find(null, [], 'country.label ASC', ['country']); |
43
|
|
|
} |
44
|
|
|
/** |
45
|
|
|
* Returns the list of users by alphabetical order. |
46
|
|
|
* |
47
|
|
|
* @return UserBean[] |
48
|
|
|
*/ |
49
|
|
|
public function getUsersFromSqlByCountryOrder() |
50
|
|
|
{ |
51
|
|
|
// The third parameter will be used in the "ORDER BY" clause of the SQL query. |
52
|
|
|
return $this->findFromSql('users JOIN country ON country.id = users.country_id', null, [], 'country.label ASC'); |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* Returns the list of users whose login starts with $login. |
57
|
|
|
* |
58
|
|
|
* @param string $login |
59
|
|
|
* @param string $mode |
60
|
|
|
* |
61
|
|
|
* @return \TheCodingMachine\TDBM\ResultIterator|\TheCodingMachine\TDBM\Test\Dao\Bean\UserBean[]|\TheCodingMachine\TDBM\Test\Dao\ResultArray |
62
|
|
|
*/ |
63
|
|
|
public function getUsersByLoginStartingWith($login = null, $mode = null) |
64
|
|
|
{ |
65
|
|
|
return $this->find('login LIKE :login', ['login' => $login.'%'], null, [], $mode); |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
/** |
69
|
|
|
* Returns the user whose login is $login. |
70
|
|
|
* |
71
|
|
|
* @param string $login |
72
|
|
|
* |
73
|
|
|
* @return \TheCodingMachine\TDBM\Test\Dao\Bean\UserBean |
74
|
|
|
*/ |
75
|
|
|
public function getUserByLogin($login) |
76
|
|
|
{ |
77
|
|
|
return $this->findOne('login = :login', ['login' => $login]); |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
public function getUsersByManagerId($managerId) |
81
|
|
|
{ |
82
|
|
|
return $this->find('contact.manager_id = :id!', ['id' => $managerId]); |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
/** |
86
|
|
|
* Triggers an error because table "contacts" does not exist. |
87
|
|
|
* |
88
|
|
|
* @return \TheCodingMachine\TDBM\ResultIterator|\TheCodingMachine\TDBM\Test\Dao\Bean\UserBean[]|\TheCodingMachine\TDBM\Test\Dao\Generated\ResultArray |
89
|
|
|
*/ |
90
|
|
|
public function getUsersWrongTableName() |
91
|
|
|
{ |
92
|
|
|
return $this->find('contacts.manager_id = 1'); |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
/** |
96
|
|
|
* Returns a list of users, sorted by a table on an external column. |
97
|
|
|
* |
98
|
|
|
* @return \TheCodingMachine\TDBM\ResultIterator|\TheCodingMachine\TDBM\Test\Dao\Bean\UserBean[]|\TheCodingMachine\TDBM\Test\Dao\Generated\ResultArray |
99
|
|
|
*/ |
100
|
|
|
public function getUsersByCountryName() |
101
|
|
|
{ |
102
|
|
|
return $this->find(null, [], 'country.label DESC'); |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
/** |
106
|
|
|
* A test to sort by function. |
107
|
|
|
* |
108
|
|
|
* @return \TheCodingMachine\TDBM\ResultIterator|\TheCodingMachine\TDBM\Test\Dao\Bean\UserBean[]|\TheCodingMachine\TDBM\Test\Dao\Generated\ResultArray |
109
|
|
|
*/ |
110
|
|
|
public function getUsersByReversedCountryName() |
111
|
|
|
{ |
112
|
|
|
return $this->find(null, [], new UncheckedOrderBy('REVERSE(country.label) ASC')); |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
/** |
116
|
|
|
* A test to check exceptions when providing expressions in ORDER BY clause. |
117
|
|
|
* |
118
|
|
|
* @return \TheCodingMachine\TDBM\ResultIterator|\TheCodingMachine\TDBM\Test\Dao\Bean\UserBean[]|\TheCodingMachine\TDBM\Test\Dao\Generated\ResultArray |
119
|
|
|
*/ |
120
|
|
|
public function getUsersByInvalidOrderBy() |
121
|
|
|
{ |
122
|
|
|
return $this->find(null, [], 'REVERSE(country.label) ASC'); |
123
|
|
|
} |
124
|
|
|
} |
125
|
|
|
|