|
9 | 9 |
|
10 | 10 | // ------------------------
|
11 | 11 |
|
12 |
| -$groups = $query->getAll('groups'); |
13 |
| -$count = $groups->getCount(); |
14 |
| -foreach ($groups->getResults() as $group) { |
15 |
| - echo $group['name']; |
16 |
| -} |
| 12 | +$results = $query->select('users')->all(); |
17 | 13 |
|
18 | 14 | // ------------------------
|
19 | 15 |
|
20 |
| -$results = $query->get('users', [['id', '>', 1]])->getResults(); |
21 |
| -var_dump($results); |
| 16 | +$results = $query->select('users')->where([['id', '=', 10]])->one(); |
| 17 | + |
| 18 | +// ------------------------ |
| 19 | + |
| 20 | +$results = $query->select('users')->where([ |
| 21 | + ['id', '>', 1], |
| 22 | + 'and', |
| 23 | + ['group_id', '=', 2], |
| 24 | +])->all(); |
| 25 | + |
| 26 | +// ------------------------ |
| 27 | + |
| 28 | +$results = $query->select('users')->like(['name', '%John%'])->all(); |
| 29 | +// or |
| 30 | +$results = $query->select('users')->where([['name', 'LIKE', '%John%']])->all(); |
| 31 | + |
| 32 | +// ------------------------ |
| 33 | + |
| 34 | +$results = $query->select('users')->notLike(['name', '%John%'])->all(); |
| 35 | +// or |
| 36 | +$results = $query->select('users')->where([['name', 'NOT LIKE', '%John%']])->all(); |
| 37 | + |
| 38 | +// ------------------------ |
| 39 | + |
| 40 | +$results = $query->select('posts') |
| 41 | + ->where([['user_id', '=', 3]]) |
| 42 | + ->offset(14) |
| 43 | + ->limit(7) |
| 44 | + ->all(); |
| 45 | + |
| 46 | +// ------------------------ |
| 47 | + |
| 48 | +$results = $query->select('users', ['counter' => 'COUNT(*)'])->one(); |
| 49 | +// or |
| 50 | +$results = $query->count('users')->one(); |
| 51 | + |
22 | 52 | // ------------------------
|
23 | 53 |
|
24 |
| -$query->get('users', [ |
25 |
| - ['id', '>', 1], |
26 |
| - 'and', |
27 |
| - ['group_id', '=', 2], |
28 |
| -]); |
| 54 | +$results = $query->select(['b' => 'branches'], ['b.id', 'b.name']) |
| 55 | + ->where([['b.id', '>', 1], 'and', ['b.parent_id', '=', 1]]) |
| 56 | + ->orderBy('b.id', 'desc') |
| 57 | + ->all(); |
29 | 58 |
|
30 | 59 | // ------------------------
|
31 | 60 |
|
32 |
| -$query->getFields('posts', ['id', 'category', 'title'], [['views', '>=', 1000]], 'GROUP BY `category`'); |
| 61 | +$results = $query->select('posts', ['id', 'category', 'title']) |
| 62 | + ->where([['views', '>=', 1000]]) |
| 63 | + ->groupBy('category') |
| 64 | + ->all(); |
33 | 65 |
|
34 | 66 | // ------------------------
|
35 | 67 |
|
36 |
| -$query->getFields('users', ['id', 'username', 'email'], [['id', '>', 2]]); |
| 68 | +$groups = $query->select('orders', ['month_num' => 'MONTH(`created_at`)', 'total' => 'SUM(`total`)']) |
| 69 | + ->where([['YEAR(`created_at`)', '=', 2020]]) |
| 70 | + ->groupBy('month_num') |
| 71 | + ->having([['total', '>', 20000]]) |
| 72 | + ->all(); |
37 | 73 |
|
38 | 74 | // ------------------------
|
39 | 75 |
|
40 |
| -$query->getFields('users', ['count' => 'COUNT(id)']); |
| 76 | +$results = $query->select(['u' => 'users'], [ |
| 77 | + 'u.id', |
| 78 | + 'u.email', |
| 79 | + 'u.username', |
| 80 | + 'perms' => 'groups.permissions' |
| 81 | +]) |
| 82 | + ->join('groups', ['u.group_id', 'groups.id']) |
| 83 | + ->limit(5) |
| 84 | + ->all(); |
41 | 85 |
|
42 | 86 | // ------------------------
|
43 | 87 |
|
44 |
| -$query->join( |
45 |
| - ['u' => 'users', 'groups'], |
46 |
| - ['u.id', 'u.email', 'u.username', 'perms' => 'groups.permissions'], |
47 |
| - ['u.group_id', '=', 'groups.id'] |
48 |
| -); |
| 88 | +$results = $query->select(['cp' => 'cabs_printers'], [ |
| 89 | + 'cp.id', |
| 90 | + 'cp.cab_id', |
| 91 | + 'cab_name' => 'cb.name', |
| 92 | + 'cp.printer_id', |
| 93 | + 'printer_name' => 'p.name', |
| 94 | + 'cartridge_type' => 'c.name', |
| 95 | + 'cp.comment' |
| 96 | +]) |
| 97 | + ->join(['cb' => 'cabs'], ['cp.cab_id', 'cb.id']) |
| 98 | + ->join(['p' => 'printer_models'], ['cp.printer_id', 'p.id']) |
| 99 | + ->join(['c' => 'cartridge_types'], 'p.cartridge_id=c.id') |
| 100 | + ->where([['cp.cab_id', 'in', [11, 12, 13]], 'or', ['cp.cab_id', '=', 5], 'and', ['p.id', '>', 'c.id']]) |
| 101 | + ->all(); |
| 102 | + |
| 103 | +// ------------------------ |
| 104 | + |
| 105 | +$new_id = $query->insert('groups', [ |
| 106 | + 'name' => 'Moderator', |
| 107 | + 'permissions' => 'moderator' |
| 108 | +])->go(); |
49 | 109 |
|
50 | 110 | // ------------------------
|
51 | 111 |
|
52 | 112 | $query->insert('groups', [
|
53 |
| - 'name' => 'Moderator', |
54 |
| - 'permissions' => 'moderator' |
55 |
| -]); |
| 113 | + ['name', 'role'], |
| 114 | + ['Moderator', 'moderator'], |
| 115 | + ['Moderator2', 'moderator'], |
| 116 | + ['User', 'user'], |
| 117 | + ['User2', 'user'], |
| 118 | +])->go(); |
| 119 | + |
| 120 | +// ------------------------ |
| 121 | + |
| 122 | +$query->update('users', [ |
| 123 | + 'username' => 'John Doe', |
| 124 | + 'status' => 'new status' |
| 125 | +]) |
| 126 | + ->where([['id', '=', 7]]) |
| 127 | + ->limit() |
| 128 | + ->go(); |
| 129 | + |
| 130 | +// ------------------------ |
| 131 | + |
| 132 | +$query->update('posts', ['status' => 'published']) |
| 133 | + ->where([['YEAR(`updated_at`)', '>', 2020]]) |
| 134 | + ->go(); |
| 135 | + |
| 136 | +// ------------------------ |
| 137 | + |
| 138 | +$query->delete('users') |
| 139 | + ->where([['name', '=', 'John']]) |
| 140 | + ->limit() |
| 141 | + ->go(); |
| 142 | + |
| 143 | +// ------------------------ |
| 144 | + |
| 145 | +$query->delete('comments') |
| 146 | + ->where([['user_id', '=', 10]]) |
| 147 | + ->go(); |
56 | 148 |
|
57 | 149 | // ------------------------
|
58 | 150 |
|
59 |
| -$query->update('users', 7, [ |
60 |
| - 'username' => 'John Doe', |
61 |
| - 'status' => 'new status' |
62 |
| -], 'LIMIT 1'); |
| 151 | +$query->truncate('users')->go(); |
63 | 152 |
|
64 | 153 | // ------------------------
|
65 | 154 |
|
66 |
| -$query->delete('users', [['id', '=', 10]]); |
| 155 | +$query->drop('temporary')->go(); |
0 commit comments