Home > Backend Development > PHP Tutorial > Thinkphp5 uses the seeder in composer

Thinkphp5 uses the seeder in composer

PHP中文网
Release: 2023-03-20 10:46:01
Original
2015 people have browsed it

Cause:

A few days ago, the customer asked to build a member question and answer system, so I followed the process. When it came time to call the database data, I felt that a It’s a bit stupid to add this~

Solution process:

After checking the manual and looking at foreign blog cases, I came up with a good method~~~

My usage record has been screenshot:

##Until the latter one displays the time, it means that the seeder is running successfully~

The following is the official process

Seeder Creation

In the Thinkphp5 project, we can enter the following command on the command line:

php think seed:create UserSeeder
Copy after login

Create a

UserSeeder file. After the creation is successful, you can database/seeds See below the directory:

database|-seeds
|-|-UserSeeder.php
Copy after login

The content is as follows:

<?phpuse think\migration\Seeder;class UserSeeder extends Seeder
{    /**
     * Run Method.
     *
     * Write your database seeder using this method.
     *
     * More information on writing seeders is available here:
     * http://docs.phinx.org/en/latest/seeding.html
     */    public function run()
    {

    }
}
Copy after login

The code is very simple, and a

run is given by default Method, now we all know that the seeder file is used to generate simulation data, and the code to generate simulation data can be written in the run method.

Seeder's run method

In the

run method, we can fill in any code that can fill the database. Here I will give you some ideas:

Requirements: Give the data table
Users to simulate and generate 100 pieces of data. The table structure is as follows:

FieldDescriptionnicknamepasswordAfter receiving the request, I can write like this:
nickname
##email<a href="http://www.php.cn/code/114.html" target="_blank"></a>EMAIL
Password
<?phpuse think\migration\Seeder;class UserSeeder extends Seeder
{    /**
     * Run Method.
     *
     * Write your database seeder using this method.
     *
     * More information on writing seeders is available here:
     * http://docs.phinx.org/en/latest/seeding.html
     */    public function run()
    {
        $rows = [];        for ($i = 0; $i < 100; $i++) {
            $rows[] = [                &#39;nickname&#39; => mt_rand(10000, 99999),                &#39;email&#39; => mt_rand(10000, 99999).&#39;@qq.com&#39;,                &#39;password&#39; => md5(&#39;123456&#39;),
            ];
        }        $this->table(&#39;users&#39;)->insert($rows)->save();
    }
}
Copy after login

Note: Be sure to call the
save()

method, otherwise it will not be saved.

First, I generated 100 pieces of data, and then called
$this->table('users')->insert($rows)->save();

Insert the generated data into the Users table of the database. Is not it simple? ^ - ^.Execute Seeder

After the Seeder file is defined, the data must be executed before it can be inserted into the database. We can execute it like this:

php think seed:run
Copy after login

Execute Success tip:

All Done. Took 0.0552s

. You can see:



##php think seed:run

The above is the detailed content of Thinkphp5 uses the seeder in composer. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template