亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

Symfony:如何從與 oneToMany 關系相關的實體中獲取所有對象數據

Symfony:如何從與 oneToMany 關系相關的實體中獲取所有對象數據

PHP
翻過高山走不出你 2024-01-19 14:55:50
我有與 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 關系中使用,但您需要一個有效的理由才能這樣做。


查看完整回答
反對 回復 2024-01-19
  • 1 回答
  • 0 關注
  • 150 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號