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

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 SymfonyComponentSerializerAnnotationGroups;

My Entity User:

/**
 * @ORMTable(name="`user`")
 * @ORMEntity(repositoryClass=UserRepository::class)
 */
class User implements UserInterface, Serializable
{
    /**
     * @ORMId()
     * @ORMGeneratedValue()
     * @ORMColumn(type="integer")
     * @Groups("apiv0")
     */
    private $id;

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

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

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

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

    /**
     * @ORMColumn(type="float")
     * @Groups("apiv0")
     */
    private $target_min;

    /**
     * @ORMColumn(type="float")
     * @Groups("apiv0")
     */
    private $target_max;

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

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

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

    /**
     * @ORMColumn(type="datetime")
     * @Groups("apiv0")
     */
    private $created_at;

    /**
     * @ORMColumn(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

Source: Symfony Questions

Was this helpful?

0 / 0

Leave a Reply 0

Your email address will not be published. Required fields are marked *