XF 2.2 insertBulk - updateBulk?

Robert9

Well-known member
Is there something like an updateBulk like we have insertBulk?

instead of

foreach(...)
$db->update('table',
['field' => 'value', ] , 'key_field= ?', $key_field_value);
 
Writing your own loop is the most appropriate in this case.

Inserting by bulk is a much easier and clearer use case as we're just telling MySQL to take this number of records and insert them, plus we can avoid conflicts by updating or replacing data automatically when duplicate keys are detected. This can happen in a single query.

Unless you're intending to affect all or multiple records during an update, each update likely needs its own where conditions and that's not something that can happen in a single query so multiple queries are required.
 
Worth mentioning that if you rely on all data to be on the same state (either pre- or post-update), use a transaction to make sure an error doesn't leave you in a partially updated state.
 
Worth mentioning that if you rely on all data to be on the same state (either pre- or post-update), use a transaction to make sure an error doesn't leave you in a partially updated state.
Unfortunately i have no idea, what to do with that.

I fetch data from somewhere to newArray
Then i fetch what i have in oldArray.

Then i decide to update or insert with comparing both arrays.

for insert i use now:

Code:
        $db = \XF::db();
        if ($newKeys)
        {
            $insert = [];
            foreach ($newKeys AS $key => $value)
            {
                $insert[] = [
                    'team_id' => NULL,
                    'server_team_id' => $key,
                    'server_team_name' => substr($value['strTeamName'],0,29),
                ];
            }

            $db->insertBulk('xf_soccer_team', $insert, false, 'team_id = VALUES(team_id)');
        }

The update will come in a minute or two ...
 
Top Bottom