我有與 oneToMany 關系相關的“汽車”和“預訂”實體:<?phpnamespace App\Entity; /** * @ORM\Entity(repositoryClass=CarRepository::class) * @UniqueEntity("registrationNumber") * @Vich\Uploadable */class Car{ /** * @ORM\Id() * @ORM\GeneratedValue() * @ORM\Column(type="integer") */ private $id; /** * @ORM\Column(type="string", length=255) * @Assert\Length(min=3, max=9) */ private $registrationNumber; /** * @ORM\OneToMany(targetEntity=Booking::class, mappedBy="car") */ private $bookings;/** * @return Collection|Booking[] */ public function getBookings(): Collection { return $this->bookings; }}public function book(Request $request, Car $car) { $booked_car = $car; $booking = new Booking(); $booking->setCar($booked_car); $form = $this->createForm(BookingType::class, $booking); $form->handleRequest($request); if ($form->isSubmitted() && $form->isValid()){ $booking = $form->getData(); $this->em->persist($booking); $this->em->flush(); $this->addFlash('success', 'Démande envoyée avec success'); return $this->redirectToRoute('car.index'); } $this->addFlash('error', 'une erreur dans le système'); return $this->render('booking/book.html.twig',[ 'booking' => $booking, 'form' => $form->createView() ]); }當在twig或列出所有預訂的索引頁面中轉儲 $booking 變量時,預訂對象中的每個汽車對象只有 id 字段,其他字段均為 null !我希望獲得與預訂相關的汽車對象的每個字段的完整數據,以在樹枝視圖中顯示它!在方法書中,我獲取了隨請求發送的 id 變量,并將其放入 setCar 方法中,我是否應該創建一個查詢來搜索 byId 并一一影響每個屬性!
1 回答

眼眸繁星
TA貢獻1873條經驗 獲得超9個贊
這種關系默認是延遲加載的。因此,由于您沒有調用汽車對象的任何 getter,因此您只顯示了 id(因為它也存儲在預訂對象中)。但這僅在轉儲的上下文中,一旦您嘗試使用汽車的 getter(getId 除外),它就會加載其余部分。
如果您確實需要將整個汽車數據與預訂數據一起加載,則可以fetch="EAGER"
在 ManyToOne 關系中使用,但您需要一個有效的理由才能這樣做。
- 1 回答
- 0 關注
- 150 瀏覽
添加回答
舉報
0/150
提交
取消