Doctrine orm 将@Groups注释与JMS序列化程序和FOSRestBundle v2.1一起使用时,JSON数组为空
我试图使用Doctrine orm 将@Groups注释与JMS序列化程序和FOSRestBundle v2.1一起使用时,JSON数组为空,doctrine-orm,symfony,fosrestbundle,jmsserializerbundle,Doctrine Orm,Symfony,Fosrestbundle,Jmsserializerbundle,我试图使用@Groups注释来序列化我的数据,但我只是收到一个空数组 我试过在我想显示的字段上使用@Serializer\Expose,但是它们会到处显示,并且忽略组 从我所读到的内容来看,我非常确定我应该只需要使用@Groups注释,但我最终得到了这个注释(有两个计划实体): 这似乎已经改变了它的工作方式,我已经找到了以前版本FOSRestBundle的示例,我只是无法将它们移植到新版本 据我所知,您可以在序列化程序上下文中设置组,并使用它来设置视图上下文,但我没有运气做到这一点 View:
@Groups
注释来序列化我的数据,但我只是收到一个空数组
我试过在我想显示的字段上使用@Serializer\Expose
,但是它们会到处显示,并且忽略组
从我所读到的内容来看,我非常确定我应该只需要使用@Groups
注释,但我最终得到了这个注释(有两个计划实体):
这似乎已经改变了它的工作方式,我已经找到了以前版本FOSRestBundle的示例,我只是无法将它们移植到新版本
据我所知,您可以在序列化程序上下文中设置组,并使用它来设置视图上下文,但我没有运气做到这一点
View::setSerializationContext和View::getSerializationContext已被删除。将View::setContext和View::getContext与新的上下文类一起使用
之前:
use JMS\Serializer\SerializationContext;
$view = new View();
$context = new SerializationContext();
$view->setSerializationContext($context);
$context = $view->getSerializationContext();
之后:
use FOS\RestBundle\Context\Context;
$view = new View();
$context = new Context();
$view->setContext($context);
$context = $view->getContext();
我正在经历一段非常痛苦的时光,我想知道这一切,我非常感谢任何能为我指明正确方向的帮助
以及以下内容,我尝试使用FOS\RestBundle\Controller\Annotations作为Rest代码>和
@Rest\View(serializerGroups({“schedule”})
在控制器上使用注释,其效果与我现在看到的相同(即无)
调度控制器
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
使用实体上的ExclutionPolicy(“全部”),您需要向组公开属性
例如
使用实体上的ExclutionPolicy(“全部”),您需要向组公开属性
例如
你所做的看起来与我使用上下文的方式相同,只是我使用$context->addGroup('group');而不是setGroups。我也使用expose与groups组合,并且它可以像广告一样工作。我发现,自从我发布它以来,我尝试了$context->addGroup('schedule')
,$context->addGroups(['schedule'])
和$context->setGroups(['schedule'])
仍然没有任何结果。不过我会再试一次,从那以后我已经完成了一些事情。非常感谢。使用$context->setGroups(['schedule'])就成功了
,我还需要在@Group
字段上使用@Serializer\Expose
。如果您想发布,我可以接受这一回答。没问题,很乐意帮助,完成了!您所做的与我使用上下文的方式相同,只是我使用$context->addGroup('Group'));而不是setGroups。我也使用expose与groups结合使用,它可以像广告中那样工作。我发现,自从我发布它后,我尝试了$context->addGroup('schedule')
,$context->addGroups(['schedule'))
和$context->setGroups(['schedule'])
还是没什么。不过我会再试一次,从那以后我已经做了一些事情。非常感谢。这就成功了,使用$context->setGroups(['schedule'])
,我还需要在@Group
字段上使用@Serializer\Expose
。如果你想发布它,我可以接受这个答案。没问题,很乐意帮忙,完成了!
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
/**
* @var \DateTime
* @Assert\NotBlank()
* @Serializer\Groups({"schedule"})
* @Serializer\Expose()
* @ORM\Column(name="start_time", type="datetime")
*/
private $startTime;