JMSSerializerBundle:" exclusion_policy:NONE"不起作用

时间:2017-02-11 19:29:41

标签: php json rest jmsserializerbundle symfony-2.8

目前我正在使用Symfony 2.8,FOSUserBundle和JMSSerializerBundle开发RESTful API应用程序。

我有一个控制器操作,显示可附加到广告的类别列表。类别由AppBundle:Category实体表示。

这是控制器代码:

class CategoryController extends FOSRestController
{
    /**
     * @DI\Inject("app.category_repository")
     * 
     * @var CategoryRepository
     */
    private $categoryRepository;

    public function getCategoriesAction()
    {   
        $countries = $this->categoryRepository->findAll();

        $context = new Context();
        $context->addGroups(["category-list"]);

        $view = $this->view($countries, 200);
        $view->setContext($context);

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

    // ...

}

这是类别实体代码:

/**
 * Category
 *
 * @ORM\Table(name="category")
 * @ORM\Entity(repositoryClass="AppBundle\Repository\CategoryRepository")
 */
class Category
{
    /**
     * @var int
     *
     * @ORM\Column(name="id", type="integer")
     * @ORM\Id
     * @ORM\GeneratedValue(strategy="AUTO")
     */
    private $id;

    /**
     * @var string
     *
     * @ORM\Column(name="name", type="string", length=255, unique=true)
     */
    private $name;


    /**
     * Get id
     *
     * @return int
     */
    public function getId()
    {
        return $this->id;
    }

    /**
     * Set name
     *
     * @param string $name
     *
     * @return Category
     */
    public function setName($name)
    {
        $this->name = $name;

        return $this;
    }

    /**
     * Get name
     *
     * @return string
     */
    public function getName()
    {
        return $this->name;
    }
}

JMSSerializerBundle 配置:

jms_serializer:
    property_naming:
        separator:  _
        lower_case: true

    metadata:
        auto_detection: true
        cache: file
        debug: "%kernel.debug%"
        file_cache:
            dir: "%kernel.cache_dir%/serializer"
        directories:
            AppBundle:
                namespace_prefix: AppBundle
                path: %kernel.root_dir%/config/serializer/AppBundle

这是类别实体配置:

AppBundle\Entity\Category:
    exclusion_policy: NONE

我打电话给API时希望得到的结果:

{"0":{"id":1,"name":"category1"},"1":{"id":2,"name":"category2"}}

我真正得到的是:

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

我做错了吗?

更新

如果我按照以下方式设置配置:

AppBundle\Entity\Category:
    exclusion_policy: ALL    
    properties:
        id:
            groups: ["category-list", "category-entity"]
        name:
            groups: ["category-list", "category-entity"]

然后我得到以下回复:

[{"id":1,"name":"category1"},{"id":2,"name":"category2"}]

更新2 如果我更改了控制器操作,即注释设置序列化上下文的所有行并将exclusion_policy切换为NONE API正常工作:

public function getCategoriesAction()
{   
    $categories = $this->categoryRepository->findAll();

    //$context = new Context();
    //$context->addGroups(["category-list"]);

    $view = $this->view($categories, 200);
    //$view->setContext($context);

    return $this->handleView($view);
}
AppBundle\Entity\Category:
    exclusion_policy: NONE

它给我带来了以下输出:

[{"id":1,"name":"category1"},{"id":2,"name":"category2"}]

0 个答案:

没有答案