如何从Phinx迁移插入中访问自动生成的ID?

时间:2018-11-21 02:40:07

标签: php cakephp phinx

PHP Phinx是CakePHP的数据库Seeder /迁移库。

这将创建一个伪造的用户,但是我将如何创建一个具有FOREIGN KEY(或常规ID引用)到该新用户(即子用户)的id的行? / p>

<?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);
    }
}

1 个答案:

答案 0 :(得分:1)

这应该返回与SELECT LAST_INSERT_ID()等效的内容:

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

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