how to insert multi row to database ?


Active member
I have line code. I want insert 1 array to db.

$tests = array(
    '1' => 'asdas',
    '2' => 'asdas',
    '3' => 'asdas',
    '4' => 'asdas',
foreach($tests as $test){
    $testid = $db->insert('******_testcontent', array(
        'testname'    => $test
    $db->insert('******_tests', array(
        'testcontentid'    => $testid,
        'threadid'    => $threadid,
        'dateline'    => XenForo_Application::$time,

this is code i use model to save. But i want using datawriter save all content .
How to use dw insert array to db ?

Thank !!!

Jake Bunce

XenForo moderator
Staff member
Here is a function from one of my addons which calls on a datawriter to save an array of data:

	public function saveOptions(array $node)
		if (!$node['node_id'])

		$dw = XenForo_DataWriter::create('NodesAsTabs_DataWriter_Options');

		$existing = $this->getOptionsById($node['node_id']);
		if (!empty($existing['node_id']))


The setExistingData() function in the datawriter is used to indicate if you are updating an existing record or not. Then you can use the bulkSet() function to set the array values to be written. The array keys are the names of the database columns.