0

按照 Sulu文档,我尝试添加选择字段类型以在管理表单中显示集合类型资源(房间对象)。目前,我可以从列表中选择这些元素,

选择元素

选定的元素

但是在表单提交和重新加载后,任何记录都无法显示,尽管 Room 对象存在于 Event 实体中:

空列表

我做错了什么?

代码:

sulu_admin.yaml

sulu_admin:
...

# Registering Selection Field Types in this section
field_type_options:
    selection:
        room_selection:
            default_type: list_overlay
            resource_key: rooms
            types:
                list_overlay:
                    adapter: table
                    list_key: rooms
                    display_properties:
                        - name
                    icon: su-clock
                    label: 'app.rooms'
                    overlay_title: 'app.rooms'

event_details.xml

        <property name="rooms" type="room_selection">
             <meta>
                 <title>app.rooms</title>
             </meta>
        </property>
4

1 回答 1

0

您的 api 响应应该只包含rooms. 假设您有一个Event具有$rooms属性的实体,请将 a 添加public function getRoomIds() {}到您的Event实体并将@VirtualProperty("rooms")注释添加到该方法。可能您必须将@Exclude注释添加到public function getRooms() {}

编辑:

我刚试了一下,如果你有类似下面的实体,它会按预期工作。如果仍然无法正常工作,则可能是您的 jms 序列化程序配置有问题。

/**
 * @Serializer\ExclusionPolicy("all")
 */
class Event
{
    /**
     * @Serializer\Expose()
     */
    private $otherProperty;

    /**
     * @var Collection<int, Room>
     */
    private $rooms;

    public function getRooms(): Collection
    {
        return $this->rooms;
    }

    /**
     * @Serializer\VirtualProperty()
     * @Serializer\SerializedName("rooms")
     */
    public function getRoomIds(): array
    {
        return $this->rooms->map(function (Room $room) {
            return $room->getId();
        });
    }
}
于 2020-07-08T11:05:41.837 回答