CodeIgniter Forums
Seeder on migration - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: CodeIgniter 4 (https://forum.codeigniter.com/forumdisplay.php?fid=28)
+--- Forum: CodeIgniter 4 Discussion (https://forum.codeigniter.com/forumdisplay.php?fid=31)
+--- Thread: Seeder on migration (/showthread.php?tid=77293)



Seeder on migration - pippuccio76 - 08-12-2020

Can i insert multiple row while run migration ? 
For example i have a class PopulateUserState

with a run method :

PHP Code:
        public function run()
        {
                $data = [
                        'stato' => 'Da confermare',
                        'atttivo'    => 1
                
];

                // Simple Queries
                $this->db->query("INSERT INTO stato_users (stato, attivo) VALUES(:stato:, :attivo:)",
                        $data
                
);

                // Using Query Builder
                $this->db->table('stato_users')->insert($data);
        }


Can i launch this method on migration ?

Other answer , how can i add pultiple row on seeder run ?


RE: Seeder on migration - InsiteFX - 08-12-2020

You would need to do a insertBatch.

PHP Code:
$this->db->table('blog')->insertBatch([
    [
        'parent_id'  => '0',
        'title'      => 'Test1',
        'body'       => 'This is the body',
        'created_at' => date('Y-m-d H:i:s'),
        'updated_at' => date('Y-m-d H:i:s'),
    ],
    [
        'parent_id'  => '1',
        'title'      => 'Test2',
        'body'       => 'This is another body',
        'created_at' => date('Y-m-d H:i:s'),
        'updated_at' => date('Y-m-d H:i:s'),
    ],
]); 

You may also be able to use a foreach loop if you need to input lots of data from a file or something.