@@ -42,7 +42,7 @@ private function cleanOrderBy($orderby) {
42
42
$ parts = explode (', ' , $ orderby );
43
43
foreach ($ parts as $ part ) {
44
44
$ p_part = explode (' ' , trim ($ part ));
45
- $ cleaned [] = '` ' .trim ($ p_part [0 ]).'` ' .(count ($ p_part )>1 ?' ' .trim ($ p_part [1 ]):'' );
45
+ $ cleaned [] = 'te. ` ' .trim ($ p_part [0 ]).'` ' .(count ($ p_part )>1 ?' ' .trim ($ p_part [1 ]):'' );
46
46
}
47
47
return implode (', ' , $ cleaned );
48
48
}
@@ -62,7 +62,21 @@ public function getOne($id) {
62
62
}
63
63
}
64
64
65
- public function getAll ($ orderby = 'id ' , $ desc = false ) {
65
+ /**
66
+ *
67
+ * @param int $page index de la page
68
+ * @param int $length longueur de la page
69
+ * @param string $orderby colone de tri
70
+ * @param boolean $desc tri DESC ou pas
71
+ * @param array $mixedConditions conditions de filtre [var: value] ou [var: [op, value]] (WHERE var = value AND ...)
72
+ */
73
+ public function getPaginate ($ page = 1 , $ length = 25 , $ orderby = 'id ' , $ desc = false , array $ mixedConditions = [], $ join = '' ) {
74
+ $ limit = $ length ;
75
+ $ offset = ($ page -1 ) * $ length ;
76
+ return $ this ->getOf ($ mixedConditions , $ orderby , $ desc , $ offset , $ limit , $ join );
77
+ }
78
+
79
+ public function getAll ($ orderby = 'id ' , $ desc = false , $ limit = null , $ offset = 0 ) {
66
80
$ dbequiv = $ this ->class ->getDBEquiv ();
67
81
if (!is_null ($ orderby ) && !empty ($ orderby )) {
68
82
$ desc = $ desc ?' DESC ' :' ASC ' ;
@@ -71,7 +85,11 @@ public function getAll($orderby = 'id', $desc = false) {
71
85
else {
72
86
$ orderby = '' ;
73
87
}
74
- $ all = $ this ->getSQL ('SELECT ' .$ dbequiv ['id ' ].' FROM ` ' .TABLE_PREFIX .$ this ->class ->DB_table .'` ' .$ orderby );
88
+ $ limitclause = '' ;
89
+ if (!is_null ($ limit )) {
90
+ $ limitclause = " LIMIT $ offset, $ limit " ;
91
+ }
92
+ $ all = $ this ->getSQL ('SELECT ' .$ dbequiv ['id ' ].' FROM ` ' .TABLE_PREFIX .$ this ->class ->DB_table .'` te ' .$ orderby .$ limitclause );
75
93
return $ all ;
76
94
}
77
95
@@ -98,7 +116,7 @@ public function countAll() {
98
116
* @param integer $limitUp [optional, default 0] si $limitUp > $limitDown alors (LIMIT $limitDown, $limitUp)
99
117
* @return Array<Entite> ($this->class) ou false si pas de resultat
100
118
*/
101
- public function getOf (array $ mixedConditions , $ orderby = 'id ' , $ desc = false , $ limitDown = 0 , $ limitUp = 0 ) {
119
+ public function getOf (array $ mixedConditions , $ orderby = 'id ' , $ desc = false , $ offset = 0 , $ limit = 0 , $ join = '' ) {
102
120
$ dbequiv = $ this ->class ->getDBEquiv ();
103
121
if (!is_null ($ orderby ) && !empty ($ orderby )) {
104
122
$ desc = $ desc ?' DESC ' :' ASC ' ;
@@ -107,29 +125,34 @@ public function getOf(array $mixedConditions, $orderby = 'id', $desc = false, $l
107
125
else {
108
126
$ orderby = '' ;
109
127
}
110
- if ($ limitUp > $ limitDown ){
111
- $ limit = ' LIMIT ' .$ limitDown .', ' .$ limitUp ;
128
+ if ($ limit > 0 ){
129
+ $ limit = ' LIMIT ' .$ offset .', ' .$ limit ;
112
130
}
113
131
else {
114
132
$ limit = '' ;
115
133
}
116
- $ cond = array () ;
134
+ $ conditions = '' ;
117
135
$ params = array ();
118
- foreach ($ mixedConditions as $ var => $ value ){
119
- $ pk = 'p_ ' .(count ($ params )+1 );
120
- $ val = $ value ;
121
- if ( is_array ( $ value ) ) {
122
- //forme [var, [op, value]]
123
- $ cond [] = '` ' .$ dbequiv [$ var ].'` ' .$ value [0 ].($ value [1 ] !== null ? ' \'{{ ' .$ pk .'}} \'' : ' NULL ' );
124
- $ val = $ value [1 ];
125
- }
126
- else {
127
- //forme [var, value] === [var, ["=", value] ]
128
- $ cond [] = '` ' .$ dbequiv [$ var ].'` = ' .($ value !==null ?'\'{{ ' .$ pk .'}} \'' :' NULL ' );
129
- }
130
- $ params [$ pk ] = $ val ;
131
- }
132
- $ all = $ this ->getSQL ( 'SELECT ` ' .$ dbequiv ['id ' ].'` FROM ` ' .TABLE_PREFIX .$ this ->class ->DB_table .'` WHERE ' .implode (' AND ' ,$ cond ).$ orderby .$ limit , $ params );
136
+ if (is_array ($ mixedConditions ) && count ($ mixedConditions ) > 0 ) {
137
+ $ cond = array ();
138
+ foreach ($ mixedConditions as $ var => $ value ){
139
+ $ pk = 'p_ ' .(count ($ params )+1 );
140
+ $ val = $ value ;
141
+ if ( is_array ( $ value ) ) {
142
+ //forme [var: [op, value]]
143
+ $ cond [] = 'te.` ' .$ dbequiv [$ var ].'` ' .$ value [0 ].($ value [1 ] !== null ? ' \'{{ ' .$ pk .'}} \'' : ' NULL ' );
144
+ $ val = $ value [1 ];
145
+ }
146
+ else {
147
+ //forme [var: value] === [var: ["=", value] ]
148
+ $ cond [] = 'te.` ' .$ dbequiv [$ var ].'` = ' .($ value !==null ?'\'{{ ' .$ pk .'}} \'' :' NULL ' );
149
+ }
150
+ $ params [$ pk ] = $ val ;
151
+ }
152
+ $ conditions = 'WHERE ' .implode (' AND ' ,$ cond );
153
+ }
154
+ $ join = " $ join " ;
155
+ $ all = $ this ->getSQL ( 'SELECT te.` ' .$ dbequiv ['id ' ].'` FROM ` ' .TABLE_PREFIX .$ this ->class ->DB_table .'` as te ' .$ join .$ conditions .$ orderby .$ limit , $ params );
133
156
return $ all ;
134
157
}
135
158
@@ -138,8 +161,8 @@ public function getOf(array $mixedConditions, $orderby = 'id', $desc = false, $l
138
161
* @param array $mixedConditions [var: value] or [var, [op, value]] (WHERE var = value AND ...)
139
162
* @return Entite $this->class ou false si pas de r�sultat
140
163
*/
141
- public function getOneOf (array $ mixedConditions , $ orderby = 'id ' , $ desc = false ){
142
- $ ret = $ this ->getOf ($ mixedConditions , $ orderby , $ desc , 0 , 1 );
164
+ public function getOneOf (array $ mixedConditions , $ orderby = 'id ' , $ desc = false , $ join = '' ){
165
+ $ ret = $ this ->getOf ($ mixedConditions , $ orderby , $ desc , 0 , 1 , $ join );
143
166
if ($ ret !== false ){
144
167
return $ ret [0 ];
145
168
}
@@ -151,25 +174,30 @@ public function getOneOf(array $mixedConditions, $orderby = 'id', $desc = false)
151
174
* @param array $mixedConditions [var: value] or [var : [op, value]] (WHERE var = value AND ...)
152
175
* @return integer
153
176
*/
154
- public function countOf (array $ mixedConditions ) {
177
+ public function countOf (array $ mixedConditions, $ join = '' ) {
155
178
$ dbequiv = $ this ->class ->getDBEquiv ();
156
- $ cond = array () ;
179
+ $ conditions = '' ;
157
180
$ params = array ();
158
- foreach ($ mixedConditions as $ var => $ value ){
159
- $ pk = 'p_ ' .(count ($ params )+1 );
160
- $ val = $ value ;
161
- if ( is_array ( $ value ) ) {
162
- //forme [var, [op, value]]
163
- $ cond [] = '` ' .$ dbequiv [$ var ].'` ' .$ value [0 ].($ value [1 ] !== null ? ' \'{{ ' .$ pk .'}} \'' : ' NULL ' );
164
- $ val = $ value [1 ];
165
- }
166
- else {
167
- //forme [var, value] === [var, ["=", value] ]
168
- $ cond [] = '` ' .$ dbequiv [$ var ].'` = ' .($ value !==null ?'\'{{ ' .$ pk .'}} \'' :' NULL ' );
169
- }
170
- $ params [$ pk ] = $ val ;
171
- }
172
- $ res = SQL ::getInstance ()->exec ('SELECT COUNT(*) as nombre FROM ` ' .TABLE_PREFIX .$ this ->class ->DB_table .'` WHERE ' .implode (' AND ' ,$ cond ), false , $ params );
181
+ if (is_array ($ mixedConditions ) && count ($ mixedConditions ) > 0 ) {
182
+ $ cond = array ();
183
+ foreach ($ mixedConditions as $ var => $ value ){
184
+ $ pk = 'p_ ' .(count ($ params )+1 );
185
+ $ val = $ value ;
186
+ if ( is_array ( $ value ) ) {
187
+ //forme [var, [op, value]]
188
+ $ cond [] = 'te.` ' .$ dbequiv [$ var ].'` ' .$ value [0 ].($ value [1 ] !== null ? ' \'{{ ' .$ pk .'}} \'' : ' NULL ' );
189
+ $ val = $ value [1 ];
190
+ }
191
+ else {
192
+ //forme [var, value] === [var, ["=", value] ]
193
+ $ cond [] = 'te.` ' .$ dbequiv [$ var ].'` = ' .($ value !==null ?'\'{{ ' .$ pk .'}} \'' :' NULL ' );
194
+ }
195
+ $ params [$ pk ] = $ val ;
196
+ }
197
+ $ conditions = 'WHERE ' .implode (' AND ' ,$ cond );
198
+ }
199
+ $ join = " $ join " ;
200
+ $ res = SQL ::getInstance ()->exec ('SELECT COUNT(*) as nombre FROM ` ' .TABLE_PREFIX .$ this ->class ->DB_table .'` as te ' .$ join .$ conditions , false , $ params );
173
201
if ($ res ) { //cas ou aucun retour requete (retour FALSE)
174
202
$ all = 0 ;
175
203
foreach ($ res as $ row ) {
0 commit comments