dqnhfbc3738 2013-05-18 21:41
浏览 39
已采纳

Doctrine FIxtures和Symfony2请求范围

I want to create Article fixtures for my project which uses Doctrine2 and Symfony 2.2.

Here is how articles are created:

They don't link to images directly but instead they contain image names. Before saving an article my ArticleManager parses article text, finding image names, searching for those images in database and replacing image markup part with real image path, for example.

This is article content
typed in form
and it contains an
![Image description](Here is My Awesome Image Name)

Then when form is submitted and ArticleManager->save($article) is called, article manager changes image markup by real file WEB path:

This is article content
typed in form
![Image description](/path/to/my_awesome_image.png)

The problem: ArticleManager relies on Assetic assets helper service to build full web image paths and that service resides in request scope. On the other hand, Doctrine fixtures are ran from CLI so they can't access that service, making me unable to get image paths when loading article fixture.

Can anyone suggest me the least hackish way of tackling this problem?

  • 写回答

1条回答 默认 最新

  • dongsu3654 2013-05-19 11:45
    关注

    Ok, thanks to @cheesemacfly's comment I digged a bit and realized that since my Images are stored in public folder I don't have to use assetic at all!

    Instead of using it to create Image URLs I injected router service into my ArticleManager and generated URLs this way:

    $baseRoute = $this->router->getContext()->getBaseUrl();
    
    $appFiles = array('/app.php', '/app_dev.php');
    $baseRoute = str_replace($appFiles, '', $baseRoute);
    
    $imageDocumentRoute = $baseRoute . '/' . $imageDocument->getWebPath();
    

    imageDocument->getWebPath() returns image name appended to subfolder where images are saved. E.g. subweb/path/imagename.png.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 c程序不知道为什么得不到结果
  • ¥40 复杂的限制性的商函数处理
  • ¥15 程序不包含适用于入口点的静态Main方法
  • ¥15 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置