@@ -123,6 +123,163 @@ Phinx只能回滚以下命令:
123123
124124### Up 方法
125125
126+ up方法会在Phinx执行迁移命令时自动执行,并且该脚本之前并没有执行过。你应该将修改数据库的方法写在这个方法里。
127+
128+ ### Down 方法
129+
130+ down方法会在Phinx执行回滚命令时自动执行,并且该脚本之前已经执行过迁移。你应该将回滚代码放在这个方法里。
131+
132+ ## 执行查询
133+
134+ 可以使用 ` execute() ` 和 ` query() ` 方法进行查询。` execute() ` 方法会返回查询条数,` query() ` 方法会返回结果。结果参照 [ PDOStatement] ( http://php.net/manual/en/class.pdostatement.php )
135+
136+ ```
137+ <?php
138+
139+ use Phinx\Migration\AbstractMigration;
140+
141+ class MyNewMigration extends AbstractMigration
142+ {
143+ /**
144+ * Migrate Up.
145+ */
146+ public function up()
147+ {
148+ // execute()
149+ $count = $this->execute('DELETE FROM users'); // returns the number of affected rows
150+
151+ // query()
152+ $rows = $this->query('SELECT * FROM users'); // returns the result as an array
153+ }
154+
155+ /**
156+ * Migrate Down.
157+ */
158+ public function down()
159+ {
160+
161+ }
162+ }
163+ ```
164+
165+ ## 获取数据
166+
167+ 有2个方法可以获取数据。 ` fetchRow() ` 方法可以返回一条数据, ` fetchAll() ` 可以返回多条数据。2个方法都可以使用原生 SQL 语句作为参数。
168+
169+ ```
170+ <?php
171+
172+ use Phinx\Migration\AbstractMigration;
173+
174+ class MyNewMigration extends AbstractMigration
175+ {
176+ /**
177+ * Migrate Up.
178+ */
179+ public function up()
180+ {
181+ // fetch a user
182+ $row = $this->fetchRow('SELECT * FROM users');
183+
184+ // fetch an array of messages
185+ $rows = $this->fetchAll('SELECT * FROM messages');
186+ }
187+
188+ /**
189+ * Migrate Down.
190+ */
191+ public function down()
192+ {
193+
194+ }
195+ }
196+ ```
197+
198+ ## 插入数据
199+
200+ Phinx 可以很简单的帮助你在表中插入数据。尽管这个功能也在 [ seed] ( /database-seeding.md ) 中实现了。你也可以在迁移脚本中实现插入数据。
201+
202+ ```
203+ <?php
204+
205+ use Phinx\Migration\AbstractMigration;
206+
207+ class NewStatus extends AbstractMigration
208+ {
209+ /**
210+ * Migrate Up.
211+ */
212+ public function up()
213+ {
214+ // inserting only one row
215+ $singleRow = [
216+ 'id' => 1,
217+ 'name' => 'In Progress'
218+ ]
219+
220+ $table = $this->table('status');
221+ $table->insert($singleRow);
222+ $table->saveData();
223+
224+ // inserting multiple rows
225+ $rows = [
226+ [
227+ 'id' => 2,
228+ 'name' => 'Stopped'
229+ ],
230+ [
231+ 'id' => 3,
232+ 'name' => 'Queued'
233+ ]
234+ ];
235+
236+ // this is a handy shortcut
237+ $this->insert('status', $rows);
238+ }
239+
240+ /**
241+ * Migrate Down.
242+ */
243+ public function down()
244+ {
245+ $this->execute('DELETE FROM status');
246+ }
247+ }
248+ ```
249+
250+ > 不能在 _ change\(\) _ 方法中使用插入数据,只能在 _ up\(\) _ 和 _ down\(\) _ 中使用
251+
252+ ## 操作数据表
253+
254+ ### Table 对象
255+
256+ Table对象是Phinx中最有用的API之一。它可以让你方便的用 PHP 代码操作数据库。我们可以通过 ` table() ` 方法取到Table对象。
257+
258+ ```
259+ <?php
260+
261+ use Phinx\Migration\AbstractMigration;
262+
263+ class MyNewMigration extends AbstractMigration
264+ {
265+ /**
266+ * Migrate Up.
267+ */
268+ public function up()
269+ {
270+ $table = $this->table('tableName');
271+ }
272+
273+ /**
274+ * Migrate Down.
275+ */
276+ public function down()
277+ {
278+
279+ }
280+ }
281+ ```
282+
126283
127284
128285
0 commit comments