api - 如何让我在 API 中的收藏为空
问题描述
(对不起我的英语,我来自法国)
我正在尝试使用 Symfony 创建一个 API,但我有两个问题:
- 如果我做一个 dd($user); 我有一个空的产品集合(多对多)
- 在 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
}
}
解决方案
推荐阅读
- javascript - React Native - 构建应用程序
- jquery - 我有一个循环,想用 ajax 将类添加到最后 4 个元素
- reactjs - Heroku 部署因 react 和下一个 js 而失败
- python-3.x - 如何使用具有特定条件的熊猫删除excel中列的重复值
- sql - 查找两个日期之间的第五个工作日sql
- azure - AZURE REST POSTMAN 问题
- docusignapi - 获取文档信封状态时出错 - 帐户 ID 混乱?
- pip - virtualenv 上的 pip install -r requirements.txt 不会在本地安装,而是在全局安装,即使在激活后也是如此
- laravel - 如何使用 WhereBetween(时间和日期分开的列)?
- python - 打开、编辑和保存 Excel