无法显示选择字段类型元素。如何在Sulu表单中正确添加自定义实体表示形式?

时间:2020-07-02 13:41:32

标签: symfony sulu

在Sulu documentation之后,我尝试添加选择字段类型以以管理员形式显示集合类型资源(Room对象)。目前,我可以从列表中选择这些元素,

select element

selected element

但是在表单提交并重新加载后,即使Room对象存在于事件实体中,也无法显示任何记录:

empty list

我做错了什么?

代码:

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>

1 个答案:

答案 0 :(得分:0)

您的api响应应仅包含rooms的房间ID的清单。假设您有一个具有Event属性的$rooms实体,请将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();
        });
    }
}