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

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

FormType 從實體關系中添加字段

FormType 從實體關系中添加字段

PHP
瀟湘沐 2023-04-28 13:57:09
在我的 Symfony 4 項目中,我有一個ParametersAdmin實體,它包含與Entreprise實體的OneToOne關系。    /**     * @ORM\OneToOne(targetEntity="App\Entity\Entreprise", mappedBy="parametresAdmin", cascade={"persist", "remove"})     */    private $entreprise;Entreprise實體有一個nom字段在我基于ParametresAdmin實體的表單中,我想包含entreprise.nom屬性。所以,我試過這個:->add('entreprise', EntityType::class, [                'label' => "Nom de l'entreprise",                "class" => Entreprise::class,                "choice_label" => "nom",                "required" => false,            ])我現在有了nom值,但它就像ChoiceType而不是TextType
查看完整描述

1 回答

?
白板的微信

TA貢獻1883條經驗 獲得超3個贊

編輯您正在編輯的實體的子實體的值相當容易。


您必須將表單的類型更改為 TextType,正如您可能已經假設的那樣,并提供有關數據在“父”實體上的位置的表單信息,這可以通過以下選項完成property_path


->add('entreprise_nom', TextType::class, [

? ? ? 'label' => "Nom de l'entreprise",

? ? ? 'property_path' => 'entreprise.nom', // <--- this

? ? ? 'required' => false,

])

entreprise.nom本質上告訴表單組件它應該首先訪問entreprise您的實體上的屬性,然后在該值上它應該訪問該nom屬性(您可以在屬性訪問)。如果提交了表單并且刷新了實體管理器,它還將更改(?。?code>nom上的屬性。entreprise



查看完整回答
反對 回復 2023-04-28
  • 1 回答
  • 0 關注
  • 114 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

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

幫助反饋 APP下載

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

公眾號

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