2016-12-14 11 views
1

マイcomposer.json(その一部):symfonyの3つのFOS休憩+ JMSシリアライザグループ

{ 
    "require": { 
     "symfony/symfony": "3.1.*", 
     "jms/serializer-bundle": "^1.1", 
     "friendsofsymfony/rest-bundle": "^2.1" 
    } 
} 

私は、リストアクション、検索アクションのための完全なため一部のデータを返すしたいのですが、いくつかのエンティティを持っています。そのために、私はこれらのファイルを持っている:

Product.php

<?php 

namespace AppBundle\Entity; 

use Doctrine\ORM\Mapping as ORM; 
use JMS\Serializer\Annotation as JMS; 

/** 
* @ORM\Entity 
* @ORM\Table(name="represented") 
* @JMS\ExclusionPolicy("ALL") 
*/ 
class Product 
{ 
    /** 
    * @var integer 
    * @ORM\Column(type="integer", nullable=false, options={"unsigned"=true}) 
    * @ORM\Id 
    * @ORM\GeneratedValue(strategy="AUTO") 
    */ 
    protected $id; 

    /** 
    * @var string 
    * @ORM\Column(type="string", nullable=false, length=48) 
    */ 
    protected $name; 

    /** 
    * @var Group 
    * @ORM\ManyToOne(targetEntity="Group", inversedBy="products") 
    * @ORM\JoinColumn(name="group_id", referencedColumnName="id") 
    */ 
    protected $group; 

    public function getId() 
    { 
     return $this->id; 
    } 

    public function setName($name) 
    { 
     $this->name = $name; 
    } 

    public function getName() 
    { 
     return $this->name; 
    } 

    public function setGroup(Group $group) 
    { 
     $this->group = $group; 
    } 

    public function getGroup() 
    { 
     return $this->group; 
    } 
} 

ProductController.php

<?php 

namespace AppBundle\Controller; 

use FOS\RestBundle\Controller\Annotations\Get; 
use FOS\RestBundle\Controller\FOSRestController; 
use AppBundle\Entity\Product; 

class ProductController extends FOSRestController 
{ 
    /** 
    * @Get("/product", name="list_products") 
    */ 
    public function listAction() 
    { 
     $products = $this->getDoctrine() 
      ->getRepository('AppBundle:Product') 
      ->findBy([], [ 'name' => 'ASC' ]); 

     $view = $this->view($products); 

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

    /** 
    * @Get("/product/{id}", requirements={"id" = "\d+"}, name="get_product") 
    */ 
    public function getAction($id) 
    { 
     $em = $this->getDoctrine()->getManager(); 

     $product = $em->getRepository('AppBundle:Product') 
      ->find($id); 

     if (! $product) { 
      $error = [ 
       'error' => 'Product not found' 
      ]; 

      $view = $this->view($error, 404); 
     } else { 
      $view = $this->view($product); 
     } 

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

私は、リストの結果にgroupプロパティを表示しないようにできるようにしたいと思います。このために私はグループを中心にいくつかのアプローチを試みました。

  1. ちょうど私がGroups({"List"})と私のリスト上の を示し、@View(serializerGroups={"List"})で コントローラ上でこのグループを参照したいプロパティのグループ名を設定します。しかし、すべてのプロパティが表示されているので、これには影響しませんでした( )。
  2. @ExclusionPolicy("all")をエンティティ全体に設定すると、 も機能しませんでした。
  3. ExclusionPolicyに加えて、私が をいくつかのグループまたはすべてのグループに表示したいと考えていたすべてのプロパティに対して@Exposeが表示されていましたが、 とマークされたすべてのプロパティが表示されました。

私はまたこれらのいくつかの変形を試みましたが、結果を変えるものは何もありませんでした。お使いのコントローラのアクションの

答えて

1

エンドだけではありません。

return $this->handleView($view); 

しかし、ワーキンググループまたはグループを取得するために、あなたはそれらを有効にする必要があります。クラスのトップでは、あなたは、FOSRestコンテキストを追加する必要はありませんJMSコンテキスト:

use FOS\RestBundle\Context\Context; 

やビューを返す前に、コントローラのアクションで:

$view = $this->view($products, 200); 
$context = new Context(); 
$context->setGroups(['list']); 
$view->setContext($context); 

return $this->handleView($view); 

これはsymfonyの3.2とFOSRest 2.1のために働くと2.0になると。 FOSRestのドキュメントをアップグレードするための

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

0

あなたが@Excludeを使用する場合、これは動作するはずです。

fos_rest: 
    serializer: 
     groups: ['Default'] 

これは、エンティティのプロパティをシリアル化するために、グループDefaultにあることが必要です。私が使用している別のオプションは、config.ymlに小さな追加を行うことです。プロパティに@Groupsアノテーションがない場合は、Defaultになりますが、@Groupsアノテーションを追加するとすぐに、Defaultグループには含まれなくなります(特に追加しない限り)。)

@Groupsアノテーションを持つエンティティフィールドを除くすべてのエンティティフィールドをデフォルトのシリアル化プロセスに含めることができました。次に、Defaultとすべてのエンティティフィールドを含めることができました。

// Function in the Controller class 
public function getAction(MyEntity $me) { 
    // Use automatic serializer, only things in 'Default' group are included 
    return $me; 
}