我正在使用 Sonata 管理后端,我想向我的用户实体(即头像)添加一个 新图像字段。因为我已经在使用
SonataMediaBundle
,所以我遵循了本教程:https://sonata-project.org/blog/2013/10/11/mediabundle-mediatype-improved
这是我的实体配置:
/**
* @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;
不幸的是我有很多问题:
Entity of type 'Application\Sonata\MediaBundle\Entity\Media' for IDs id(6) was not found
"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 }
我解决了所有3个问题!我在这里为所有有同样困难的人提供我的解决方案。
- 在我的后端中未显示预览:
正如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()
现在将显示预览:)
- 如果我删除图库中的媒体,我在编辑用户时会收到此错误:实体类型 ID id(6) 的“Application\Sonata\MediaBundle\Entity\Media”不是 找到了
正如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;
- 客户端无法使用生成的 API(使用 FOSRestBundle 生成):
这个非常棘手,但我能够从 this 帖子开始实现自定义 JMS 处理程序。
我查看了
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 }
仅此而已!