如何在表单

时间:2017-02-08 07:10:12

标签: php symfony

我不知道如何将数据从数据库加载到选择框

我的实体类型:

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';
    }
}

我的控制器:

        $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()) );

我收到错误 类型"字符串"," Delivery \ AdminBundle \ Forms \ KitchenType"的预期参数;给定

我想在我的html cod中加载Kitchen title到selectbox。 (抱歉我的英文不好

1 个答案:

答案 0 :(得分:0)

<强> 1。将选项传递给您的表单

您的表单类型:

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');
    }
}

您的控制器操作:

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

    ....
}

<强> 2。将表单定义为服务

您的表单类型:

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;
    }
}

注册为服务:

<强> 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>

来源:How to Access Services or Config from Inside a Form