第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號(hào)安全,請(qǐng)及時(shí)綁定郵箱和手機(jī)立即綁定
已解決430363個(gè)問(wèn)題,去搜搜看,總會(huì)有你想問(wèn)的

使用帶有文本輸入的 EntityType

使用帶有文本輸入的 EntityType

PHP
慕碼人8056858 2022-05-27 14:45:20
我創(chuàng)建了一個(gè)EntityType表格,您可以在其中添加您對(duì)活動(dòng)的參與字段和您的職位。不幸的是,字段的數(shù)量和名稱在頁(yè)面上動(dòng)態(tài)更新,因此在創(chuàng)建表單時(shí),我沒(méi)有所有可用的字段(或者更糟糕的是一些不再存在)為簡(jiǎn)單起見(jiàn),我想使用一個(gè)簡(jiǎn)單的 textType,我的 javascripts 可以在其中輸入一個(gè)Id數(shù)字并鏈接數(shù)據(jù)庫(kù)中的相應(yīng)元素。代碼很簡(jiǎn)單class FieldPositionType extends AbstractType{    public function buildForm(FormBuilderInterface $builder, array $options)    {        $builder            ->add('position', EntityType::class, [                'class' => Position::class,                'choice_label' => 'name'            ])            ->add('field', EntityType::class, [                'class' => Field::class,                'choice_label' => 'id'            ])        ;    }    public function configureOptions(OptionsResolver $resolver)    {        $resolver->setDefaults([            'data_class' => Participation::class,        ]);    }}但不是EntityType,我更喜歡TextType。我想我需要對(duì)我的設(shè)置器進(jìn)行一些修改,但我不知道如何將 Id 轉(zhuǎn)換為實(shí)體EntityType。
查看完整描述

2 回答

?
喵喵時(shí)光機(jī)

TA貢獻(xiàn)1846條經(jīng)驗(yàn) 獲得超7個(gè)贊

正如Cerad所建議的,dataTransformer是解決方案。


所以只是為了position,我創(chuàng)建了一個(gè)dataTransformer


class PositionToIdTransformer implements DataTransformerInterface

{

    private $em;


    public function __construct(EntityManagerInterface $em)

    {

        $this->em = $em;

    }


    public function transform($position)

    {

        if (null === $position) {

            return '';

        }


        return $position->getId();

    }


    public function reverseTransform($id)

    {

        if (!$id){

            return;

        } 


        $position = $this->em->getRepository(Position::class)->find($id);


        if (null === $position){

            throw new TransformationFailedException(sprintf("the position '%s' does not exist!", $id));

        }


        return $position;


    }

}

我在我的formBuilder:



class FieldPositionType extends AbstractType

{

    private $pt; //PositionToIdTransformer


    public function __construct(PositionToIdTransformer $pt)

    {

        $this->pt = $pt;

    }


    public function buildForm(FormBuilderInterface $builder, array $options)

    {

        $builder

            ->add('position', TextType::class)

        ;


        $builder->get('position')->addModelTransformer($this->pt);

    }

}

它就像一個(gè)魅力!


查看完整回答
反對(duì) 回復(fù) 2022-05-27
?
富國(guó)滬深

TA貢獻(xiàn)1790條經(jīng)驗(yàn) 獲得超9個(gè)贊

查詢生成器鏈接:https ://symfony.com/doc/current/reference/forms/types/entity.html#ref-form-entity-query-builder


如評(píng)論中所述,嘗試使用它。我無(wú)法進(jìn)一步阻止您,因?yàn)槲铱床坏侥钠溆噙壿嫞珵榱讼拗茖?shí)體字段中顯示的條目,它看起來(lái)像這樣:


$builder->add('users', EntityType::class, [

    'class' => User::class,

    'query_builder' => function (EntityRepository $er) {

        return $er->createQueryBuilder('u')

            ->orderBy('u.username', 'ASC');

    },

    'choice_label' => 'username',

]);


查看完整回答
反對(duì) 回復(fù) 2022-05-27
  • 2 回答
  • 0 關(guān)注
  • 149 瀏覽

添加回答

舉報(bào)

0/150
提交
取消
微信客服

購(gòu)課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動(dòng)學(xué)習(xí)伙伴

公眾號(hào)

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號(hào)