Can DataWriter be used for saving multiple records?

simunaqv

Well-known member
Hello,
I have a question regarding DataWriter. The save function of DataWriter is used for saving fields in one row of the database table. Is there any mechanism in place for providing field values for several recordsets and then bulk saving?
 
I read the question two different ways, can you be more specific on what you want to do? If you are setting the same values for each row you can save it to an array and call, for example, $dw->bulkSet($dwInput);
 
I read the question two different ways, can you be more specific on what you want to do? If you are setting the same values for each row you can save it to an array and call, for example, $dw->bulkSet($dwInput);

No, I want to save different differernt values in each record.
 
I think he means, that he want to save MORE entries at once.
For example:

PHP:
$user1 = array('username', 'pw', 'mail',...);
$user2 = array('username2, ...);
$user3 = array('userdata...);

$addusers = array($user1, $user2, $user3);

$writer = XenForo_DataWriter::create('XenForo_DataWriter_User');
$writer->addMany($addusers);
$writer->save();

AFAIK it's not possible.
 
This is not possible with DataWriters because they work at row level. They can only be used to add, delete, verify or manipulate data for a single row at a time. If you want to create multiple records using a DataWriter, create a new DW & save() it for each row individually.

PHP:
XenForo_Db::beginTransaction();

foreach ($records as $record)
{
	$dw = XenForo_DataWriter::create('Foo_DataWriter_Bar');

	$dw->bulkSet($record);
	$dw->save();
}

XenForo_Db::commit();


If you are going to reuse this code heavily in multiple locations (different controllers/actions, for example); you can move it to the corresponding Model. And use the model instead of writing this code over and over.

PHP:
$barModel = XenForo_Model::create('Foo_Model_Bar');
$barModel->insertMultipleBars($records);
 
Top Bottom