S
S
skajtersen2017-06-15 09:42:42
Yii
skajtersen, 2017-06-15 09:42:42

How to accumulate data before writing to tables?

There is an array of rows - $rows, each row of the array is passed to the aggregator function.

public function uploader($rows)
{
foreach ($rows as $row => $data) {
                $this->aggregator($data);
            }
...
}

In the aggregator function, numerous actions are performed on each row, after which different parts of the rows are written to tables.
public function aggregator($data)
{
...
table1->column1=data[1];
...
table1->column10=date[10];
table1->save();
...
table2->column1=data[11];
table2->column2=table1->column3;
...
table2->save();

}

The problem is that for a very large $rows array, writing takes a long time. I'm interested in how you can accumulate records into an array in the aggregator function, and then write down any certain number of rows in the uploader function. Some kind of batchInsert is needed, but it's not entirely clear how to write it in this situation.

Answer the question

In order to leave comments, you need to log in

3 answer(s)
M
Maxim Fedorov, 2017-06-15
@skajtersen

In the aggregator function, numerous actions are performed on each row, after which different parts of the rows are written to tables.

Bad, the method should be responsible only for the implementation of one logical action, and not for all at once
Normally, nothing ... you can, for example, use global or similar options - but this will be bad code.
Better divide the responsibility of the methods, like this:
class MyClass
{
    public function uploader($rows)
    {
        $items = [];
        foreach ($rows as $row => $data) {
            $items[] = $this->aggregator($data);
        }
        $this->save($items)
    }

    public function aggregator($data)
    {
        // выполнение нужных операций над данными
    }

    public function save($items)
    {
        // сохранение массива данных, аля batchInsert
    }
}

D
davidnum95, 2017-06-15
@davidnum95

Use transactions?

M
Maxim Timofeev, 2017-06-15
@webinar

Need some kind of batchInsert

Well, it is in yii:
www.yiiframework.com/doc-2.0/yii-db-command.html#b...
https://stackoverflow.com/questions/27355262/activ...
www.yiiframework.com /doc-2.0/yii-db-batchqueryresu...

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question