Can't send entire user to API json response, only send id and email

Viewed 63

I'm tring to send back to front end my user entity, but only id and email are sent, not others proprities. The other proprities are using @Groups like id and email. Like you will see, I can't send in response id and email. So I don't understand where is the problem. I have the use Symfony\Component\Serializer\Annotation\Groups;

My Entity User:

/**
 * @ORM\Table(name="`user`")
 * @ORM\Entity(repositoryClass=UserRepository::class)
 */
class User implements UserInterface, \Serializable
{
    /**
     * @ORM\Id()
     * @ORM\GeneratedValue()
     * @ORM\Column(type="integer")
     * @Groups("apiv0")
     */
    private $id;

    
    /**
     * @ORM\Column(type="json")
     * @Groups("apiv0")
     */
    private $roles = [];
    
    /**
     * @ORM\Column(type="string", length=180, unique=true)
     * @Groups("apiv0")
     */
    private $email;
    /**
     * @var string The hashed password
     * @ORM\Column(type="string")
     * @Groups("apiv0")
     */
    private $password;

    /**
     * @ORM\OneToMany(targetEntity=Bloodsugar::class, mappedBy="user")
     * @Groups("apiv0")
     */
    private $bloodsugars;

    /**
     * @ORM\Column(type="string", length=50)
     * @Groups("apiv0")
     */
    private $lastname;

    /**
     * @ORM\Column(type="string", length=50)
     * @Groups("apiv0")
     */
    private $firstname;

    /**
     * @ORM\Column(type="float")
     * @Groups("apiv0")
     */
    private $target_min;

    /**
     * @ORM\Column(type="float")
     * @Groups("apiv0")
     */
    private $target_max;

    /**
     * @ORM\Column(type="string", length=50, nullable=true)
     * @Groups("apiv0")
     */
    private $doctor_name;

    /**
     * @ORM\Column(type="string", length=20)
     * @Groups("apiv0")
     */
    private $diabetes_type;

    /**
     * @ORM\Column(type="string", length=50, nullable=true)
     * @Groups("apiv0")
     */
    private $doctor_email;

    /**
     * @ORM\Column(type="datetime")
     * @Groups("apiv0")
     */
    private $created_at;

    /**
     * @ORM\Column(type="string", length=255)
     * @Groups("apiv0")
     */
    private $username;

My Controller:

$userCreated =  $repository->find($user->getId());
                    
                    dump($user);
                    dump($userCreated);
                
                    $response = $this->json($userCreated, 200, [], ['groups' => 'apiv0']);

                    dd($response);

                    return $response

My dumps:

enter image description here

1 Answers

You should use graph bracket in the groups definition, so do like this:

/**
 * @ORM\Column(type="string", length=180, unique=true)
 * @Groups({"apiv0"})
 */
private $email;

instead of this:

/**
 * @ORM\Column(type="string", length=180, unique=true)
 * @Groups("apiv0")
 */
private $email;
Related