| | 153 | +++ Sample Queries |
| | 154 | |
| | 155 | All of the below queries were executed with the following schema and data fixtures: |
| | 156 | |
| | 157 | Schema |
| | 158 | |
| | 159 | <code type="yaml"> |
| | 160 | --- |
| | 161 | User: |
| | 162 | actAs: |
| | 163 | Timestampable: |
| | 164 | Sluggable: |
| | 165 | fields: [username] |
| | 166 | columns: |
| | 167 | email_address: string(255) |
| | 168 | username: string(255) |
| | 169 | password: string(255) |
| | 170 | relations: |
| | 171 | Groups: |
| | 172 | class: Group |
| | 173 | refClass: UserGroup |
| | 174 | foreignAlias: Users |
| | 175 | |
| | 176 | Phonenumber: |
| | 177 | actAs: [Timestampable] |
| | 178 | columns: |
| | 179 | user_id: integer |
| | 180 | phone: string(255) |
| | 181 | primary_num: |
| | 182 | type: boolean |
| | 183 | default: false |
| | 184 | relations: |
| | 185 | User: |
| | 186 | foreignAlias: Phonenumbers |
| | 187 | |
| | 188 | Group: |
| | 189 | # required to renamed to groups because group is a reserved word in mysql |
| | 190 | tableName: groups |
| | 191 | columns: |
| | 192 | name: string(255) |
| | 193 | |
| | 194 | UserGroup: |
| | 195 | columns: |
| | 196 | user_id: integer |
| | 197 | group_id: integer |
| | 198 | </code> |
| | 199 | |
| | 200 | Data fixtures |
| | 201 | |
| | 202 | <code type="yaml"> |
| | 203 | --- |
| | 204 | User: |
| | 205 | User_1: |
| | 206 | username: jwage |
| | 207 | password: changeme |
| | 208 | Phonenumbers: |
| | 209 | Phonenumber_1: |
| | 210 | phone: 6155139185 |
| | 211 | primary_num: true |
| | 212 | Phonenumber_2: |
| | 213 | phone: 6153137679 |
| | 214 | Groups: [Group_1] |
| | 215 | |
| | 216 | Group: |
| | 217 | Group_1: |
| | 218 | name: Group 1 |
| | 219 | Group_2: |
| | 220 | name: Group 2 |
| | 221 | </code> |
| | 222 | |
| | 223 | Count number of records for a relationship |
| | 224 | |
| | 225 | <code type="php"> |
| | 226 | $q = Doctrine_Query::create() |
| | 227 | ->select('u.*, COUNT(DISTINCT p.id) AS num_phonenumbers') |
| | 228 | ->from('User u') |
| | 229 | ->leftJoin('u.Phonenumbers p') |
| | 230 | ->groupBy('u.id'); |
| | 231 | |
| | 232 | $users = $q->fetchArray(); |
| | 233 | |
| | 234 | echo $users[0]['Phonenumbers'][0]['num_phonenumbers']; |
| | 235 | </code> |
| | 236 | |
| | 237 | Simple WHERE with one parameter value |
| | 238 | |
| | 239 | <code type="php"> |
| | 240 | $q = Doctrine_Query::create() |
| | 241 | ->from('User u') |
| | 242 | ->where('u.username = ?', 'jwage'); |
| | 243 | $users = $q->fetchArray(); |
| | 244 | </code> |
| | 245 | |
| | 246 | Multiple WHERE with multiple parameters values |
| | 247 | |
| | 248 | <code type="php"> |
| | 249 | $q = Doctrine_Query::create() |
| | 250 | ->from('User u') |
| | 251 | ->where('u.is_active = ? AND u.is_online = ?', array(1, 1)); |
| | 252 | $users = $q->fetchArray(); |
| | 253 | |
| | 254 | // You can also optionally use the addWhere() to add to the existing where parts |
| | 255 | $q = Doctrine_Query::create() |
| | 256 | ->from('User u') |
| | 257 | ->where('u.is_active = ?', 1) |
| | 258 | ->addWhere('u.is_online = ?', 1); |
| | 259 | $users = $q->fetchArray(); |
| | 260 | </code> |
| | 261 | |
| | 262 | Using whereIn() convenience method |
| | 263 | |
| | 264 | <code type="php"> |
| | 265 | $q = Doctrine_Query::create() |
| | 266 | ->from('User u') |
| | 267 | ->whereIn('u.id', array(1, 2, 3)) |
| | 268 | $users = $q->fetchArray(); |
| | 269 | |
| | 270 | // This is the same as above |
| | 271 | $q = Doctrine_Query::create() |
| | 272 | ->from('User u') |
| | 273 | ->where('u.id IN (1, 2, 3)'); |
| | 274 | $users = $q->fetchArray(); |
| | 275 | </code> |
| | 276 | |
| | 277 | Using DBMS function in your WHERE |
| | 278 | |
| | 279 | <code type="php"> |
| | 280 | $userEncryptedKey = 'a157a558ac00449c92294c7fab684ae0'; |
| | 281 | $q = Doctrine_Query::create() |
| | 282 | ->from('User u') |
| | 283 | ->where("MD5(CONCAT(u.username, 'secret_user_key')) = ?", $userEncryptedKey); |
| | 284 | $user = $q->fetchOne(); |
| | 285 | |
| | 286 | $q = Doctrine_Query::create() |
| | 287 | ->from('User u') |
| | 288 | ->where('LOWER(u.username) = LOWER(?)', 'jwage'); |
| | 289 | $user = $q->fetchOne(); |
| | 290 | |
| | 291 | </code> |
| | 292 | |
| | 293 | Limiting resultsets using aggregate functions |
| | 294 | |
| | 295 | <code type="php"> |
| | 296 | // Users with more than 1 phonenumber |
| | 297 | $q = Doctrine_Query::create() |
| | 298 | ->select('u.*, COUNT(DISTINCT p.id) AS num_phonenumbers') |
| | 299 | ->from('User u') |
| | 300 | ->leftJoin('u.Phonenumbers p') |
| | 301 | ->having('num_phonenumbers > 1') |
| | 302 | ->groupBy('u.id'); |
| | 303 | $users = $q->fetchArray(); |
| | 304 | </code> |
| | 305 | |
| | 306 | Join only primary phonenumbers using WITH |
| | 307 | |
| | 308 | <code type="php"> |
| | 309 | $q = Doctrine_Query::create() |
| | 310 | ->from('User u') |
| | 311 | ->leftJoin('u.Phonenumbers p WITH p.primary_num = ?', true); |
| | 312 | $users = $q->fetchArray(); |
| | 313 | </code> |
| | 314 | |
| | 315 | Selecting certain columns for optimization |
| | 316 | |
| | 317 | <code type="php"> |
| | 318 | $q = Doctrine_Query::create() |
| | 319 | ->select('u.username, p.phone') |
| | 320 | ->from('User u') |
| | 321 | ->leftJoin('u.Phonenumbers p'); |
| | 322 | $users = $q->fetchArray(); |
| | 323 | </code> |
| | 324 | |
| | 325 | Using wildcards to select all columns |
| | 326 | |
| | 327 | <code type="php"> |
| | 328 | // Select all User columns but only the phone phonenumber column |
| | 329 | $q = Doctrine_Query::create() |
| | 330 | ->select('u.*, p.phone') |
| | 331 | ->from('User u') |
| | 332 | ->leftJoin('u.Phonenumbers p'); |
| | 333 | $users = $q->fetchArray(); |
| | 334 | </code> |
| | 335 | |
| | 336 | Perform DQL delete with simple WHERE |
| | 337 | |
| | 338 | <code type="php"> |
| | 339 | // Delete phonenumbers for user id = 5 |
| | 340 | $deleted = Doctrine_Query::create() |
| | 341 | ->delete() |
| | 342 | ->from('Phonenumber') |
| | 343 | ->addWhere('user_id = 5') |
| | 344 | ->execute(); |
| | 345 | </code> |
| | 346 | |
| | 347 | Perfom simple DQL update for a column |
| | 348 | |
| | 349 | <code type="php"> |
| | 350 | // Set user id = 1 to active |
| | 351 | Doctrine_Query::create() |
| | 352 | ->update('User u') |
| | 353 | ->set('u.is_active', '?', true) |
| | 354 | ->where('u.id = ?', 1) |
| | 355 | ->execute(); |
| | 356 | </code> |
| | 357 | |
| | 358 | Perform DQL update with dbms functions |
| | 359 | |
| | 360 | <code type="php"> |
| | 361 | // Make all usernames lowercase |
| | 362 | Doctrine_Query::create() |
| | 363 | ->update('User u') |
| | 364 | ->set('u.username', 'LOWER(u.username)') |
| | 365 | ->execute(); |
| | 366 | </code> |
| | 367 | |
| | 368 | Using mysql LIKE to search for records |
| | 369 | |
| | 370 | <code type="php"> |
| | 371 | $q = Doctrine_Query::create() |
| | 372 | ->from('User u') |
| | 373 | ->where('u.username LIKE ?', '%jwage%'); |
| | 374 | $users = $q->fetchArray(); |
| | 375 | </code> |
| | 376 | |
| | 377 | Use the INDEXBY keyword to hydrate the data where the key of record entry is the name of the column you |
| | 378 | assign |
| | 379 | |
| | 380 | <code type="php"> |
| | 381 | $q = Doctrine_Query::create() |
| | 382 | ->from('User u INDEXBY u.username'); |
| | 383 | |
| | 384 | $users = $q->fetchArray(); |
| | 385 | print_r($users['jwage']); // Will print the user with the username of jwage |
| | 386 | </code> |
| | 387 | |
| | 388 | Using positional and named parameters |
| | 389 | |
| | 390 | <code type="php"> |
| | 391 | // Positional parameters |
| | 392 | $q = Doctrine_Query::create() |
| | 393 | ->from('User u') |
| | 394 | ->where('u.username = ?', array('Arnold')); |
| | 395 | $users = $q->fetchArray(); |
| | 396 | |
| | 397 | // Named parameters |
| | 398 | $q = Doctrine_Query::create() |
| | 399 | ->from('User u') |
| | 400 | ->where('u.username = :username', array(':username' => 'Arnold')); |
| | 401 | $users = $q->fetchArray(); |
| | 402 | </code> |
| | 403 | |
| | 404 | Using subqueries in your WHERE |
| | 405 | |
| | 406 | <code type="php"> |
| | 407 | // Find uers not in group named Group 2 |
| | 408 | $q = Doctrine_Query::create() |
| | 409 | ->from('User u') |
| | 410 | ->where('u.id NOT IN (SELECT u.id FROM User u2 INNER JOIN u2.Groups g WHERE g.name = ?)', 'Group 2'); |
| | 411 | $users = $q->fetchArray(); |
| | 412 | |
| | 413 | // You can accomplish this without subqueries like the 2 below |
| | 414 | // This is similar as above |
| | 415 | $q = Doctrine_Query::create() |
| | 416 | ->from('User u') |
| | 417 | ->innerJoin('u.Groups g WITH g.name != ?', 'Group 2') |
| | 418 | $users = $q->fetchArray(); |
| | 419 | |
| | 420 | // or this |
| | 421 | $q = Doctrine_Query::create() |
| | 422 | ->from('User u') |
| | 423 | ->leftJoin('u.Groups g') |
| | 424 | ->where('g.name != ?', 'Group 2'); |
| | 425 | </code> |
| | 426 | |
| | 427 | Doctrine has many different ways you can execute queries and retrieve the data. Below is a list of all |
| | 428 | the different ways you can execute queries. |
| | 429 | |
| | 430 | <code type="php"> |
| | 431 | $q = Doctrine_Query::create() |
| | 432 | ->from('User u'); |
| | 433 | |
| | 434 | // Array hydration |
| | 435 | $users = $q->fetchArray(); // Fetch the results as a hydrated array |
| | 436 | $users = $q->execute(array(), Doctrine::HYDRATE_ARRAY); // This is the same as above |
| | 437 | $users = $q->setHydrationMode(Doctrine::HYDRATE_ARRAY)->execute(); // So is this |
| | 438 | |
| | 439 | // No hydration |
| | 440 | $users = $q->execute(array(), Doctrine::HYDRATE_NONE); // Execute the query with plain PDO and no hydration |
| | 441 | $users = $q->setHydrationMode(Doctrine::HYDRATE_NONE)->execute(); // This is the same as above |
| | 442 | |
| | 443 | // Fetch one |
| | 444 | $user = $q->fetchOne(); |
| | 445 | |
| | 446 | // Fetch all and get the first from collection |
| | 447 | $user = $q->execute()->getFirst(); |
| | 448 | </code> |
| | 449 | |