dongqiongjiong4740 2015-11-24 07:51 采纳率: 0%
浏览 50
已采纳

从数据库填充表单

For some reason I can't manage symfony to populate two dropdown lists with selected values from database. I have two entities category and product

CategoryType.php

namespace AppBundle\Form;

use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolver;

class CategoryType extends AbstractType
{
    public function buildForm(FormBuilderInterface $builder, array $options)
    {
        $builder->add('name', 'entity', array(
            'class' => 'AppBundle\Entity\Category',
            'property' => 'name',
            'expanded' => false,
            'multiple' => false
        ));
    }


    public function configureOptions(OptionsResolver $options)
    {
        $options->setDefaults([
            'data_class' => 'AppBundle\Entity\Category',
        ]);
    }

    public function getName()
    {
        return 'app_category_type';
    }
}

ProductType.php

<?php

namespace AppBundle\Form;

use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolver;

class ProductType extends AbstractType
{
    public function buildForm(FormBuilderInterface $builder, array $options)
    {
        $builder->add('name', 'entity', array(
            'class' => 'AppBundle\Entity\Product',
            'property' => 'name',
            'label' => 'hohoohoh',
            'data' => '',
            'expanded' => false,
            'multiple' => false
        ));
    }

    public function configureOptions(OptionsResolver $options)
    {
        $options->setDefaults([
            'data_class' => 'AppBundle\Entity\Product',
        ]);
    }

    public function getName()
    {
        return 'app_product_type';
    }
}

CommonType.php

<?php

namespace AppBundle\Form;

use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilderInterface;
use AppBundle\Form\CategoryType;
use AppBundle\Form\ProductType;


class CommonType extends AbstractType
{
    public function buildForm(FormBuilderInterface $builder, array $options)
    {
        $data = $builder->getData();

        $builder->add('category', new CategoryType());
        $builder->add('product', new ProductType());
    }



    public function getName()
    {
        return 'app_common_type';
    }
}

DefaultController.php

<?php

namespace AppBundle\Controller;

use AppBundle\Form\CommonType;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Symfony\Component\HttpFoundation\Request;

class DefaultController extends Controller
{
    /**
     * @Route("/", name="homepage")
     */
    public function indexAction(Request $request)
    {

        $product = $this->getDoctrine()->getRepository('AppBundle:Product')->findOneBy(['id' => 2]);


        $form = $this->createForm(new CommonType(), ['product' => $product]);

        return $this->render('AppBundle:Default:index.html.twig', ['form' => $form->createView()]);

    }
}

Dump of $product variable

DefaultController.php on line 20:
Product {#555 ▼
  #id: 2
  #name: "second"
  #price: "12.00"
  #description: "testing"
  #category: Category {#572 ▼
    +__isInitialized__: false
    -id: 2
    -name: null
    #products: null
     …2
  }
}

What I get as result is dropdown list with the values from the database but with no selected one. What I'm doing wrong and how to fix the issue?

  • 写回答

1条回答 默认 最新

  • dongshi949737 2015-11-24 08:53
    关注

    I think there is a problem with both your productType and your categoryType.

    You have not to create an entity field.

    You should do instead (CommonType.php) :

    <?php
    
    namespace AppBundle\Form;
    
    use Symfony\Component\Form\AbstractType;
    use Symfony\Component\Form\FormBuilderInterface;
    use AppBundle\Form\CategoryType;
    use AppBundle\Form\ProductType;
    
    
    class CommonType extends AbstractType
    {
        public function buildForm(FormBuilderInterface $builder, array $options)
        {
            $data = $builder->getData();
    
            $builder->add('category', 'entity', array(
                'class' => 'AppBundle:Category',
                'property' => 'name',
                'label' => 'Category Name',
                'expanded' => false,
                'multiple' => false
                'placeholder' => 'Select',
                'required' => true
            ))
            ->add('product', 'entity', array(
                'class' => 'AppBundle:Product',
                'property' => 'name',
                'label' => 'ProductName',
                'expanded' => false,
                'multiple' => false
                'placeholder' => 'Select',
                'required' => true
            ));
        }
    
    
    
        public function getName()
        {
            return 'app_common_type';
        }
    }
    

    You also should to remove the data property ( 'data' => '', ). It is this one which make your select box empty instead of matchin category/product element.

    UPDATE :

    If you want to filter values in entity field you should use a queryBuilder.

    'query_builder' => function(EntityRepository $er) use ($idValue) {
                        return $er->createQueryBuilder('p')
                                  ->where('p.id = :idValue')
                                  ->setParameter('idValue', $idValue);
                       },
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 关于#网络安全#的问题:求ensp的网络安全,不要步骤要完成版文件
  • ¥15 可否在不同线程中调用封装数据库操作的类
  • ¥20 使用Photon PUN2解决游戏得分同步的问题
  • ¥15 微带串馈天线阵列每个阵元宽度计算
  • ¥15 keil的map文件中Image component sizes各项意思
  • ¥20 求个正点原子stm32f407开发版的贪吃蛇游戏
  • ¥15 划分vlan后,链路不通了?
  • ¥20 求各位懂行的人,注册表能不能看到usb使用得具体信息,干了什么,传输了什么数据
  • ¥15 Vue3 大型图片数据拖动排序
  • ¥15 Centos / PETGEM