Pomm2 的 Faker API
dev-master / 1.0.x-dev
2017-11-16 19:21 UTC
Requires
- fzaninotto/faker: ~1.4
- pomm-project/foundation: 2.*@dev
Requires (Dev)
- atoum/atoum: ~1.0
This package is auto-updated.
Last update: 2024-09-07 19:03:40 UTC
README
此包是 Pomm2 客户端,用于使用 Faker 生成的数据填充表。
此包仍在开发中,因此没有测试,可能存在错误。
安装
要安装 Pomm2Faker,只需在您的 composer.json
文件的 require-dev
部分中添加以下行
"pragmafabrik/pomm-faker": "1.0-dev"
然后执行 composer.phar update
命令。
使用方法
使用客户端相当简单
<?php use PommProject\Foundation\Pomm; use PragmaFabrik\Pomm\Faker\FakerPooler; use Faker\Factory as FakerFactory; $loader = require __DIR__.'/vendor/autoload.php'; $pomm = new Pomm(['my_db' => ['dsn' => 'pgsql://user:pass@host:port/db_name']]); // register Faker Pooler to the session $session = $pomm ->getDefaultSession() ->registerClientPooler(new FakerPooler(FakerFactory::create())) ; /* Configure how we want data to be generated in the table `student`. We do not want to insert any data for the `student_id` field nor the `created_at`, they are both set by the database and will be returned by the insert query. We can use Faker's formatters directly using the `setFormatterType` method or create a custom generator with the `setDefinition` method. The untouched field `last_section` will be set to NULL by default. */ $session->getFaker('student')->getRowDefinition() ->unsetDefinition('student_id') ->setFormatterType('first_name', 'firstName') ->setFormatterType('last_name', 'lastName') ->setFormatterType('birthdate', 'dateTime', ['18 years ago']) ->setFormatterType('section', 'randomElement', [['A1', 'A2', 'A3']]) ->setFormatterType('login', 'lastName') ->setDefinition('password', function($generator) { return sprintf("pass%s", $generator->format('password')); }) ->unsetDefinition('created_at') ; // Populate the database with 10 students and return the rows converted values. $students = $session->getFaker('student')->save(10);
复杂结构和批量类。
有时,结构可能相当复杂,具有交叉外键约束。在这种情况下,可以使用 ModelLayer 利用 Postgres 的事务机制。此 Pooler 由 ModelManager 包 提供。