开发者

Symfony 1.4 save all items' froms with one action

I want to create thumbnails for 200+ objects with one action in Symfony 1.4. The problem is that thmbnail generation takes place on saving the form.

class AuthorForm extends BaseAuthorForm
{
  public function configure()
  {
    /* some configs */
  }

  public function save($con = null)
  {      
    /* create thmbnail from original picture */ 
  }

}

How can I write an (batch) action to be able to save them all at once, rather than going to each item in the backend a开发者_如何学Pythonnd saving?

Please note, that just $author->save(); won't work, of course.

Thanks!


You have to fetch the objects, loop through them, create the form and save. Like the following.

$authors = Doctrine_Core::getTable('Author')->findAll();
foreach($authors as $author){
    $form = new AuthorForm($author);
    $form->save();
}

You'll probably have memory issues if you're running it on a hosting plan (not your dev machine). A better way to get thumbnails is using a plugin like sfImageTransformExtraPlugin (http://www.symfony-project.org/plugins/sfImageTransformExtraPlugin) that generates a cached thumbnail as you need them. You don't even need to go through the trouble of generating the thumbnails. And still can have multiple thumbnail versions of the same photo pretty easily.

If you still need to use this way, do some unset stuff during the loop, like the following.

$authors = Doctrine_Core::getTable('Author')->findAll();
foreach($authors as $author){
    $form = new AuthorForm($author);
    $form->save();
    unset($form, $author);
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜