douxun1407 2016-12-14 01:37
浏览 42

Symfony 3 FOS Rest + JMS Serializer组

My composer.json (part of it):

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

I have some entities which I'd like to return partial data for the list action and complete for the find action. For so, I have these files:

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);
    }
}

I would like to be able to not show the group property on the list result. For this I have tried a few of approaches, mainly with groups.

  1. Just use configure the group name for the properties I want to show on my list with Groups({"List"}) and refer this group on the controller with @View(serializerGroups={"List"}). But this had no affect as all properties are visible.
  2. Configure @ExclusionPolicy("all") for the entire entity didn't work as well.
  3. In addition to ExclusionPolicy, @Expose to all properties I wanted to show in some or all groups, but that made all properties marked to be shown.

I also tried some more variants of these, but nothing that change the results.

  • 写回答

2条回答 默认 最新

  • douguang9014 2017-03-28 22:11
    关注

    End of your controller action should not be only:

    return $this->handleView($view);
    

    But for getting a group or groups working you need to activate them. On top of the class you need to add FOSRest Context, not JMS context:

    use FOS\RestBundle\Context\Context;
    

    and in the controller actions before returning view:

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

    This will work for Symfony 3.2 and FOSRest 2.1 and 2.0.

    Link for upgrading documentation for FOSRest: https://github.com/FriendsOfSymfony/FOSRestBundle/blob/master/UPGRADING-2.0.md

    评论

报告相同问题?

悬赏问题

  • ¥15 HFSS 中的 H 场图与 MATLAB 中绘制的 B1 场 部分对应不上
  • ¥15 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?