PHP code example of liryan / dbfiller

1. Go to this page and download the library: Download liryan/dbfiller library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

liryan / dbfiller example snippets


providers=[
    ...
    Dbfiller\DBFillerProvider::class,
],

...
$app->register(Dbfiller\DBFillerProvider::class);


return[
    '表1'=>
    [
        'total'=>'要生成多少数据',
        'key'=>'此表的主键字段名字,默认为id',
        'define'=>[
            '字段名1'=>['format'=>'格式说明','from'=>'引用数据']  
            '字段名2'=>['format'=>'格式说明','from'=>'引用数据']  
            ...
        ]
    ],
    '表2'=>
    [
        'total'=>'要生成多少数据',
        'key'=>'此表的主键字段名字,默认为id',
        'define'=>[
            '字段名1'=>['format'=>'格式说明','from'=>'引用数据']  
            '字段名2'=>['format'=>'格式说明','from'=>'引用数据']  
            //不写字段,则会按照数据库中定义的数据类型自动生成
            ...
        ]
    ],
    ...
];

        'format'=>function($row){  //$row :目前生成的数据集,值传递,不要修改,在字段需要与字段产生关系的时候调用
                    return mt_rand(time()-30*24*3600,time())  //时间为最近一年某一刻
                  }

return [
    'member'=>[
        'total'=>10000,
        'key'=>'id',
        'define'=>[
            'avatar'=>['format'=>'http://%8-6s.com/%6s/%10-5s.jpg'],
            'name'=>['format'=>'%16-4s'],
        ],
    ],

    'address'=>[
        'total'=>5000,
        'key'=>'id',
        'define'=>[
            'userid'=>['format'=>'',from=>'#.member.id'] //数据来自上面member表单字段,member一定要先生成
            'address'=>['format'=>'%32-16s'],
            'postcode'=>['format'=>function($row){
                $table=[100012,100013,100023,200010];    //随机返回一个数据
                return $table[mt_rand(0,count($table)-1)];
            }],
        ],
    ],
];