简体   繁体   English

Typo3-通过计划的命令执行bulkInsert-代码应位于何处?

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

I have created an extension using Extension Builder. 我已经使用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? 我找到了bulkInsert的示例代码,但是我不确定应该放在哪里?

I originally assume putting it in the Controller was the logical solution, but perhaps there is a better place to put it? 我最初认为将其放入Controller是合理的解决方案,但也许有更好的放置方法了? 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: 批量插入需要我提供表名,即“ testTable”是表名,我可以像这样建立连接:

$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 然后利用“ bulkInsert”工具,再次传递表名,记录数组和关联的字段名数组

$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. 可以直接通过存储库直接使用bulkInsert很好,但是我敢肯定有一些逻辑原因无法实现。 But this solution works well anyway. 但是无论如何,这种解决方案都行之有效。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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