2017-02-13 5 views
2

JMSシリアライザとFOSRestBundleの設定に問題があり、このカスタムリポジトリメソッドをシリアル化しています。JMSシリアライザがFOSRestのカスタムリポジトリメソッドで動作しない

私は$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(); 
    } 
} 
+0

シリアル化が動作するには、配列ではなく実体が必要です。あなたの問題の解決策である@martin answerをチェックしてください。 – malcolm

答えて

5

あなたがgetArrayResult()とあなたの方法から結果を返す場合は、代わりに、エンティティオブジェクトのネストされた配列を生成します。

JMSSerializerは、適切なメタデータをロードするためにどのクラスをシリアライズしているかを知る必要があります。だからおそらくgetResult()を代わりに使うべきです。

+0

どれくらいの時間があれば、私は運動に連れていかれたのか分かりません。ありがとうございました! – Sarcoma

関連する問題