Using transaction in Joomla

35 views
Skip to first unread message

Viper

unread,
Jun 4, 2013, 10:08:01 PM6/4/13
to joomla-de...@googlegroups.com
I have a couple of queries that I need to execute. But I don't know how.

My code:

$db->setDebug(true);
$db->lockTable('#__countries');
$db->transactionStart();

foreach ($country_ids as $key=>$value) {
// Here is the problem

    $db->setQuery("DELETE FROM ".$db->quoteName('#__countries')." WHERE `country_id` = ".(int)$value." AND `id` = ".(int)$ids[$key]);
    $query = $db->execute();
}

if ($query === true) { // $query returns only last result of execution... fff
    $db->transactionCommit();
} else {
    $db->transactionRollback();
}

$db->unlockTables();
$db->setDebug(false);


What should I write inside the loop to execute the query?

Mark Dexter

unread,
Jun 4, 2013, 10:35:53 PM6/4/13
to joomla-de...@googlegroups.com
One way would be to change it to something like:

$query = true;
foreach ($country_ids as $key=>$value) {
// Here is the problem
$db->setQuery("DELETE FROM ".$db->quoteName('#__countries')."
WHERE `country_id` = ".(int)$value." AND `id` = ".(int)$ids[$key]);
if (!$db->execute())
{
$query = false;
break; // we break out of the loop if any query fails. No sense
in doing more queries if we are going to roll back in any case.
}
}
if ($query == true) ...

Mark
> --
> You received this message because you are subscribed to the Google Groups
> "Joomla! General Development" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to joomla-dev-gene...@googlegroups.com.
> To post to this group, send an email to joomla-de...@googlegroups.com.
> Visit this group at
> http://groups.google.com/group/joomla-dev-general?hl=en-GB.
> For more options, visit https://groups.google.com/groups/opt_out.
>
>
Reply all
Reply to author
Forward
0 new messages