how to insert multi row to database ?

vinavb

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

PHP:
$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 !!!
 
Here is a function from one of my addons which calls on a datawriter to save an array of data:

Code:
	public function saveOptions(array $node)
	{
		if (!$node['node_id'])
		{
			return;
		}

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

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

		$dw->bulkSet($node);

		$dw->save();
	}

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.
 
Top Bottom