简体   繁体   中英

Typo3 - bulkInsert from a scheduled command - where should the code reside?

I have created an extension using Extension Builder. This has created my necessary table and repository and controllers files.

I have created a command class with a method that is called once a day (via the scheduler extension). The purpose of the command is to truncate a table and replace the data with a bulk insert of new rows.

I have found example code for a bulkInsert however I am not sure where I should put it?

I originally assume putting it in the Controller was the logical solution, but perhaps there is a better place to put it? Or a better way to run a bulk insert?

A bulk insert requires me to provide the table name, ie where "testTable" is the table name I could make the connection like so:

$connection = GeneralUtility::makeInstance(ConnectionPool::class)->getConnectionForTable('testTable');

...but connecting to the table name like this doesn't feel right as the rest of the extension utilises the repository. It feels a bit disjointed.

Can anybody give me any guidance on where and how I should do this to keep my source code nice and tight.

The solution was to make a separate instance of the table connection like so:

$connection = GeneralUtility::makeInstance(ConnectionPool::class)->getConnectionForTable($tblname);

and then to utilise the "bulkInsert" tool, passing over the table name (again), an array of records and an array of the associated field names

$connection->bulkInsert($tblname, $records, $fieldnames);

It would be nice to have bulkInsert available directly via the respository but I'm sure there is some logical reason why that isn't possible. But this solution works well anyway.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM