dsgfdgh14569 2016-03-17 08:06
浏览 49
已采纳

Symfony 2 Sonata Media捆绑:保存媒体文件图像,不带奏鸣曲管理员

I have using the Sonata Admin Bundle and Sonata Media Bundle. It's great, but i have the problem about using Media. https://sonata-project.org/bundles/media/2-2/doc/index.html

Example I have a Post Document, in Post I have $image variable with targetDocument is Media. And When I create new Post, I must upload the image file, and before save Post, I must save the upload image file to Media Entity first and then I pointing the $image of Post to the Media Entity just been saved.

Post Document:

<?php
namespace Acme\Bundle\PostBundle\Document;

use Doctrine\ODM\MongoDB\Mapping\Annotations as MongoDB;
use Symfony\Component\Validator\Constraints as Assert;

/**
 * @MongoDB\Document(repositoryClass="Acme\Bundle\PostBundle\Repository\PostRepository")
 */
class Post
{
    /**
     * @MongoDB\Id
     */
    protected $id;

    /**
     * @MongoDB\String
     */
    protected $name;

    /**
     * @MongoDB\ReferenceOne(targetDocument="Application\Sonata\MediaBundle\Document\Media", nullable=true )
     */
    protected $image;

Can we have any way to access the Media Entity and save the upload file to it in Post Document function. I had read this but it not help more.

https://sonata-project.org/bundles/media/master/doc/reference/form.html

  • 写回答

1条回答 默认 最新

  • doufeng1249 2016-03-21 03:09
    关注

    The answer is here:

    In above code, we must save the upload image to media first and then pointed it to the post:

        $mediaManager = $this->container->get('sonata.media.manager.media');
        $media = new Media();
        $media->setBinaryContent($uploadImage);
        $media->setContext('default');
        $media->setProviderName('sonata.media.provider.image');
        $mediaManager->save($media);
        $post->setImage($media);
        print_r($media);exit;
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 R语言Rstudio突然无法启动
  • ¥15 关于#matlab#的问题:提取2个图像的变量作为另外一个图像像元的移动量,计算新的位置创建新的图像并提取第二个图像的变量到新的图像
  • ¥15 改算法,照着压缩包里边,参考其他代码封装的格式 写到main函数里
  • ¥15 用windows做服务的同志有吗
  • ¥60 求一个简单的网页(标签-安全|关键词-上传)
  • ¥35 lstm时间序列共享单车预测,loss值优化,参数优化算法
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值