Doctrine orm JMS序列化程序无法使用FOSRest处理自定义存储库方法
我在设置JMS序列化程序和FOSRestBundle以序列化此自定义存储库方法时遇到问题 如果我使用Doctrine orm JMS序列化程序无法使用FOSRest处理自定义存储库方法,doctrine-orm,symfony,fosrestbundle,jmsserializerbundle,Doctrine Orm,Symfony,Fosrestbundle,Jmsserializerbundle,我在设置JMS序列化程序和FOSRestBundle以序列化此自定义存储库方法时遇到问题 如果我使用$schedules=$em->getRepository('RadioBundle:Schedule')->findAll()它工作正常,但是当我尝试我的自定义方法时,没有一个字段被排除 有人能帮我找出毛病吗 控制器: use RadioBundle\Entity\Schedule; use Symfony\Component\HttpFoundation\Request; use FOS\Re
$schedules=$em->getRepository('RadioBundle:Schedule')->findAll()代码>它工作正常,但是当我尝试我的自定义方法时,没有一个字段被排除
有人能帮我找出毛病吗
控制器:
use RadioBundle\Entity\Schedule;
use Symfony\Component\HttpFoundation\Request;
use FOS\RestBundle\Controller\Annotations\View;
class ScheduleController extends BaseController
{
/**
* @param $date
* @return \Symfony\Component\HttpFoundation\Response
* @View(serializerGroups={"schedule"})
*/
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
);
return $this->handleView($view);
}
}
存储库方法:
class ScheduleRepository extends EntityRepository
{
/**
* @param \DateTime $startDate
* @param \DateTime $endDate
* @return array
*/
public function findByRange(\DateTime $startDate, \DateTime $endDate)
{
$em = $this->getEntityManager();
$qb = $em->createQueryBuilder();
$qb->select('s')
->from('RadioBundle:Schedule', 's')
->leftJoin('s.radioShow', 'rs')
->add(
'where',
$qb->expr()->between(
's.startTime',
':from',
':to'
)
)
->orderBy('s.startTime', 'asc')
->andWhere('rs.isActive = true')
->setParameters(['from' => $startDate, 'to' => $endDate]);
return $qb->getQuery()->getArrayResult();
}
}
如果使用getArrayResult()
从方法返回结果,它将生成嵌套数组而不是实体对象
JMSSerializer需要知道要序列化哪些类以加载适当的元数据。因此,您可能应该改用getResult()
。序列化需要真正的实体而不是数组,请检查@martin answer,这是您问题的解决方案。我不知道我需要多长时间才能完成训练,非常感谢!