我有與 oneToMany 關(guān)系相關(guān)的“汽車(chē)”和“預(yù)訂”實(shí)體:<?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() ]); }當(dāng)在twig或列出所有預(yù)訂的索引頁(yè)面中轉(zhuǎn)儲(chǔ) $booking 變量時(shí),預(yù)訂對(duì)象中的每個(gè)汽車(chē)對(duì)象只有 id 字段,其他字段均為 null !我希望獲得與預(yù)訂相關(guān)的汽車(chē)對(duì)象的每個(gè)字段的完整數(shù)據(jù),以在樹(shù)枝視圖中顯示它!在方法書(shū)中,我獲取了隨請(qǐng)求發(fā)送的 id 變量,并將其放入 setCar 方法中,我是否應(yīng)該創(chuàng)建一個(gè)查詢來(lái)搜索 byId 并一一影響每個(gè)屬性!
Symfony:如何從與 oneToMany 關(guān)系相關(guān)的實(shí)體中獲取所有對(duì)象數(shù)據(jù)
翻過(guò)高山走不出你
2024-01-19 14:55:50