trOnGAtE

The 'Insert Batch' Method
The 'Insert Batch' method can be used for performing bulk database table inserts. This method accepts the following arguments:
- $table (required) - the name of the table where new records are to be inserted.
- $records (required) - I have no idea!?
What Gets Returned?
When the 'Insert Batch' method has been successfully invoked, it will return an integer, representing the number of new rows that have been inserted.
Example 1
The syntax below inserts three rows into a members table and then outputs the number of rows inserted.
<?php
class Members extends Trongate {
function test() {
$row_data['first_name'] = 'Archie';
$row_data['last_name'] = 'MacPherson';
$row_data['trongate_user_id'] = 92;
$data[] = $row_data;
$row_data['first_name'] = 'Dougie';
$row_data['last_name'] = 'Donnolly';
$row_data['trongate_user_id'] = 93;
$data[] = $row_data;
$row_data['first_name'] = 'Hugh';
$row_data['last_name'] = 'Keevins';
$row_data['trongate_user_id'] = 94;
$data[] = $row_data;
$num = $this->model->insert_batch('members', $data);
echo $num; //outputs 3
}
Example 2
Below is a more advanced example. Here, we take a JSON string of characters and use PHP's built-in json_decode() method to turn the JSON string into an array. However, since each row in our array is represented by a PHP object, we loop through the rows and covert each row into an array. With our $data array finally prepared, we then perform a batch insert and output the number of rows added.
function test() {
$json_str = '[
{
"first_name": "Archie",
"last_name": "MacPherson",
"trongate_user_id": 92
},
{
"first_name": "Dougie",
"last_name": "Donnolly",
"trongate_user_id": 93
},
{
"first_name": "Hugh",
"last_name": "Keevins",
"trongate_user_id": 94
}
]';
$data = json_decode($json_str);
foreach($data as $key => $value) {
$data[$key] = (array) $value;
}
$num = $this->model->insert_batch('members', $data);
echo $num; //outputs 3
}
HELP & SUPPORT
If you have a question or a comment relating to anything you've see here, please goto the Help Bar.