使用 symfony 表单验证器进行批量插入

发布于 2024-11-17 20:22:45 字数 747 浏览 2 评论 0原文

我收到用户发来的 CSV 文件,我想将其插入数据库。我想使用表单验证:

$pf = new ProductForm();
$old_memory_usage = 0;

while($data = $csvreader->read())
{
  $mem = memory_get_usage() / 1024;
  echo "Memory-Usage: " . $mem . " KB; Mem-Diff: " . $mem - $old_memory_usage . "\n";
  $old_memory_usage = $mem;

  $p = new Product();
  $p->fromArray($data);
  $pf->bind($p->toArray(), array());
  if($pf->isValid())
  {
    $p->save();
  }
  else
  {
    //display error message to the user
  }
}

当 isValid() 返回 true 时,效果很好。内存差异为 0 KB。但如果“表单”中存在错误,则内存差异为 6-7 KB。因此,当 CSV 文件非常大时,我会收到 allowed_memory 错误。

我尝试释放表格:

unset($pf)
$pf = null;
$pf = new ProductForm();

没有成功。情况更糟!

和想法? 谢谢!

i receive an CSV File from the User and I want to insert it into the Database. I want to use the Form Validation:

$pf = new ProductForm();
$old_memory_usage = 0;

while($data = $csvreader->read())
{
  $mem = memory_get_usage() / 1024;
  echo "Memory-Usage: " . $mem . " KB; Mem-Diff: " . $mem - $old_memory_usage . "\n";
  $old_memory_usage = $mem;

  $p = new Product();
  $p->fromArray($data);
  $pf->bind($p->toArray(), array());
  if($pf->isValid())
  {
    $p->save();
  }
  else
  {
    //display error message to the user
  }
}

This works fine when isValid() returns true. The Memory Difference is 0 KB. But if there is an Error in the "form" the memory-difference is 6-7 KB. So I get an allowed_memory error when the CSV-File is very huge.

I tryed to free the form like:

unset($pf)
$pf = null;
$pf = new ProductForm();

no success. It is just worse!

ans ideas?
thx!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

如痴如狂 2024-11-24 20:22:45

您可以在数据库 yml 中禁用学说分析器:

  dev:
    doctrine:
      class: sfDoctrineDatabase
      param:
        profiler: false

You can disable doctrine profiler in your databse yml:

  dev:
    doctrine:
      class: sfDoctrineDatabase
      param:
        profiler: false
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文