在 JMS Serializer 和 FOSRestBundle v2.1 中使用 @Groups 批注时为空 JSON 数组

Empty JSON Array when using @Groups annotation with JMS Serializer and FOSRestBundle v2.1

我正在尝试使用 @Groups 注释来序列化我的数据,但我只收到一个空数组。

我试过在我想显示的字段上使用 @Serializer\Expose,但它们随处可见并忽略了组。

从我读过的内容来看,我很确定我应该只需要使用 @Groups 注释,但我最终得到了这个(有两个计划实体):

{
  "schedules": [
    {},
    {}
  ]
}

这应该如何工作似乎有所改变,我找到了以前版本 FOSRestBundle 的示例,但我无法将它们移植到新版本。

据我了解,您可以在序列化程序上下文中设置组并使用它来设置视图上下文,但我没有成功。

https://github.com/FriendsOfSymfony/FOSRestBundle/blob/master/UPGRADING-2.0.md

View::setSerializationContext and View::getSerializationContext have been removed. Use View::setContext and View::getContext together with the new Context class instead.

Before:

use JMS\Serializer\SerializationContext;

$view = new View();
$context = new SerializationContext();
$view->setSerializationContext($context);
$context = $view->getSerializationContext();

After:

use FOS\RestBundle\Context\Context;

$view = new View();
$context = new Context();
$view->setContext($context);
$context = $view->getContext();

我很难理解这一切,如果能给我指明正确方向的任何帮助,我将不胜感激。

以及以下我尝试 use FOS\RestBundle\Controller\Annotations as Rest; @Rest\View(serializerGroups({"schedule"}) 控制器上的注释,它与我现在看到的效果相同 (ie none).

调度控制器

use FOS\RestBundle\Context\Context;

class ScheduleController extends BaseController
{
    /**
     * @param $date
     * @return \Symfony\Component\HttpFoundation\Response
     */
    public function getSchedulesScheduleAction($date)
    {

        $em = $this->getDoctrine()->getManager();
        list($startDate, $endDate) = $this->get('radio.utils.date_and_time')->findWeekRange($date);
        $schedules = $em->getRepository('RadioBundle:Schedule')->findByRange($startDate, $endDate);

        $view = $this->view(
            [
                'schedules' => $schedules,
            ],
            200
        );

        $context = new Context();
        $context->setGroups(["schedule"]);
        $view->setContext($context);

        return $this->handleView($view);
    }
}

安排实体

use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\ORM\Mapping as ORM;
use Symfony\Component\Validator\Constraints as Assert;
use JMS\Serializer\Annotation as Serializer;

/**
 * Schedule
 *
 * @ORM\Table(name="schedule")
 * @ORM\Entity(repositoryClass="RadioBundle\Repository\ScheduleRepository")
 * @Serializer\ExclusionPolicy("all")
 */
class Schedule
{
    /**
     * @var int
     *
     * @ORM\Column(name="id", type="integer")
     * @ORM\Id
     * @ORM\GeneratedValue(strategy="AUTO")
     */
    private $id;

    /**
     * @var \DateTime
     * @Assert\NotBlank()
     * @Serializer\Groups({"schedule"})
     * @ORM\Column(name="start_time", type="datetime")
\     */
    private $startTime;

    /**
     * @var \DateTime
     * @Assert\NotBlank()
     * @Serializer\Groups({"schedule"})
     * @ORM\Column(name="end_time", type="datetime")
     */
    private $endTime;



    /**
     * @Assert\NotBlank()
     * @Serializer\Groups({"schedule"})
     * @ORM\ManyToOne(targetEntity="RadioBundle\Entity\RadioShow", inversedBy="schedule")
     */
    private $radioShow;

    ...

app/config.yml

fos_rest:
    param_fetcher_listener: true
    body_listener: true
    format_listener: true
    view:
        view_response_listener: 'force'
    routing_loader:
        default_format: json

jms_serializer:
    metadata:
        auto_detection: true

在实体上使用 ExclusionPolicy("all") 后,您需要向群组公开 属性。

例如

   /**
     * @var \DateTime
     * @Assert\NotBlank()
     * @Serializer\Groups({"schedule"})
     * @Serializer\Expose()
     * @ORM\Column(name="start_time", type="datetime")
     */
    private $startTime;