如何Yii2 faker数据库关系

时间:2016-07-18 07:35:21

标签: php database orm yii2 faker

如何使用 Yii2 Faker 为ORM表创建虚假记录? 例如:

User:
 -id
 -email
 -password
 -auth_key
 -password_reset_token
 -name
 -family
 -user_img
 -is_admin

users_detail:
 -id
 -user_id
 -phone_number
 -country
 -city
 -facebook_url
 -googleplus_url
 -linkedin_url
 -instagram_url
 -comment


app.tests.codeception.unit.templates.fixtures个文件代码:

user.php的

return [
    "email" => $faker->email,
    "password" => Yii::$app->getSecurity()->generatePasswordHash('password_' . $index),
    "auth_key" => Yii::$app->getSecurity()->generateRandomString(),
    "password_reset_token" => Yii::$app->getSecurity()->generateRandomString(),
    "name" => $faker->firstName,
    "family" => $faker->lastName,
    "user_img" => $faker->image(Yii::getAlias("@app/web/uploads/users"), 200, 200, null, false),
    "is_admin" => $faker->randomElement(array("1", "0"))
]


user_details.php

return [
    "user_id" => ??? I don't know ???,
    "phone_number" => $faker->phoneNumber,
    "country" => $faker->country,
    "city" => $faker->city,
    "facebook_url" => $faker->optional()->url,
    "googleplus_url" => $faker->optional()->url,
    "linkedin_url" => $faker->optional()->url,
    "instagram_url" => $faker->optional()->url,
    "comment" => $faker->optional()->realText()
];

我需要为用户表中的某个用户使用faker为user_details表创建数据

1 个答案:

答案 0 :(得分:2)

对我有用的解决方案是控制主记录的id生成,并在引用字段中也使用此值。

user.php faker定义:

<?php
/**
 * @var $faker \Faker\Generator
 * @var $index integer
 */

$security = Yii::$app->getSecurity();

return [
    'id'            => $index + 1,
    'username'      => $faker->userName,
    'email'         => $faker->email,
    'auth_key'      => $security->generateRandomString(),
    'password_hash' => $security->generatePasswordHash( 'password_' . $index ),
    //'password_reset_token' => $security->generateRandomString() . '_' . time(),
    'confirmed_at'  => time(),
    //'last_login_at' => time(),
    'flags'         => 0,
    'created_at'    => time(),
    'updated_at'    => time(),
];

相关的token.php faker定义:

<?php
/**
 * @var $faker \Faker\Generator
 * @var $index integer
 */

$security = Yii::$app->getSecurity();

return [
    'user_id' => $index + 1,
    'code' => $security->generateRandomString(),
    'created_at' => time(),
    'type' => 1,
];

$ index变量默认为0. MySQL不喜欢ID 0值,因此需要添加+1。

这将生成user.php fixture,如:

<?php

return [
    'user0' => [
        'id' => 1,
        'username' => 'arjun.lindgren',
        'email' => 'lquitzon@mitchell.com',
        'auth_key' => 'yDL6BzlyxBOAFbK15uQ-Fx_AVEqZhE06',
        'password_hash' => '$2y$13$SRyNY1ZceQdVMjDFEYaNxeTs0yr5raTgRsquzh6Czom/0loAX9C3W',
        'confirmed_at' => 1506074296,
        'flags' => 0,
        'created_at' => 1506074296,
        'updated_at' => 1506074296,
    ],
    'user1' => [
        'id' => 2,
        'username' => 'jaqueline.bartoletti',
        'email' => 'stehr.reginald@schoen.org',
        'auth_key' => 'hk1pE_TUoUmqWcxoArOPmRb-t2P7LvKq',
        'password_hash' => '$2y$13$IsQxrCaLX4ficjulKKSEbucgLfTyyFSLle30efejBw1giKhwILGSm',
        'confirmed_at' => 1506074296,
        'flags' => 0,
        'created_at' => 1506074296,
        'updated_at' => 1506074296,
    ],
];

并且token.php:     

return [
    'token0' => [
        'user_id' => 1,
        'code' => 'nr3oU86ukLjb1oKIlKDBgnj0bWb96mNH',
        'created_at' => 1506074295,
        'type' => 1,
    ],
    'token1' => [
        'user_id' => 2,
        'code' => 'TTLcxuS_d_sYDME9m7VShbDsOMEVBqZq',
        'created_at' => 1506074295,
        'type' => 1,
    ],
];

不幸的是,生成的别名不会与记录ID同步。可能应该将yii2-faker扩展名更改为从1开始修复此问题。