-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathseed.php
46 lines (34 loc) · 1.83 KB
/
seed.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
<?php
require('vendor/autoload.php');
$pdo = new PDO('mysql:host=localhost;dbname=blogpoo;charset=utf8', 'root', '', [
PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION,
PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC
]);
$articleQuery = $pdo->prepare("INSERT INTO articles SET title = :title, slug = :slug, created_at = :created_at, introduction = :introduction, content = :content");
$commentQuery = $pdo->prepare("INSERT INTO comments SET article_id = :article_id, author = :author, created_at = :created_at, content = :content");
$userQuery = $pdo->prepare("INSERT INTO users SET first_name = :first_name, last_name = :last_name, email = :email");
$pdo->query('DELETE FROM articles; DELETE FROM comments; DELETE FROM users;');
$faker = Faker\Factory::create('fr_FR');
for ($u = 0; $u < 30; $u++) {
$first_name = $faker->firstName();
$last_name = $faker->lastName();
$email = $faker->email();
$userQuery->execute(compact('first_name', 'last_name', 'email'));
}
for ($a = 0; $a < 20; $a++) {
$title = $faker->catchPhrase;
$slug = $faker->slug;
$created_at = $faker->dateTimeBetween('-6 months')->format('Y-m-d H:i:s');
$introduction = $faker->paragraph();
$content = "<p>" . implode("</p><p>", $faker->paragraphs(5)) . "</p>";
$articleQuery->execute(compact('title', 'slug', 'created_at', 'introduction', 'content'));
$article_id = $pdo->lastInsertId();
$days = "-" . (new DateTime())->diff(new DateTime($created_at))->days . " days";
$commentsCount = mt_rand(2, 8);
for ($c = 0; $c < $commentsCount; $c++) {
$created_at = $faker->dateTimeBetween($days)->format('Y-m-d H:i:s');
$content = "<p>" . implode("</p><p>", $faker->paragraphs(5)) . "</p>";
$author = $faker->userName;
$commentQuery->execute(compact('created_at', 'content', 'author', 'article_id'));
}
}