duanfaxin7014 2017-02-08 07:10
浏览 41
已采纳

如何在表单中加载实体类型到选择框

I don't know, How I can load data from DB to selectbox

My Entity Type:

class MyEntityType extends AbstractType
{
    public function __construct($em) {
        $this->em = $em;
    }

    public function buildForm(FormBuilderInterface $builder, array $options) {
        $kitchen = new \Delivery\AdminBundle\Entity\Kitchen();
        $builder
            ->add('title', 'entity', array(
                'class' => 'DeliveryAdminBundle:Kitchen',
                'property' => 'title',
                'query_builder' => function(EntityRepository $er) {
                    return $er->createQueryBuilder('e')
                        ->orderBy('e.title', 'ASC');
                },
                'data' => $this->em->getReference("DeliveryAdminBundle:Kitchen", 3)
               ))
            ->add('save', SubmitType::class, array('label'=>'Отправить'));
    }

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

My Controller:

        $em = $this->getDoctrine()->getManager();
        $entity = new MyEntity();
        // $form = $this->createForm(MyEntityType::class, $entity);    
        $form = $this->createForm(new KitchenType($this->getDoctrine()->getManager()), $entity);
        $form->handleRequest($request);

        if ($form->isSubmitted() && $form->isValid()) {
            $em->persist($entity);
            $em->flush();

            return $this->redirectToRoute('show_myentity');
        }
        return $this->render( 'DeliveryAdminBundle:Entity:new.html.twig', 
                array('form'=>$form->createView()) );

I get error: Expected argument of type "string", "Delivery\AdminBundle\Forms\KitchenType" given

I want load Kitchen title to selectbox in my html cod. (Sorry for my bad English)

  • 写回答

1条回答 默认 最新

  • douxi8759 2017-02-08 08:57
    关注

    1. Pass Options to your Form

    Your form type class:

    use S‌​ymfony\Component\Opt‌​ionsResolver\Options‌​Resolver;
    
    class MyEntityType extends AbstractType
    {
        public function buildForm(FormBuilderInterface $builder, array $options)
        {
            $em = $options['entity_manager'];
        }
    
        public function configureOptions(OptionsResolver $resolver)
        {
            $resolver->setRequired('entity_manager');
        }
    }
    

    Your controller action:

    public function yourAction()
    {
        $entity = ...;
        $form = $this->createForm(MyEntityType::class, $entity, [
            'entity_manager' => $this->get('doctrine.orm.entity_manager')
        ]);
    
        ....
    }
    

    2. Define your Form as Service

    Your form type class:

    use Doctrine\ORM\EntityManager;
    
    class MyEntityType extends AbstractType
    {
        protected $em;
    
        public function __construct(EntityManager $em)
        {
            $this->em = $em;
        }
    
        public function buildForm(FormBuilderInterface $builder, array $options)
        {
            $em = $this->em;
        }
    }
    

    Register as service:

    YAML

    services:
    
        ....
    
        app.form.type.task:
            class: Namespace\Of\MyEntityType
            arguments: ['@doctrine.orm.entity_manager']
            tags:
                - { name: form.type }
    

    XML

    <?xml version="1.0" encoding="UTF-8" ?>
    <container xmlns="http://symfony.com/schema/dic/services"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://symfony.com/schema/dic/services http://symfony.com/schema/dic/services/services-1.0.xsd">
        <services>
            <service id="your.service.name" class="Namespace\Of\MyEntityType">
                <argument type="service" id="doctrine.orm.entity_manager"/>
                <tag name="form.type" />
            </service>
        </services>
    </container>
    

    Source: How to Access Services or Config from Inside a Form

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥20 sub地址DHCP问题
  • ¥15 delta降尺度计算的一些细节,有偿
  • ¥15 Arduino红外遥控代码有问题
  • ¥15 数值计算离散正交多项式
  • ¥30 数值计算均差系数编程
  • ¥15 redis-full-check比较 两个集群的数据出错
  • ¥15 Matlab编程问题
  • ¥15 训练的多模态特征融合模型准确度很低怎么办
  • ¥15 kylin启动报错log4j类冲突
  • ¥15 超声波模块测距控制点灯,灯的闪烁很不稳定,经过调试发现测的距离偏大