2017-08-08 86 views
0

我正在使用Sonata Admin後端,我想將新圖像字段添加到我的用戶實體(這是一個化身)。既然我已經使用了SonataMediaBundle我跟着這個教程:https://sonata-project.org/blog/2013/10/11/mediabundle-mediatype-improvedSonata Admin Image字段

這裏是我的實體配置:

/** 
* @var \Application\Sonata\MediaBundle\Entity\Media 
* 
* @ORM\ManyToOne(targetEntity="Application\Sonata\MediaBundle\Entity\Media", cascade={"all"}, fetch="LAZY") 
* @ORM\JoinColumn(name="avatar_id", referencedColumnName="id") 
*/ 
protected $avatar; 

不幸的是我有很多問題:

  1. 在我的後端的預覽未顯示: enter image description here
  2. 如果我刪除圖庫中的媒體,編輯用戶時收到此錯誤:Entity of type 'Application\Sonata\MediaBundle\Entity\Media' for IDs id(6) was not found
  3. 所得API(與FOSRestBundle生成)是由客戶機不可用:
"avatar": { 
     "provider_metadata": { 
      "filename": "Test.png" 
     }, 
     "name": "Test.png", 
     "description": null, 
     "enabled": false, 
     "provider_name": "sonata.media.provider.image", 
     "provider_status": 1, 
     "provider_reference": "325564b03489a6473e7c9def01dc58bab611eccb.png", 
     "width": 1430, 
     "height": 321, 
     "length": null, 
     "copyright": null, 
     "author_name": null, 
     "context": "default", 
     "cdn_is_flushable": null, 
     "cdn_flush_at": null, 
     "cdn_status": null, 
     "updated_at": "2017-08-08T12:31:19+02:00", 
     "created_at": "2017-08-08T12:31:19+02:00", 
     "content_type": "image/png", 
     "size": 24978, 
     "id": 7 
    } 

感謝。

回答

0

我解決了所有3個問題!我爲所有遇到同樣困難的人們提供瞭解決方案。

  1. 在我的後端預覽沒有顯示:

由於esplained here我有一個自定義表單控件添加到我的config.yml文件:

twig: 
    # Sonata form themes 
    form_themes: 
     - 'SonataMediaBundle:Form:media_widgets.html.twig' 

並在我的UserAdmin

->with('Profile') 
    ->add('avatar', 'sonata_media_type', array(
     'provider' => 'sonata.media.provider.image', 
     'context' => 'default', 
    )) 
->end() 

現在預覽將顯示:)

  • 如果刪除在畫廊我編輯用戶當收到此錯誤的媒體: 類型的實體「應用程序\索納塔\ MediaBundle \實體\媒體」的標識ID(6)不 發現
  • 正如解釋here我需要在我的實體添加onDelete="SET NULL"

    /** 
    * @var \Application\Sonata\MediaBundle\Entity\Media 
    * 
    * @ORM\ManyToOne(targetEntity="Application\Sonata\MediaBundle\Entity\Media", cascade={"persist"}, fetch="LAZY") 
    * @ORM\JoinColumn(name="avatar_id", referencedColumnName="id", onDelete="SET NULL") 
    */ 
    protected $avatar; 
    
  • (與FOSRestBundle產生)將所得的API是由所述客戶端不可用:
  • 這一次是非常棘手,但我能夠實現自定義的JMS處理程序從this開始。

    我偷看進SonataMediaBundle源代碼,我發現這個片斷:

    /** 
    * Returns media urls for each format. 
    * 
    * @ApiDoc(
    * requirements={ 
    *  {"name"="id", "dataType"="integer", "requirement"="\d+", "description"="media id"} 
    * }, 
    * statusCodes={ 
    *  200="Returned when successful", 
    *  404="Returned when media is not found" 
    * } 
    *) 
    * 
    * @param $id 
    * 
    * @return array 
    */ 
    public function getMediumFormatsAction($id) 
    { 
        $media = $this->getMedium($id); 
    
        $formats = array(MediaProviderInterface::FORMAT_REFERENCE); 
        $formats = array_merge($formats, array_keys($this->mediaPool->getFormatNamesByContext($media->getContext()))); 
    
        $provider = $this->mediaPool->getProvider($media->getProviderName()); 
    
        $properties = array(); 
        foreach ($formats as $format) { 
         $properties[$format]['url'] = $provider->generatePublicUrl($media, $format); 
         $properties[$format]['properties'] = $provider->getHelperProperties($media, $format); 
        } 
    
        return $properties; 
    } 
    

    所以我把它變成我的源和完整的處理程序如下:

    <?php 
    
    namespace AppBundle\Serializer; 
    
    use Application\Sonata\MediaBundle\Entity\Media; 
    use JMS\Serializer\Context; 
    use JMS\Serializer\GraphNavigator; 
    use JMS\Serializer\Handler\SubscribingHandlerInterface; 
    use JMS\Serializer\JsonSerializationVisitor; 
    use Sonata\MediaBundle\Provider\MediaProviderInterface; 
    
    class MediaHandler implements SubscribingHandlerInterface 
    { 
        private $mediaPool; 
    
        public function __construct($mediaPool) 
        { 
         $this->mediaPool = $mediaPool; 
        } 
    
        public static function getSubscribingMethods() 
        { 
         return array(
          array(
           'direction' => GraphNavigator::DIRECTION_SERIALIZATION, 
           'format' => 'json', 
           'type'  => 'Application\Sonata\MediaBundle\Entity\Media', 
           'method' => 'serializeToJson', 
          ), 
         ); 
        } 
    
        public function serializeToJson(JsonSerializationVisitor $visitor, Media $media, array $type, Context $context) 
        { 
         $formats = array(MediaProviderInterface::FORMAT_REFERENCE); 
         $formats = array_merge($formats, array_keys($this->mediaPool->getFormatNamesByContext($media->getContext()))); 
    
         $provider = $this->mediaPool->getProvider($media->getProviderName()); 
    
         $properties = array(); 
         foreach ($formats as $format) { 
          $properties[$format]['url']  = $provider->generatePublicUrl($media, $format); 
          $properties[$format]['properties'] = $provider->getHelperProperties($media, $format); 
         } 
    
         return $properties; 
        } 
    } 
    

    服務設置:

    app.serializer.media: 
        class: AppBundle\Serializer\MediaHandler 
        arguments: 
         - '@sonata.media.pool' 
        tags: 
         - { name: jms_serializer.subscribing_handler } 
    

    這就是全部!

    相關問題