Insert multiple rows using a single query
Asked Answered
L

7

9

Can Joomla's DB object add multiple rows at once? MySQL can do this like so:

INSERT INTO x (a,b)
VALUES 
 ('1', 'one'),
 ('2', 'two'),
 ('3', 'three')

But can Joomla's own functions achieve the same thing in a single query? Currently I am doing a loop to insert each row (same table) in separate query. Not a good idea when dealing with tons of rows at once.

Lydia answered 8/4, 2011 at 6:54 Comment(2)
How Are you inserting it in Joomla?Farly
$this->store($data,'tablename') - using the Jtable instanceLydia
S
7

In your model you can do this:

$db = $this->getDBO();
$query = "
  INSERT INTO x (a,b)
  VALUES 
  ('1', 'one'),
  ('2', 'two'),
  ('3', 'three')
";
$db->setQuery($query);
$db->query();

If you are outside your model you need to get the DB object like so:

$db = JFactory::getDBO();
Shanty answered 8/4, 2011 at 9:4 Comment(2)
Yeah this is what I did. The problem is, I have to do two separate queries for INSERT and UPDATE. With JTable instance, it will choose automatically depending on presence of PRIMARY KEY.Lydia
I thought you said you have to do 2 separate queries INSERT and UPDATE. Then why would JTable have to choose? Surely it would also need to do both INSERT and UPDATE as well, unless there is something you have failed to mention about what you want to do.Shanty
R
6

You can use:

$db = JFactory::getDbo();
$query = $db->getQuery(true); // !important, true for every new query

$query->insert('#__table_name'); // #__table_name = databse prefix + table name
$query->set('`1`="one"');
$query->set('`2`="two"');
$query->set('`3`="three"');
/* or something like this:
$query->columns('`1`,`2`,`3`');
$query->values('"one","two","three"');
*/

$db->setQuery($query);
$db->query();

and $db->insertId() can return you autoinc id if you have one.

Rentsch answered 2/4, 2012 at 23:13 Comment(2)
I've corrected the answer tested with $query->columns, $query->values as it was initially wrong.Liveried
This works very well, except if you try it on 1000's of rows, then I would rather suggest Martin's solution up top for efficiency.Nodical
R
5

Try this, if you have values in an array :

$query = $this->db->getQuery(true);
$query->insert($this->db->quoteName('#__table_name'));
$query->columns($this->db->quoteName(array('col_1','col_2','col_3','col_4')));

for($i=0; $i < lengthOfArray; $i++)
{
    $values= $arr_1[$i].','.$this->db->quote($arr_2[$i]).','.$this->db->quote($arr_3[$i]).','. $arr_4[$i];
    $query->values($values);
}
$this->db->setQuery($query);
$result = $this->db->query();
Restorative answered 16/3, 2013 at 16:48 Comment(0)
F
2

You don't need $db = $this->getDBO();

just use this:-

$query = "
  INSERT INTO x (a,b)
  VALUES 
  ('1', 'one'),
  ('2', 'two'),
  ('3', 'three')
";

$this->_db->setQuery($query);
$this->_db->query();
Filippo answered 19/3, 2012 at 7:29 Comment(0)
K
2

Try this:

$db = JFactory::getDbo();
$query = $db->getQuery(true);

$query->insert('x');
$query->columns('a,b');
$query->values('1', 'one');
$query->values('2', 'two');
$query->values('3', 'three');

$db->setQuery($query);
$db->query();

A description of "values" method

Adds a tuple, or array of tuples that would be used as values for an INSERT INTO statement.
Usage:
$query->values('1,2,3')->values('4,5,6');
$query->values(array('1,2,3', '4,5,6'));

Kelleher answered 11/3, 2013 at 0:12 Comment(0)
E
2

In latest version of Joomla!, you can use it's own DB class as follows. Remember to use 'quoteName()' and 'quote()' functions as you needed.

$dbo = JFactory::getDbo();
$query = $dbo->getQuery(true);

$columns = array('col_one','col_two', 'col_three');
$values = array();

//if you need, here you can use forloop/foreach loop to populate the array
$values[] = 'val_1, val_2, val_3'; // first row values
$values[] = 'val_4, val_5, val_6'; // second row values
...

$query->insert($dbo->quoteName('#__table_name'));
$query->columns($columns);

$query->values($values);

$dbo->setQuery($query);
$dbo->query();

Hope this saves your time. Thanks. Happy coding! :)

Enlighten answered 13/10, 2014 at 17:55 Comment(0)
J
0
...
$columns = array('user_id', 'type', 'object', 'lvl', 'date');
$values  = array();
foreach ($batch as $row) {
    $array    = array(
        $row->user_id,
        $db->quote($row->type),
        $db->quote($row->object),
        $db->quote($row->lvl),
        $db->quote($row->date),
    );
    $values[] = implode(',', $array);
}
$query->insert($db->quoteName('#activity_log'));
$query->columns($db->quoteName($columns));
$query->values($values);

$db->setQuery($query);
$result = $db->execute();
Jer answered 24/7, 2018 at 15:14 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.