|
| 1 | +Executing Queries |
| 2 | +################# |
| 3 | + |
| 4 | +Queries can be executed with the ``execute()`` and ``query()`` methods. The |
| 5 | +``execute()`` method returns the number of affected rows whereas the |
| 6 | +``query()`` method returns the result as a |
| 7 | +`CakePHP Statement <https://book.cakephp.org/5/en/orm/database-basics.html#interacting-with-statements>`_. Both methods |
| 8 | +accept an optional second parameter ``$params`` which is an array of elements, |
| 9 | +and if used will cause the underlying connection to use a prepared statement:: |
| 10 | + |
| 11 | + <?php |
| 12 | + |
| 13 | + use Migrations\BaseMigration; |
| 14 | + |
| 15 | + class MyNewMigration extends BaseMigration |
| 16 | + { |
| 17 | + /** |
| 18 | + * Migrate Up. |
| 19 | + */ |
| 20 | + public function up(): void |
| 21 | + { |
| 22 | + // execute() |
| 23 | + $count = $this->execute('DELETE FROM users'); // returns the number of affected rows |
| 24 | + |
| 25 | + // query() |
| 26 | + $stmt = $this->query('SELECT * FROM users'); // returns PDOStatement |
| 27 | + $rows = $stmt->fetchAll(); // returns the result as an array |
| 28 | + |
| 29 | + // using prepared queries |
| 30 | + $count = $this->execute('DELETE FROM users WHERE id = ?', [5]); |
| 31 | + $stmt = $this->query('SELECT * FROM users WHERE id > ?', [5]); // returns PDOStatement |
| 32 | + $rows = $stmt->fetchAll(); |
| 33 | + } |
| 34 | + |
| 35 | + /** |
| 36 | + * Migrate Down. |
| 37 | + */ |
| 38 | + public function down(): void |
| 39 | + { |
| 40 | + |
| 41 | + } |
| 42 | + } |
| 43 | + |
| 44 | +Fetching Rows |
| 45 | +============= |
| 46 | + |
| 47 | +There are two methods available to fetch rows. The ``fetchRow()`` method will |
| 48 | +fetch a single row, whilst the ``fetchAll()`` method will return multiple rows. |
| 49 | +Both methods accept raw SQL as their only parameter:: |
| 50 | + |
| 51 | + <?php |
| 52 | + |
| 53 | + use Migrations\BaseMigration; |
| 54 | + |
| 55 | + class MyNewMigration extends BaseMigration |
| 56 | + { |
| 57 | + /** |
| 58 | + * Migrate Up. |
| 59 | + */ |
| 60 | + public function up(): void |
| 61 | + { |
| 62 | + // fetch a user |
| 63 | + $row = $this->fetchRow('SELECT * FROM users'); |
| 64 | + |
| 65 | + // fetch an array of messages |
| 66 | + $rows = $this->fetchAll('SELECT * FROM messages'); |
| 67 | + } |
| 68 | + |
| 69 | + /** |
| 70 | + * Migrate Down. |
| 71 | + */ |
| 72 | + public function down(): void |
| 73 | + { |
| 74 | + |
| 75 | + } |
| 76 | + } |
| 77 | + |
| 78 | +Inserting Data |
| 79 | +============== |
| 80 | + |
| 81 | +Migrations makes it easy to insert data into your tables. Whilst this feature is |
| 82 | +intended for the :doc:`seed feature <seeding>`, you are also free to use the |
| 83 | +insert methods in your migrations:: |
| 84 | + |
| 85 | + <?php |
| 86 | + |
| 87 | + use Migrations\BaseMigration; |
| 88 | + |
| 89 | + class NewStatus extends BaseMigration |
| 90 | + { |
| 91 | + /** |
| 92 | + * Migrate Up. |
| 93 | + */ |
| 94 | + public function up(): void |
| 95 | + { |
| 96 | + $table = $this->table('status'); |
| 97 | + |
| 98 | + // inserting only one row |
| 99 | + $singleRow = [ |
| 100 | + 'id' => 1, |
| 101 | + 'name' => 'In Progress' |
| 102 | + ]; |
| 103 | + |
| 104 | + $table->insert($singleRow)->saveData(); |
| 105 | + |
| 106 | + // inserting multiple rows |
| 107 | + $rows = [ |
| 108 | + [ |
| 109 | + 'id' => 2, |
| 110 | + 'name' => 'Stopped' |
| 111 | + ], |
| 112 | + [ |
| 113 | + 'id' => 3, |
| 114 | + 'name' => 'Queued' |
| 115 | + ] |
| 116 | + ]; |
| 117 | + |
| 118 | + $table->insert($rows)->saveData(); |
| 119 | + } |
| 120 | + |
| 121 | + /** |
| 122 | + * Migrate Down. |
| 123 | + */ |
| 124 | + public function down(): void |
| 125 | + { |
| 126 | + $this->execute('DELETE FROM status'); |
| 127 | + } |
| 128 | + } |
| 129 | + |
| 130 | +.. note:: |
| 131 | + |
| 132 | + You cannot use the insert methods inside a `change()` method. Please use the |
| 133 | + `up()` and `down()` methods. |
| 134 | + |
0 commit comments