簡體   English   中英

沒有水化的Symfony表單EntityType

[英]Symfony Form EntityType without Hydration

我正在建立一個帶有很多嵌套對象的網站。 但隨着數據庫的增長,慢慢地,但肯定地,Doctrine關聯開始真正顯示出來。 我遇到的最重要的問題之一是,我需要創建下拉菜單,以便用戶可以關聯其中一些實體。 下面的代碼是我用來生成表單的FormType之一的一部分。

   $builder
        ->add('sidebarcontent')
        ->add('publicAgenda')
        ->add('assets')
        ->add('structure')
        ->add('history')
        ->add('emblem')
        ->add('demonym')
        ->add('type', EntityType::Class, array(
            'class' => 'ContentBundle\Entity\OrganizationType',
            'choice_label' => 'name',
            'empty_data'   => '',
            'required'      => false,
            'query_builder' =>function (EntityRepository $er) use ( $world ) {
                return $er->createQueryBuilder('c')
                    ->orderBy('c.name', 'ASC');
            }))
        ->add('geographicLocation', EntityType::Class, array(
            'class' => 'ContentBundle\Entity\Location',
            'choice_label' => 'title',
            'empty_data'   => '',
            'required'      => false,
            'query_builder' =>function (EntityRepository $er) use ( $world ) {
                return $er->createQueryBuilder('c')
                    ->where('c.world = ?1')
                    ->setParameter(1, $world)
                    ->andWhere('c.state != ?2')
                    ->setParameter(2, 'archived')
                    ->orderBy('c.title', 'ASC');
            }
        ))
        ->add('parent', EntityType::Class, array(
            'class' => 'ContentBundle\Entity\Organization',
            'choice_label' => 'title',
            'empty_data'   => '',
            'required'      => false,
            'query_builder' =>function (EntityRepository $er) use ( $world ) {
                return $er->createQueryBuilder('c')
                    ->where('c.world = ?1')
                    ->setParameter(1, $world)
                    ->andWhere('c.state != ?2')
                    ->setParameter(2, 'archived')
                    ->orderBy('c.title', 'ASC');
            }
        ))
        ->add('ethnicities', EntityType::Class, array(
            'class' => 'ContentBundle\Entity\Ethnicity',
            'choice_label' => 'title',
            'empty_data'   => '',
            'multiple'     => true,
            'expanded'     => true,
            'required'      => false,
            'query_builder' =>function (EntityRepository $er) use ( $world ) {
                return $er->createQueryBuilder('c')
                    ->where('c.world = ?1')
                    ->setParameter(1, $world)
                    ->andWhere('c.state != ?2')
                    ->setParameter(2, 'archived')
                    ->orderBy('c.title', 'ASC');
            }
        ));

有沒有辦法將這些實體提取到最低限度(uuid,標題),而不是水合它們? 我什至不確定這是否是正確的問題。 我只是想減少現在的加載時間。

EntityType表單字段是必需的實體對象,您不能僅選擇必需的字段,因為否則,它將不知道在實體關系中要保留的內容。

如果確實只需要提取某些字段,則必須使用ChoiceType字段。 但是請記住,在持久化實體時,將需要具有相關實體的對象。

作為另一種選擇,您也可以嘗試將choices參數與找到的查詢結果一起使用,這樣您就可以緩存查詢和/或查詢結果。

例如:用表格中的choices參數替換query_builder參數。

$choices = $this->createQueryBuilder('c')
    ->where('c.world = ?1')
    ->setParameter(1, $world)
    ->andWhere('c.state != ?2')
    ->setParameter(2, 'archived')
    ->orderBy('c.title', 'ASC'); 
    ->getQuery()
    ->useQueryCache(true) 
    ->useResultCache(true, 3600) // this will cache most common results for a while.
    ->execute();

然后該領域如下。

->add('geographicLocation', EntityType::Class, array(
            'class' => 'ContentBundle\Entity\Location',
            'choice_label' => 'title',
            'empty_data'   => '',
            'required'      => false,
            'choices' => $choices
))

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM