Ho entità:Symfony2, FOSRestBundle. Come usare il gruppo con JMSSerializerBundle?
<?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;
}
mio controller:
<?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);
}
}
Un utente con ruolo ROLE_ADMIN non hai l'auto (auto hanno solo gli utenti con ruolo ROLE_DRIVER). Quando cerco di ottenere tutti gli amministratori ottengo questo:
{
"id": 4,
"username": "[email protected]",
"email": "[email protected]",
"roles": [
"ROLE_ADMIN"
],
"photoFiles": [],
"name": "Andrii",
"car": null
}
ho cercato aggiungere gruppi. Che cosa ho bisogno di aggiungere il mio controller per ricevere i dati solo dal gruppo predefinito e notifica
Salve, quale importazione (utilizzo) è necessario utilizzare l'annotazione '* @Annotations \ View (serializerGroups = {" default "," notification "})' nel controller? Grazie! –
Risposta personale: utilizzare FOS \ RestBundle \ Controller \ Annotations –
La funzione 'setSerializerGroups' è stata rimossa. È necessario utilizzare [contesto di serializzazione direttamente] (https://github.com/FriendsOfSymfony/FOSRestBundle/issues/521#issuecomment-207071563). – fracz