【发布时间】:2018-11-09 21:24:34
【问题描述】:
对于我的项目,我必须导入 12-15 个文件,从 CSV、xml 到 gz。由于所有文件都有自己的结构,我必须为每个文件创建 1 个命令。但这太多了,想要为所有文件创建一个常规导入。
要求:
- 导入脚本应该每小时运行一次
- 导入脚本应该是导入的类别 + 来自导入的产品
- 导入脚本必须检查我的类别是否与导入的类别匹配,如果是,则应将信息添加到 如果不忽略数据库。
- 如果在后台导入,则导入不应影响网站。
我目前的指挥权:
$handle = fopen("https://example.csv",'r');
$output->writeln('Downloading done!');
$categortArray = [];
$categortA = [];
while (($row = fgetcsv($handle, 4096, ";")) !== FALSE)
{
$categortA [ $row[21]] = $row[23] ;
$categortArray [] = $row[21];
}
$output->writeln('Updating category list !');
$result = array_unique($categortArray);
$result2 = array_unique($categortA);
$distributor = $distributor->findByName("Name of Distributor");
foreach ($result as $productgroup) {
$result = $categoryRepository->findByTitle($productgroup);
if (empty($result)){
$category = new Category();
$category->setTitle($productgroup);
$category->setDescription("Category Description");
$category->setDistributor($distributor);
$categoryService->create($category);
}
}
foreach ($result2 as $key => $productgroup) {
$result = $categoryRepository->findByTitle($key);
/** @var $result Category */
if ($result !== null) {
$category = new Category();
$category->setTitle($productgroup);
$category->setParent($result[0]);
$category->setDescription("Name of Distributor");
$category->setDistributor($distributor);
$categoryService->create($category);
}
}
while (($row = fgetcsv($handle, 4096, ";")) !== FALSE)
{
foreach ($child as $c) {
if ($c->getTitle() === $row['21']) {
$product = new Product();
$product->setName($row[1]);
$product->setCategory($categoryTop);
$product->setSku($row[14]);
$product->setEanUpc($categoryTop->getId());
$this->productService->save($product);
}
}
}
我能想到的选项:
- 在 1 个命令中提取所有自动导入脚本并在每个命令中运行它 小时。这个选项并不难,但我认为这不是最好的解决方案,因为它会占用我服务器上的大量资源。
- 结合任务运行器创建一个任务调度程序,我们记录不同步骤的每个事件,例如下载文件、读取文件、检查是否存在、检查类别是否匹配、检查产品是否存在、检查产品库存是否充足迄今为止等。对于这个选项,我想创建自己的调度程序 + 运行程序,但没有太多经验,不知道这是否对我的项目有帮助。由于每个任务都将设置在队列中,因此更容易检查是否有故障
也感谢我对当前 Symfony 命令的建议。
【问题讨论】: