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 解决一个加好友限制问题 或者有好的方案
  • ¥15 关于#java#的问题,请各位专家解答!
  • ¥15 急matlab编程仿真二阶震荡系统
  • ¥20 TEC-9的数据通路实验
  • ¥15 ue5 .3之前好好的现在只要是激活关卡就会崩溃
  • ¥50 MATLAB实现圆柱体容器内球形颗粒堆积
  • ¥15 python如何将动态的多个子列表,拼接后进行集合的交集
  • ¥20 vitis-ai量化基于pytorch框架下的yolov5模型
  • ¥15 如何实现H5在QQ平台上的二次分享卡片效果?
  • ¥30 求解达问题(有红包)