forked from codeigniter4/CodeIgniter4
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path006.php
More file actions
48 lines (40 loc) · 1.29 KB
/
006.php
File metadata and controls
48 lines (40 loc) · 1.29 KB
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
47
48
<?php
namespace App\Database\Seeds;
use CodeIgniter\Database\Seeder;
class BookSeeder extends Seeder
{
public function run()
{
// Define author data and insert
$authorData = [
['name' => 'Frank Herbert'],
['name' => 'William Gibson'],
['name' => 'Ursula K. Le Guin'],
];
$this->db->table('authors')->insertBatch($authorData);
// Get all inserted authors, keyed by name for easy lookup
$authors = $this->db->table('authors')
->get()
->getResultArray();
$authorsByName = array_column($authors, 'id', 'name');
// Define books with author references
$books = [
[
'title' => 'Dune',
'author_id' => $authorsByName['Frank Herbert'],
'year' => 1965,
],
[
'title' => 'Neuromancer',
'author_id' => $authorsByName['William Gibson'],
'year' => 1984,
],
[
'title' => 'The Left Hand of Darkness',
'author_id' => $authorsByName['Ursula K. Le Guin'],
'year' => 1969,
],
];
$this->db->table('books')->insertBatch($books);
}
}