1

PHP Phinx是来自 CakePHP 的数据库播种/迁移库。

这将创建一个假用户,但是我将如何创建一个对新用户(即子用户)具有FOREIGN KEY(或常规 ID 引用)的行?id

<?php

use Phinx\Seed\AbstractSeed;

class UserSeeder extends AbstractSeed
{
    public function run()
    {
        $faker = Faker\Factory::create();
        $data = [];
        for ($i = 0; $i < 100; $i++) {
            $data[] = [
                'username'      => $faker->userName,
                'password'      => sha1($faker->password),
                'password_salt' => sha1('foo'),
                'email'         => $faker->email,
                'first_name'    => $faker->firstName,
                'last_name'     => $faker->lastName,
                'created'       => date('Y-m-d H:i:s'),
            ];
        }

        $this->insert('users', $data);
    }
}
4

1 回答 1

6

这应该返回相当于SELECT LAST_INSERT_ID()

$this->getAdapter()->getConnection()->lastInsertId();

参考:https ://github.com/cakephp/phinx/issues/819

于 2018-11-21T02:50:09.497 回答