首页 > 解决方案 > 如何让我在 API 中的收藏为空

问题描述

(对不起我的英语,我来自法国)

我正在尝试使用 Symfony 创建一个 API,但我有两个问题:

  1. 如果我做一个 dd($user); 我有一个空的产品集合(多对多)
  2. 在 Insomnia 的 API 中,我根本没有任何产品(我的 productPerson 在 DB 中是空的,所以没问题)
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;      
    }

我的礼仪有组apiV0

class User {
    /**
     * @ORM\Id()
     * @ORM\GeneratedValue()
     * @ORM\Column(type="integer")
     * @Groups("apiV0")
     */
    private $id;

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

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

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

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

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

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

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

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

转储死了:第 34 行的 UserApiController.php:

    App\Entity\User {#847 ▼
      -id: 300
      -name: "Zacharie"
      -city: "Albert"
      -email: "lacombe.honore@wanadoo.fr"
      -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: Doctrine\ORM\PersistentCollection {#873 ▼
        -snapshot: []
        -owner: App\Entity\User {#847}
        -association: array:20 [ …20]
        -em: Doctrine\ORM\EntityManager {#609 …11}
        -backRefFieldName: "users"
        -typeClass: Doctrine\ORM\Mapping\ClassMetadata {#843 …}
        -isDirty: false
        #collection: Doctrine\Common\Collections\ArrayCollection {#880 ▼
          -elements: []
        }
        #initialized: false
      }
      -productsPerso: Doctrine\ORM\PersistentCollection {#882 ▼
        -snapshot: []
        -owner: App\Entity\User {#847}
        -association: array:15 [ …15]
        -em: Doctrine\ORM\EntityManager {#609 …11}
        -backRefFieldName: "user"
        -typeClass: Doctrine\ORM\Mapping\ClassMetadata {#800 …}
        -isDirty: false
        #collection: Doctrine\Common\Collections\ArrayCollection {#881 ▶}
        #initialized: true
      }
    }

标签: apisymfony

解决方案


推荐阅读