Symfony 1.4 improve doctrine save() method

China☆狼群 提交于 2019-12-01 10:58:36

问题


I have 500 entries in my db. In my backend I have action. For example:

 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() performed for a long time. How to solve this problem? Is it possible?

In my problem part I have all known error Fatal error: Maximum execution time of 30 seconds exceeded in


回答1:


Save each record instead of a collection

$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();
}

or use Doctrine to update the records using a query

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


来源:https://stackoverflow.com/questions/10961743/symfony-1-4-improve-doctrine-save-method

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!