我解決了所有3個問題!我爲所有遇到同樣困難的人們提供瞭解決方案。
- 在我的後端預覽沒有顯示:
由於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 }
這就是全部!