Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Save 20 users with one SQL query?

Tags:

sql

php

doctrine

i save a user like this in doctrine:

$user = User();
$user->name = 'peter';
$user->save();

is there a way to save 20 users in one sql query?

or do i have to loop the above code 20 times hence creating 20 sql queries?

thanks

like image 383
never_had_a_name Avatar asked Jan 23 '23 02:01

never_had_a_name


2 Answers

You may add your $user objects to Doctrine_Collection and then call $collection->save(), which basically does the loop for you.

like image 167
takeshin Avatar answered Jan 31 '23 16:01

takeshin


From your point of view as a Doctrine user, you are working with objects, and should not be concerned by SQL queries.

If you have 20 different users, then, you should have 20 different objects -- which means, yes, 20 queries (or more, depending on what your objects are doing) to save them.

like image 44
Pascal MARTIN Avatar answered Jan 31 '23 15:01

Pascal MARTIN