我有实体:
<?php namespace AppBundle\Entity; use Doctrine\ORM\Mapping as ORM; use JMS\Serializer\Annotation\Groups; //... /** * @ORM\Entity(repositoryClass="AppBundle\Repository\UserRepository") **/ class User extends BaseUser { /** * @ORM\Id * @ORM\Column(type="integer") * @ORM\GeneratedValue(strategy="AUTO") * @Groups({"default"}) */ protected $id; /** * @ORM\ManyToMany(targetEntity="StorageBundle\Entity\File") * @ORM\JoinTable(name="users_photos", * joinColumns={@ORM\JoinColumn(name="user_id", referencedColumnName="id")}, * inverseJoinColumns={@ORM\JoinColumn(name="photo_id", referencedColumnName="id", onDelete="CASCADE")} * ) * @Groups({"default"}) */ protected $photoFiles; /** * @ORM\Column(type="string", length=255, nullable=true, unique=false) * @Groups({"notification"}) */ protected $deviceId; /** * @ORM\Column(type="string", length=255, nullable=true, unique=false) * @Groups({"default"}) */ protected $name; /** * @ORM\OneToOne(targetEntity="AppBundle\Entity\Car", mappedBy="driver") * @Groups({"driver"}) */ private $car; /** * @var \DateTime $created * * @Gedmo\Timestampable(on="create") * @ORM\Column(type="datetime") * @Groups({"default"}) */ protected $created; /** * @var \DateTime $updated * * @Gedmo\Timestampable(on="update") * @ORM\Column(type="datetime") * @Groups({"default"}) */ protected $updated; }我的控制器:
<?php namespace AppBundle\Controller; use AppBundle\Entity\User; //... class UsersController extends AppController{ /** * Get list of Admins * @Annotations\Get("/api/v1/admins", name="list_of_admins") * @return \FOS\RestBundle\View\View */ public function getAdminsAction(){ if(!$this->isGranted('ROLE_ADMIN')){ throw new AccessDeniedException('Only for Admin'); } $admins = $this->getDoctrine()->getRepository('AppBundle:User')->findByRole('ROLE_ADMIN'); return $this->view(['data' => $admins], Codes::HTTP_OK); } }角色为 ROLE_ADMIN 的用户没有汽车(汽车只有角色为 ROLE_DRIVER 的用户).当我尝试获取所有管理员时,我得到了这个:
A user with role ROLE_ADMIN does not have the car (car have only users with role ROLE_DRIVER). When I try to get all admins I get this:
{ "id": 4, "username": "admin@site", "email": "admin@site", "roles": [ "ROLE_ADMIN" ], "photoFiles": [], "name": "Andrii", "car": null }我尝试添加组.我需要添加我的控制器以仅从组默认和通知
I tried add Groups. What I need to add my controller to receive data only from group default and notification
推荐答案提醒一下,JMSSerializer 中的 Groups 用作排除策略.
As reminder, Groups in JMSSerializer are used as exclusion strategy.
排除策略包括根据条件/上下文公开/排除属性的特定方法.
An exclusion strategy consists to a specific approach used to expose/exclude properties depending on condition/context.
为了正确使用它们,您要做的第一件事就是排除实体的所有属性:
In order to use them correctly, the first thing you have to do is exclude all properties of your entity :
// ... use JMS\Serializer\Annotation as JMS; /** * @ORM\Entity(repositoryClass="AppBundle\Repository\UserRepository") * @JMS\ExclusionPolicy("all") */ class User extends BaseUser现在,您已经根据 Groups 显式公开属性:
Now, you have explicitly expose properties depending on Groups :
/** * @ORM\Column(type="string", length=255, nullable=true, unique=false) * @JMS\Groups({"default"}) // Idem for all properties you want render in group "default" */ protected $name; /** * @ORM\Column(type="string", length=255, nullable=true, unique=false) * @JMS\Groups({"notification"}) */ protected $deviceId;然后,您必须在控制器操作中定义使用的 Group :
Then, you have to define the used Group in your controller action :
/** * Get list of Admins * @Annotations\Get("/api/v1/admins", name="list_of_admins") * @Annotations\View(serializerGroups={"default", "notification"}) // Here set the Group used * @return \FOS\RestBundle\View\View */ public function getAdminsAction() { if(!$this->isGranted('ROLE_ADMIN')){ throw new AccessDeniedException('Only for Admin'); } $admins = $this->getDoctrine()->getRepository('AppBundle:User')->findByRole('ROLE_ADMIN'); return $this->view(['data' => $admins], Codes::HTTP_OK); }现在,$admins 只包含暴露给 default 和 notification 组的属性.
Now, $admins contains only the properties exposed to the group default and notification.
您也可以手动完成,而不是使用注释:
You can also do it manually instead of use an annotation :
$view = $this->view($admins, 200); $view->setSerializerGroups(array('default', 'notification')); return $this->handleView($view);有关详细信息,请参阅排除策略.
For more informations, see Exclusion strategies .
希望你能明白.
更多推荐
Symfony2,FOSRestBundle.如何将组与 JMSSerializerBundle 一起使用?
发布评论