Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
Public Member Functions
BatchInsert Class Reference

Public Member Functions

 __construct (\Magento\Framework\App\ResourceConnection $resourceConnection, $insertIntoTable, $batchSize)
 
 insert (array $dataToInsert)
 
 flush ()
 

Detailed Description

Encapsulate logic that performs batch insert into table

Definition at line 12 of file BatchInsert.php.

Constructor & Destructor Documentation

◆ __construct()

__construct ( \Magento\Framework\App\ResourceConnection  $resourceConnection,
  $insertIntoTable,
  $batchSize 
)
Parameters
\Magento\Framework\App\ResourceConnection$resourceConnection
string$insertIntoTable
int$batchSize

Definition at line 49 of file BatchInsert.php.

53  {
54  $this->resourceConnection = $resourceConnection;
55  $this->insertIntoTable = $insertIntoTable;
56  $this->batchSize = $batchSize;
57  $this->dataStorage = new \SplFixedArray($batchSize);
58  }

Member Function Documentation

◆ flush()

flush ( )

Insert all data form $dataStorage to DB and clear $dataStorage

Returns
void

Definition at line 82 of file BatchInsert.php.

83  {
84  if ($this->currentStorageIndex > 0) {
85  if ($this->currentStorageIndex < $this->batchSize) {
86  $this->dataStorage->setSize($this->currentStorageIndex);
87  }
88 
89  $this->getDbConnection()
90  ->insertArray(
91  $this->insertIntoTable,
92  array_keys(reset($this->dataStorage)),
93  $this->dataStorage->toArray()
94  );
95 
96  $this->dataStorage = new \SplFixedArray($this->batchSize);
97  $this->currentStorageIndex = 0;
98  }
99  }

◆ insert()

insert ( array  $dataToInsert)

Save data to $dataStorage and automatically flush it to DB when storage size becomes equal to $batchSize

Parameters
array$dataToInsert
Returns
void

Definition at line 67 of file BatchInsert.php.

68  {
69  $this->dataStorage[$this->currentStorageIndex] = $dataToInsert;
70  $this->currentStorageIndex++;
71 
72  if ($this->currentStorageIndex >= $this->batchSize) {
73  $this->flush();
74  }
75  }

The documentation for this class was generated from the following file: