Php Symfony如何序列化实体FOSRestController
我对api和ExclutionPolicy使用FOSRestController,对entity使用Expose,对于某些逻辑,我有两个操作,但当我返回entity$位时,我希望看到不同的字段,例如一个操作只是id和created,而第二个操作只是字段projectId。因为现在我返回带有注释@Expose的字段,但这是所有操作的结果。我有一个问题,如何为另一个操作配置类BitController extends FOSRestController的实体字段?我在控制器中使用@Groups({“list”})和Php Symfony如何序列化实体FOSRestController,php,symfony,jmsserializerbundle,Php,Symfony,Jmsserializerbundle,我对api和ExclutionPolicy使用FOSRestController,对entity使用Expose,对于某些逻辑,我有两个操作,但当我返回entity$位时,我希望看到不同的字段,例如一个操作只是id和created,而第二个操作只是字段projectId。因为现在我返回带有注释@Expose的字段,但这是所有操作的结果。我有一个问题,如何为另一个操作配置类BitController extends FOSRestController的实体字段?我在控制器中使用@Groups({“
$bits = $this->get('serializer')->serialize(new Bit(), 'json', SerializationContext::create()->setGroups(array('list')));
但是有$bits={}也许这是因为$bits数组
/**
* Bit
*
* @ORM\Table(name="bit")
* @ORM\Entity(repositoryClass="Artel\ProfileBundle\Entity\Repository\BitRepository")
* @ExclusionPolicy("all")
*/
class Bit
{
/**
* @var integer
*
* @ORM\Column(name="id", type="integer")
* @ORM\Id
* @ORM\GeneratedValue(strategy="AUTO")
* @Expose()
* @Groups({"list"})
*/
private $id;
/**
* @var datetime $created
*
* @Gedmo\Timestampable(on="create")
* @ORM\Column(type="datetime")
* @Expose()
*/
private $created;
/**
* @ORM\ManyToOne(targetEntity="Project", inversedBy="bit")
* @ORM\JoinColumn(name="project_id", referencedColumnName="id", onDelete="CASCADE")
*
* @Groups({"list"})
*/
private $projectId;
class BitController extends FOSRestController
{
public function getBitByProjectAction($id, $token)
{
$security = $this->get('security.context');
$user = $this->getDoctrine()->getRepository('ArtelProfileBundle:Users')->findOneBySecuritytoken($token);
if (!empty($user) || $security->isGranted('ROLE_ADMIN') ) {
$bits = $this->getDoctrine()->getManager()
->getRepository('ArtelProfileBundle:Bit')
->findBitByProject($id, $token);
if (!$bits) {
throw new NotFoundHttpException();
}
$view = $this->view($bits, 200)
->setSerializationContext(SerializationContext::create()->setGroups(array('list')))
;
return $this->handleView($view);
public function getBitsProjectsAction($token)
{
$user = $this->getDoctrine()->getRepository('ArtelProfileBundle:Users')->findOneBySecuritytoken($token);
$view = View::create();
if (!empty($user) && !empty($token)) {
$bits = $this->getDoctrine()->getRepository('ArtelProfileBundle:Bit')->findClientsBitsByProjects($token);
$view->setStatusCode(200);
$view = $this->view($bits, 200)
->setSerializationContext(SerializationContext::create()->setGroups(array('list')))
;
return $this->handleView($view);
}else{
$view->setStatusCode(101);
}
return $view;
}
已解决
$view = $this->view($bits, 200)
->setSerializationContext(SerializationContext::create()->setGroups(array('list')))
;
return $this->handleView($view);
您是否检查过是否创建了对象的不同视图?我不知道,我需要创建VersionedObject,以及VersionedObject如何知道实体位bot-undership或我需要使用注释@Groups?返回$bits这是arrayI更新我的问题