2016-12-06 3 views
2

symfonyの3.1.7 + FOSRestBundle最新バージョンの私の記事のエンティティで次にグループ注釈が

<?php 
namespace PM\ApiBundle\Controller; 

... 
use FOS\RestBundle\Controller\Annotations as Rest; 
use FOS\RestBundle\View\View; 

class ArticlesController extends FOSRestController 
{ 
    /** 
    * @ApiDoc(
    * section="articles", 
    * resource=true, 
    * description="Get articles published" 
    *) 
    * @Rest\View(serializerGroups={"article"}) 
    * @Rest\Get("/articles") 
    */ 
    public function getArticlesAction(Request $request) 
    { 
     $articles = $this->getDoctrine() 
      ->getManager() 
      ->getRepository('PMPlatformBundle:Article') 
      ->findAllDateDesc(); 
     /* @var $articles Article[] */ 
     return $articles; 
    } 

に動作していない私は、右の使用文で、この注釈@Groups({「記事を」})を追加しました。

私が取得聖霊降臨祭のデフォルト・シリアライザ:

[ 
    [], 
    [] 
] 

聖霊降臨祭JMSシリアライザ(バンドル)私が手:

{ 
    "0": {}, 
    "1": {} 
} 

(私はデシベルでの2件の記事を持っている) それは "記事" グループのように思えます認識されません。この注釈なしでデフォルトのシリアライザを使用すると、循環エラーが発生します。

どういうところが間違っていますか?

[EDIT]

/** 
* @ApiDoc(
* section="articles", 
* resource=true, 
* description="Get articles published" 
*) 
* @Rest\View() 
* @Rest\Get("/articles") 
*/ 
public function getArticlesAction(Request $request) 
{ 
    $context = new Context(); 
    $context->addGroup('article'); 

    $articles = $this->getDoctrine() 
     ->getManager() 
     ->getRepository('PMPlatformBundle:Article') 
     ->findAllDateDesc(); 
    /* @var $articles Article[] */ 
    $view = $this->view($articles, 200); 
    $view->setContext($context); 

    return $view; 
} 

まだ空の応答と同じ動作。

+0

findAllDateDesc()メソッドが返すものを教えてください。(var_dumpまたはprint_r)/エンティティまたはシリアライザ設定のコードを貼り付けてください。 –

+0

私のメソッドfindAllDateDesc()はうまく動作します(そしてdebbugingのために、神は!var_dumpまたはprint_rを使用しません、personnaly私はxdebug ..を使用します)、私のエンティティはここではシリアライザも問題ありません。私は私の質問に答え、私はこの問題をJMSシリアライザで解決します。いずれにせよ助けてくれてありがとう! –

答えて

2

[OK]を私はこのようにJMSシリアライザを使用してそれを修正:

use JMS\Serializer\SerializationContext; 
use JMS\Serializer\SerializerBuilder; 


class ArticlesController extends FOSRestController 
{ 
    /** 
    * @ApiDoc(
    * section="articles", 
    * resource=true, 
    * description="Get articles published" 
    *) 
    * @Rest\View() 
    * @Rest\Get("/articles") 
    */ 
    public function getArticlesAction(Request $request) 
    { 
     $serializer = SerializerBuilder::create()->build(); 

     $articles = $this->getDoctrine() 
      ->getManager() 
      ->getRepository('PMPlatformBundle:Article') 
      ->findAllDateDesc(); 
     /* @var $articles Article[] */ 


     return $serializer->serialize($articles, 'json', SerializationContext::create()->setGroups(array('article'))); 
    } 

今のグループの注釈が正常に動作します。私には、文字列としてJSONを返す

あなたの提案答え

$serializer = SerializerBuilder::create()->build(); 
return $serializer->serialize($collection, 'json', SerializationContext::create()->setGroups(['group'])); 
を使用して

+0

$ serializer = $ this-> get( 'jms_serializer.serializer')を使用してjmsSerializerサービスを取得できます – Arno

0

。あなたの問題を解決するために、あなたはconfig.ymlファイルに次のように持っている必要があります:

jms_serializer: 
    metadata: 
    auto_detection: true 

これは、それはあなたが実体ファイルであなたのアノテーションで指定したマッピングをどのように認識するかです。その詳細はこちらdocs。それで、@View(serializerGroups={"group"})アノテーションを必要に応じて使用することができます。

P.S. Symfony 3.2.2; JMSSerializer 1.1; FOSRestBundle 2.1

1

symfonyのデフォルトシリアライザをそのまま使用できます。 JMSSerializerは不要です。

あなたは(https://symfony.com/doc/current/serializer.html#using-serialization-groups-annotations)config.ymlにシリアライザの注釈を有効にする

#app/config/config.yml 
framework: 
    .... 
    serializer: { enable_annotations: true } 

を忘れてしまったかもしれconfig.ymlでview_response_listenerを強制する必要がある(http://symfony.com/doc/master/bundles/FOSRestBundle/3-listener-support.htmlhttp://symfony.com/doc/master/bundles/FOSRestBundle/view_response_listener.html

#app/config/config.yml 
fos_rest: 
    view: 
     view_response_listener: 'force' 

それはうまくいくはずです!