我有与 oneToMany 关系相关的“汽车”和“预订”实体:
<?php
namespace 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 并一一影响每个属性!
眼眸繁星