マイ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
プロパティを表示しないようにできるようにしたいと思います。このために私はグループを中心にいくつかのアプローチを試みました。
- ちょうど私が
Groups({"List"})
と私のリスト上の を示し、@View(serializerGroups={"List"})
で コントローラ上でこのグループを参照したいプロパティのグループ名を設定します。しかし、すべてのプロパティが表示されているので、これには影響しませんでした( )。 @ExclusionPolicy("all")
をエンティティ全体に設定すると、 も機能しませんでした。- ExclusionPolicyに加えて、私が をいくつかのグループまたはすべてのグループに表示したいと考えていたすべてのプロパティに対して
@Expose
が表示されていましたが、 とマークされたすべてのプロパティが表示されました。
私はまたこれらのいくつかの変形を試みましたが、結果を変えるものは何もありませんでした。お使いのコントローラのアクションの