传递给选择字段的实体必须进行管理 - 在app.php和app_dev.php上不同

问题描述 投票:1回答:2

欢迎!

我在表单中添加了一个字段

    ->add('languageLevel', ChoiceType::class, [
        'choices' => [
            Meme::LEVEL_BEGINNER => Meme::LEVEL_BEGINNER,
            Meme::LEVEL_INTERMEDIATE => Meme::LEVEL_INTERMEDIATE,
            Meme::LEVEL_ADVANCED => Meme::LEVEL_ADVANCED,
            Meme::LEVEL_EXPERT => Meme::LEVEL_EXPERT
        ]
    ])

此表单使用相同的属性保存实体“Meme”

/**
 * @var string
 * @ORM\Column(name="language_level", type="string", length=20)
 */
private $languageLevel;

prod.log给了我这个:

[2017-12-17 23:41:18] request.INFO: Matched route "meme_add". {"route":"meme_add","route_parameters":{"_controller":"AppBundle\\Controller\\MemeController::addAction","_route":"meme_add"},"request_uri":"http://keenweasel.com/meme/add","method":"GET"} []
[2017-12-17 23:41:18] security.DEBUG: Read existing security token from the session. {"key":"_security_main"} []
[2017-12-17 23:41:18] security.DEBUG: User was reloaded from a user provider. {"username":"acid","provider":"FOS\\UserBundle\\Security\\UserProvider"} []
[2017-12-17 23:41:18] php.WARNING: Warning: spl_object_hash() expects parameter 1 to be object, integer given {"exception":"[object] (ErrorException(code: 0): Warning: spl_object_hash() expects parameter 1 to be object, integer given at /var/www/keen/vendor/doctrine/orm/lib/Doctrine/ORM/UnitOfWork.php:1239)"} []
[2017-12-17 23:41:18] php.WARNING: Warning: spl_object_hash() expects parameter 1 to be object, integer given {"exception":"[object] (ErrorException(code: 0): Warning: spl_object_hash() expects parameter 1 to be object, integer given at /var/www/keen/vendor/doctrine/orm/lib/Doctrine/ORM/UnitOfWork.php:1592)"} []
[2017-12-17 23:41:18] request.CRITICAL: Uncaught PHP Exception Symfony\Component\Form\Exception\RuntimeException: "Entities passed to the choice field must be managed. Maybe persist them in the entity manager?" at /var/www/keen/vendor/symfony/symfony/src/Symfony/Bridge/Doctrine/Form/ChoiceList/IdReader.php line 98 {"exception":"[object] (Symfony\\Component\\Form\\Exception\\RuntimeException(code: 0): Entities passed to the choice field must be managed. Maybe persist them in the entity manager? at /var/www/keen/vendor/symfony/symfony/src/Symfony/Bridge/Doctrine/Form/ChoiceList/IdReader.php:98)"} []
[2017-12-17 23:41:18] security.DEBUG: Stored the security token in the session. {"key":"_security_main"} []

在我的开发机器上的dev.log中没有类似的东西,它工作正常。在prod上我得到错误500.请帮忙吗?我尝试删除供应商并在dev和prod服务器上再次安装它。使用--env = prod多次清除缓存。


编辑

好的,在开发服务器上遇到了同样的问题(刚从apache配置中的app_dev.php切换到app.php并添加了app / autoload.php)

/**
 * @Route("/meme/add", name="meme_add")
 * @param Request $request
 * @return \Symfony\Component\HttpFoundation\Response
 * @internal param Request $request
 */
public function addAction(Request $request)
{
    $user = $this->getUser();
    if (!$user || !$user->hasRole('ROLE_MEME_ADD_AWAITING')) {
        $this->addFlash('notice', 'You need to login');
        return $this->redirectToRoute('fos_user_security_login');
    }

    if (!$this->get('security.authorization_checker')->isGranted('IS_AUTHENTICATED_FULLY')) {
        throw $this->createAccessDeniedException();
    }

    $meme = new Meme();
    $form = $this->createForm(MemeAddType::class, $meme);

    $form->handleRequest($request);

    if ($form->isSubmitted() && $form->isValid() ) {
        if ($user->hasRole('ROLE_MEME_ADD_AWAITING')) {
            $meme->setPublishingStatus('awaiting-publishing');
        } else if ($this->getUser()->hasRole('ROLE_MEME_ADD_MAIN')) {
            $meme->setPublishingStatus('published');
        }
        $em = $this->getDoctrine()->getManager();
        $em->persist($meme);
        $em->flush();
    }

    return $this->render(
        '@App/memes/add.html.twig',
        [
            'memeAddForm' => $form->createView()
        ]
    );
}

表格:

<?php

namespace AppBundle\Form;

use AppBundle\Entity\Language;
use AppBundle\Entity\Meme;
use Symfony\Bridge\Doctrine\Form\Type\EntityType;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\Extension\Core\Type\ChoiceType;
use Symfony\Component\Form\Extension\Core\Type\SubmitType;
use Symfony\Component\Form\Extension\Core\Type\TextareaType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolver;
use Vich\UploaderBundle\Form\Type\VichImageType;

class MemeAddType extends AbstractType
{
    public function buildForm(FormBuilderInterface $builder, array $options)
    {

        $builder
            ->add('textOne', TextareaType::class, [
                'label' => 'A phrase or a word',
                'attr' => ['style' => 'width:320px; height: 110px']
            ])
            ->add('textOneLang', EntityType::class , [
                'class' => Language::class,
                'choice_label' => 'language',
                'expanded' => false,
                'multiple' => false,
                'label' => 'Language of the phrase of the word',
                'data' => 2

            ])
            ->add('textTwo', TextareaType::class, [
                'label' => 'Translation, might be with an example',
                'attr' => ['style' => 'width:320px; height: 110px']
            ])
            ->add('textTwoLang', EntityType::class , [
                'class' => Language::class,
                'choice_label' => 'language',
                'expanded' => false,
                'multiple' => false,
                'label' => 'Language of the translation of the phrase of the word',
                'data' => 2
            ])
            ->add('languageLevel', ChoiceType::class, [
                'choices' => [
                    Meme::LEVEL_BEGINNER => Meme::LEVEL_BEGINNER,
                    Meme::LEVEL_INTERMEDIATE => Meme::LEVEL_INTERMEDIATE,
                    Meme::LEVEL_ADVANCED => Meme::LEVEL_ADVANCED,
                    Meme::LEVEL_EXPERT => Meme::LEVEL_EXPERT
                ]
            ])
            ->add('imageFile', VichImageType::class)
            ->add(
                'save',
                SubmitType::class,
                [
                    'attr' => ['class' => 'save']
                ]
            )
        ;
    }

    public function configureOptions(OptionsResolver $resolver)
    {
        $resolver->setDefaults([
            'data_class' => Meme::class
        ]);
    }

    public function getBlockPrefix()
    {
        return 'app_bundle_meme_add';
    }

    public function getName() {
        return 'app_bundle_meme_add';
    }
}
forms symfony
2个回答
2
投票

在formType中尝试这个

<?php

namespace AppBundle\Form;

use AppBundle\Entity\Language;
use AppBundle\Entity\Meme;
use Symfony\Bridge\Doctrine\Form\Type\EntityType;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\Extension\Core\Type\ChoiceType;
use Symfony\Component\Form\Extension\Core\Type\SubmitType;
use Symfony\Component\Form\Extension\Core\Type\TextareaType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolver;
use Vich\UploaderBundle\Form\Type\VichImageType;

class MemeAddType extends AbstractType
{
    public function buildForm(FormBuilderInterface $builder, array $options)
    {

        $builder
            ->add('textOne', TextareaType::class, [
                'label' => 'A phrase or a word',
                'attr' => ['style' => 'width:320px; height: 110px']
            ])
            ->add('textOneLang', EntityType::class , [
                'class' => Language::class,
                'choice_label' => 'language',
                'expanded' => false,
                'multiple' => false,
                'label' => 'Language of the phrase of the word',
                'data' => 2

            ])
            ->add('textTwo', TextareaType::class, [
                'label' => 'Translation, might be with an example',
                'attr' => ['style' => 'width:320px; height: 110px']
            ])
            ->add('textTwoLang', EntityType::class , [
                'class' => Language::class,
                'choice_label' => 'language',
                'expanded' => false,
                'multiple' => false,
                'label' => 'Language of the translation of the phrase of the word',
                'data' => 2
            ])
            ->add('languageLevel', ChoiceType::class, [
                'choices' => [
                    Meme::LEVEL_BEGINNER => Meme::LEVEL_BEGINNER,
                    Meme::LEVEL_INTERMEDIATE => Meme::LEVEL_INTERMEDIATE,
                    Meme::LEVEL_ADVANCED => Meme::LEVEL_ADVANCED,
                    Meme::LEVEL_EXPERT => Meme::LEVEL_EXPERT
                ]
            ])
            ->add('imageFile', VichImageType::class)
            ->add(
                'save',
                SubmitType::class,
                [
                    'attr' => ['class' => 'save']
                ]
            )
        ;
        $builder->get('languageLevel')->resetViewTransformers();
    }

    public function configureOptions(OptionsResolver $resolver)
    {
        $resolver->setDefaults([
            'data_class' => Meme::class
        ]);
    }

    public function getBlockPrefix()
    {
        return 'app_bundle_meme_add';
    }
}

1
投票

实际上,我发生了什么

 'data' => 2

在textOneLang表单元素中,当它被赋予spl_object_hash()函数时它失败了,它只接受对象而不是整数。没有使用数据转换器。但是这个'数据'参数不应该首先存在,不知道它是如何结束的。可能在尝试调试时。经验教训,找出所有选项,并将你在spl_object_hash()中作为参数转储。

app_dev.php和app.php之间的区别与它没有任何关系。它可能在app_dev.php中以某种方式缓存,并在一些旧版本上工作。

© www.soinside.com 2019 - 2024. All rights reserved.