How can I get my collection in API is empty

(Sorry for my english, I am from France)

I’m trying to create an API with Symfony but I have 2 problems:

  1. If I make a dd($user); I have an empty collection of products (many-to-many)
  2. In the API on Insomnia I don’t have any products at all (my productPerson is empty in DB so no problem)
class UserApiController extends AbstractController
{

    /**
     * @Route("/user/{id}", name="user_detail", requirements={"id" = "d+"}, methods="GET")
     */

    public function userView($id, UserRepository $userRepository)
    {
        $user = $userRepository->userWithAllData($id);
        
        // dd($user);
        if ($user == null) {
                    return $this->json([], $status = 404,[], $context = []);
        }

        $response = $this->json($user, 200, [], ['groups' =>'apiV0']);

        return $response;      
    }

My proprieties have the groups apiV0

class User {
    /**
     * @ORMId()
     * @ORMGeneratedValue()
     * @ORMColumn(type="integer")
     * @Groups("apiV0")
     */
    private $id;

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

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

    /**
     * @ORMColumn(type="string", length=100)
     * @Groups("apiV0")
     */
    private $email;

    /**
     * @ORMColumn(type="string", length=255)
     * @Groups("apiV0")
     */
    private $password;

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

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

    /**
     * @ORMManyToMany(targetEntity=Product::class, inversedBy="users")
     * @Groups("apiV0")
     */
    private $products;

    /**
     * @ORMOneToMany(targetEntity=ProductPerso::class, mappedBy="user")
     * @Groups("apiV0")
     */
    private $productsPerso;
Insomnia:
    {
        "id": 300,
        "name": "Zacharie",
        "city": "Albert",
        "email": "[email protected]",
        "password": "+`k#"4:-c{@gu",
        "pseudo": "Xavier",
        "created_at": "2020-07-01T17:58:24+02:00"
     }

The dump die: UserApiController.php on line 34:

    AppEntityUser {#847 ▼
      -id: 300
      -name: "Zacharie"
      -city: "Albert"
      -email: "[email protected]"
      -password: "+`k#"4:-c{@gu"
      -pseudo: "Xavier"
      -created_at: DateTime @1593619104 {#793 ▼
        date: 2020-07-01 17:58:24.0 Europe/Berlin (+02:00)
      }
      -products: DoctrineORMPersistentCollection {#873 ▼
        -snapshot: []
        -owner: AppEntityUser {#847}
        -association: array:20 [ …20]
        -em: DoctrineORMEntityManager {#609 …11}
        -backRefFieldName: "users"
        -typeClass: DoctrineORMMappingClassMetadata {#843 …}
        -isDirty: false
        #collection: DoctrineCommonCollectionsArrayCollection {#880 ▼
          -elements: []
        }
        #initialized: false
      }
      -productsPerso: DoctrineORMPersistentCollection {#882 ▼
        -snapshot: []
        -owner: AppEntityUser {#847}
        -association: array:15 [ …15]
        -em: DoctrineORMEntityManager {#609 …11}
        -backRefFieldName: "user"
        -typeClass: DoctrineORMMappingClassMetadata {#800 …}
        -isDirty: false
        #collection: DoctrineCommonCollectionsArrayCollection {#881 ▶}
        #initialized: true
      }
    }

Source: Symfony Questions

Was this helpful?

0 / 0

Leave a Reply 0

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