保存表格后出错[Symfony3]

时间:2017-02-10 12:10:19

标签: php doctrine symfony

MyEntityType:

public function buildForm(FormBuilderInterface $builder, array $options) {
        $em = $this->em;
        $em = $options['entity_manager'];
        $builder
            ->add('title')
            ->add('photo', FileType::class)
            ->add('kitchen', EntityType::class, array(
                'class' => 'DeliveryAdminBundle:Kitchen',
                'multiple' => true,
                'query_builder' => function (\Delivery\AdminBundle\Repository\KitchenRepository $er) {
                    return $er->createQueryBuilder('k')
                        ->orderBy('k.title', 'ASC');

                },
                'data' => ['16'=>'Японская'],        
            ))
            ->add('specialization', EntityType::class, array(
                'class' => 'DeliveryAdminBundle:Specialization',
                'multiple' => true,
                'query_builder' => function (\Delivery\AdminBundle\Repository\SpecializationRepository $er) {
                    return $er->createQueryBuilder('k')
                        ->orderBy('k.title', 'ASC');

                },
            ))
            ->add('type', EntityType::class, array(
                'class' => 'DeliveryAdminBundle:EntityType',
                'multiple' => false,
                'query_builder' => function (\Delivery\AdminBundle\Repository\EntityTypeRepository $er) {
                    return $er->createQueryBuilder('k')
                        ->orderBy('k.title', 'ASC');

                },
            ))
            ->add('save', SubmitType::class, array('label'=>'Отправить'));
    }

控制器操作:

    public function newAction(Request $request) {
        $em = $this->getDoctrine()->getManager();
        $repo = $em->getRepository('DeliveryAdminBundle:MyEntity');

        $entity = new MyEntity();
        $form = $this->createForm(MyEntityType::class, $entity, [
            'entity_manager' => $this->get('doctrine.orm.entity_manager')
        ]);

        $form->handleRequest($request); // ERROR !!!

        if ($form->isSubmitted() && $form->isValid()) {
            $repo->update($entity, $request->request->get('id'));

            return $this->redirectToRoute('show_myentity');
        }

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

项目已发布https://github.com/zchipirov/delivery

设置表Kitchen-> MyEntity(OneToMany)和Specialization-> MyEntity(OneToMany)之间的通信。表“MyEntityKitchen”和“MyEntitySpecialization”联合MyEntity,Kitchen和Specialization

保存后我有错误:

Expected argument of type "Delivery\AdminBundle\Entity\MyEntitySpecialization", "Delivery\AdminBundle\Entity\Kitchen" given

堆栈:

if (null === $response) {
$msg .=' Did you forget to add a return statement somewhere in your controller?';
}
throw new \LogicException($msg);
}
}
return $this->filterResponse($response, $request, $type);




at HttpKernel ->handleRaw (object(Request), 1)
in var\cache\dev\classes.php at line 4855  +
at HttpKernel ->handle (object(Request), 1, true)
in vendor\symfony\symfony\src\Symfony\Component\HttpKernel\Kernel.php at line 168  +
at Kernel ->handle (object(Request))
in web\app_dev.php at line 28  +

2 个答案:

答案 0 :(得分:0)

这是因为你有MyEntity课程:

/**
 * Add kitchen
 *
 * @param \Delivery\AdminBundle\Entity\MyEntitySpecialization $kitchen
 *
 * @return MyEntity
 */
public function addKitchen(\Delivery\AdminBundle\Entity\MyEntitySpecialization $kitchen)
{
    $this->kitchen[] = $kitchen;
    return $this;
}

并以表格形式提供:

->add('kitchen', EntityType::class, array(
    'class' => 'DeliveryAdminBundle:Kitchen',
    'multiple' => true,
    'query_builder' => function (\Delivery\AdminBundle\Repository\KitchenRepository $er) {
        return $er->createQueryBuilder('k')
            ->orderBy('k.title', 'ASC');
        },
    'data' => ['16'=>'Японская'],        
))

并且提交类\Delivery\AdminBundle\Entity\Kitchen的表单实体时会传递给MyEntity->addKitchen。没关系,但由于某种原因,你有类型:\Delivery\AdminBundle\Entity\MyEntitySpecialization,这就是PHP投掷Exception的原因。

this line更改为:

public function addKitchen(\Delivery\AdminBundle\Entity\Kitchen $kitchen)

this line

public function removeKitchen(\Delivery\AdminBundle\Entity\Kitchen $kitchen)

答案 1 :(得分:0)

我添加了代码:

/**
     * Add kitchen
     *
     * @param \Delivery\AdminBundle\Entity\MyEntitySpecialization $kitchen
     *
     * @return MyEntity
     */
    public function addKitchen(\Delivery\AdminBundle\Entity\MyEntitySpecialization $kitchen, \Delivery\AdminBundle\Entity\MyEntitySpecialization $specialization)
    {
        $this->kitchen[] = $kitchen;
        $this->specialization[] = $specialization;

        return $this;
    }

    /**
     * Remove kitchen
     *
     * @param \Delivery\AdminBundle\Entity\MyEntitySpecialization $kitchen
     */
    public function removeKitchen(\Delivery\AdminBundle\Entity\MyEntitySpecialization $kitchen, \Delivery\AdminBundle\Entity\MyEntitySpecialization $specialization)
    {
        $this->kitchen->removeElement($kitchen);
        $this->specialization->removeElement($specialization);
    }

谢谢,它有效!

如何将Form中的数据保存到多个表中(从MyEntityType到MyEntity,MyEntityKitchen和MyEntitySpecialization)? 保存后我收到错误:

Expected value of type "Doctrine\Common\Collections\Collection|array" for association field "Delivery\AdminBundle\Entity\MyEntity#$kitchen", got "Delivery\AdminBundle\Entity\Kitchen" instead.