Welcome Guest, Not a member yet? Register   Sign In
Seeder on migration
#1

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 ?
Reply
#2

(This post was last modified: 08-12-2020, 11:43 AM by InsiteFX.)

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.
What did you Try? What did you Get? What did you Expect?

Joined CodeIgniter Community 2009.  ( Skype: insitfx )
Reply




Theme © iAndrew 2016 - Forum software by © MyBB