symfony - 列表中的速录媒体展示

标签 symfony sulu

我创建了一个实体 PlayerInfo。在其中,我有照片字段并定义了 MediaInterface,并将其与它一起使用。在 adminUI 中,一切都显示正常,在前端也是如此,但是当我想在列表中显示缩略图时,我得到空字段,并且控制台中出现错误:

Invalid type given: "number". "object" is needed. (TRANSFORMER ERROR)

这是我的列表 XML 属性代码:

<property name="avatar" visibility="always" translation="sulu_admin.avatar" sortable="false">
    <field-name>id</field-name>
    <entity-name>SuluMediaBundle:Media</entity-name>
    <joins>
        <join>
            <entity-name>SuluMediaBundle:Media</entity-name>
            <field-name>App\Entity\PlayerInfo.photo</field-name>
        </join>
    </joins>

    <transformer type="thumbnails"/>
</property>

可能是什么问题?

最佳答案

Sulu ListView 的 table 适配器使用 transformer 根据 API 返回的数据确定应在单元格内呈现的内容。

您已经在代码中使用了thumbnails 转换器。如果您查看 ThumbnailFieldTransformer implementation ,您会看到转换器需要格式如下的数据:

{
    "sulu-40x40":"/uploads/media/sulu-40x40/02/2-isle.jpg?v=1-0",
}

我怀疑您已使用 Sulu 的 ListBuilder 组件在实现您的 API 的 Controller 中收集该列表数据(如 sulu-demo project )。 ListBuilder 组件使用优化查询直接从数据库加载请求的数据。

遗憾的是,ListBuilder 组件仅返回关联媒体实体的 ID,不会自动将数据转换为 ThumbnailFieldTransformer 所需的格式。您需要使用 MediaManagerInterface::getFormatUrls method 在 Controller 中构造和设置包含缩略图 url 的对象。 .

举个例子,你可以看看 organization logo is handled in the AccountController实现Sulu内置组织列表的API:

$ids = \array_filter(\array_column($accounts, 'logo'));
$logos = $this->mediaManager->getFormatUrls($ids, $locale);
foreach ($accounts as $key => $account) {
    if (\array_key_exists('logo', $account) && $account['logo'] && \array_key_exists($account['logo'], $logos)) {
        $accounts[$key]['logo'] = $logos[$account['logo']];
    }
}

关于symfony - 列表中的速录媒体展示,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/67821553/

相关文章:

php - Sulu CMS GET-请求错误

symfony - Symfony2 路由中的两个可选变量

symfony - 访问 Sulu 前端中的安全服务 : Hashing a Users password

symfony - 如何在Symfony2中测试基于子域的路由

mysql - Symfony 显示 blob 值

php - SuluArticleBundle 抛出 undefined index : article

symfony - Sulu:添加新的locatlistation导致权限错误

security - Symfony2 in_memory 用户提供程序问题

php - Doctrine 让实体在数小时内分发