내 composer.json
(의 일부) :심포니 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 나는 목록 결과에 group
속성을 표시하지 수 있도록하고 싶습니다
<?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);
}
}
: 그래서, 나는이 파일이 있습니다. 이를 위해 나는 주로 그룹과 함께 몇 가지 접근 방식을 시도했다.
- 그냥 내가
Groups({"List"})
내 목록에 을 보여@View(serializerGroups={"List"})
으로 컨트롤러에서이 그룹을 참조 할 특성에 대해 그룹 이름을 구성하십시오. 그러나 모든 속성을 볼 수 있으므로이 경우에는 의 영향이 없습니다. - 전체 엔터티에 대해
@ExclusionPolicy("all")
을 구성해도 이 작동하지 않았습니다. - ExclusionPolicy 외에도 을 일부 또는 모든 그룹에 표시하려고했지만 모든 속성을 으로 표시하려면 모든 속성에 대해
@Expose
을 표시해야합니다.
나는 또한 이들 중 몇 가지 변종을 시도했지만 그 결과는 변하지 않았습니다. 컨트롤러 액션의