【问题标题】:Symfony 1.4 improve doctrine save() methodSymfony 1.4 改进了学说 save() 方法
【发布时间】:2012-06-13 06:34:57
【问题描述】:

我的数据库中有 500 个条目。在我的后端,我有行动。例如:

 public function executeMyAction(sfWebRequest $request) {

 // Get some data from table
 $templates = Doctrine_Core::getTable('SeoTemplates')->findOneByEntity('training');

//Get data from other table(500 items)
 $trainings = Doctrine::getTable('Training')->getTraining();

  // Make some operations with data
  foreach ($trainings as $training) {

       $training->setSomeValue1('some_data');
       $training->setSomeValue2('some_data');
       $training->setSomeValue2('some_data');

  }

// Problem part (try to save)
$trainings->save();
}

save() 执行了很长时间。如何解决这个问题呢?有可能吗?

在我的问题部分,我有所有已知错误致命错误:超过 30 秒的最大执行时间

【问题讨论】:

    标签: performance symfony1 doctrine symfony-1.4 doctrine-1.2


    【解决方案1】:

    保存每条记录而不是集合

    $templates = Doctrine_Core::getTable('SeoTemplates')->findOneByEntity('training');
    $trainings = Doctrine::getTable('Training')->getTraining();
    foreach ($trainings as $training) {
       $training->setSomeValue1('some_data');
       $training->setSomeValue2('some_data');
       $training->setSomeValue2('some_data');
       $training->save();
    }
    

    或使用 Doctrine 通过查询更新记录

    $q = Doctrine_Query::create()
       ->update('TABLE')
       ->set($val1, '?', $val1)
       ->set($val2, '?', $val2)
       ->set($val3, '?', $val3)
       ->where('id = ?', $id)
       ->execute();
    

    【讨论】:

    • 谢谢!第二种方法很快:-)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-09-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多