Symfony2.3 Doctrine Entity Manager - isset或empty中的非法偏移类型

时间:2013-11-18 12:57:37

标签: php forms symfony doctrine-orm doctrine

我有一个基于Sytrfony 2.3和Doctrine的项目,我有一个实体'UserEducation'的表单'EducationType',它包含一个相关实体'EducationDegree'的选择框

$builder->add('educationDegree', 'entity', array('em'=>$this->em, 'class'=>'Hr\OnlineBundle\Entity\EducationDegree','required'=>true, 'empty_value' => '-- Please select --',  'attr'=>array('class'=>'full-width')));

请注意,将此作为独立形式使用时,它可以正常工作。

但是,当我将表单嵌入到另一种表单类型中时:

$builder->add('educations', 'collection', array(
    'label' => false,
    'required' => true, 
    'type' => new EducationType($this->fieldSettings, $this->em),
    'allow_add' => true,
    'allow_delete' => true,
    'by_reference' => false
));

在Doctrine \ Common \ Persistence \ AbstractManagerRegistry中我遇到问题:

if (!isset($this->managers[$name])) {:

警告:isset中的非法偏移类型或C:\ wamp \ www \ hronline \ vendor \ doctrine \ common \ lib \ Doctrine \ Common \ Persistence \ AbstractManagerRegistry.php第187行

$ name变量将作为对象(Doctrine \ ORM \ EntityManager)。

即使我明确地传递了一个null,也会发生这种情况:

$builder->add('educationDegree', 'entity', array('em'=>null, 'class'=>'Hr\OnlineBundle\Entity\EducationDegree','required'=>true, 'empty_value' => '-- Please select --',  'attr'=>array('class'=>'full-width')));

或隐含地:

$builder->add('educationDegree');

因此,AbstractManagerRegistry中的这一行永远不会执行:

if (null === $name) {
    $name = $this->defaultManager;
}

如果我们在getManager()方法中有这样的东西:

if (!is_string($name)) {
    $name = $this->defaultManager; 
}

它会起作用。

$ name var应该是一个字符串,而不是接口(https://github.com/doctrine/common/blob/master/lib/Doctrine/Common/Persistence/ManagerRegistry.php)中定义的对象。

有没有办法通过传递参数或以不同的方式嵌入表单来解决这个问题?


堆栈追踪:

in C:\wamp\www\hronline\vendor\doctrine\common\lib\Doctrine\Common\Persistence\AbstractManagerRegistry.php at line 181   -
            $name = $this->defaultManager;
        }
        if (!isset($this->managers[$name])) {
            throw new \InvalidArgumentException(sprintf('Doctrine %s Manager named "%s" does not exist.', $this->name, $name));
        }
at ErrorHandler ->handle ('2', 'Illegal offset type in isset or empty', 'C:\wamp\www\hronline\vendor\doctrine\common\lib\Doctrine\Common\Persistence\AbstractManagerRegistry.php', '181', array('name' => object(EntityManager))) 
in C:\wamp\www\hronline\vendor\doctrine\common\lib\Doctrine\Common\Persistence\AbstractManagerRegistry.php at line 181   + 
at AbstractManagerRegistry ->getManager (object(EntityManager)) 
in C:\wamp\www\hronline\vendor\symfony\symfony\src\Symfony\Bridge\Doctrine\Form\Type\DoctrineType.php at line 141   + 
at DoctrineType ->Symfony\Bridge\Doctrine\Form\Type\{closure} (object(Options), object(EntityManager)) 
in C:\wamp\www\hronline\vendor\symfony\symfony\src\Symfony\Component\OptionsResolver\Options.php at line 507   + 
at Options ->normalize ('em') 
in C:\wamp\www\hronline\vendor\symfony\symfony\src\Symfony\Component\OptionsResolver\Options.php at line 236   + 
at Options ->get ('em') 
in C:\wamp\www\hronline\vendor\symfony\symfony\src\Symfony\Component\OptionsResolver\Options.php at line 351   + 
at Options ->offsetGet ('em') 
in C:\wamp\www\hronline\vendor\symfony\symfony\src\Symfony\Bridge\Doctrine\Form\Type\DoctrineType.php at line 113   + 
at DoctrineType ->Symfony\Bridge\Doctrine\Form\Type\{closure} (object(Options), null) 
in C:\wamp\www\hronline\vendor\symfony\symfony\src\Symfony\Component\OptionsResolver\Options.php at line 467   + 
at Options ->resolve ('choice_list') 
in C:\wamp\www\hronline\vendor\symfony\symfony\src\Symfony\Component\OptionsResolver\Options.php at line 309   + 
at Options ->all () 
in C:\wamp\www\hronline\vendor\symfony\symfony\src\Symfony\Component\OptionsResolver\OptionsResolver.php at line 231   + 
at OptionsResolver ->resolve (array('em' => object(EntityManager), 'class' => 'Hr\OnlineBundle\Entity\EducationDegree', 'required' => true, 'empty_value' => '-- Please select --', 'attr' => array('class' => 'full-width'))) 
in C:\wamp\www\hronline\vendor\symfony\symfony\src\Symfony\Component\Form\ResolvedFormType.php at line 109   + 
at ResolvedFormType ->createBuilder (object(FormFactory), 'educationDegree', array('em' => object(EntityManager), 'class' => 'Hr\OnlineBundle\Entity\EducationDegree', 'required' => true, 'empty_value' => '-- Please select --', 'attr' => array('class' => 'full-width'))) 
in C:\wamp\www\hronline\vendor\symfony\symfony\src\Symfony\Component\Form\FormFactory.php at line 87   + 
at FormFactory ->createNamedBuilder ('educationDegree', 'entity', null, array('em' => object(EntityManager), 'class' => 'Hr\OnlineBundle\Entity\EducationDegree', 'required' => true, 'empty_value' => '-- Please select --', 'attr' => array('class' => 'full-width'))) 
in C:\wamp\www\hronline\vendor\symfony\symfony\src\Symfony\Component\Form\FormBuilder.php at line 106   + 
at FormBuilder ->create ('educationDegree', 'entity', array('em' => object(EntityManager), 'class' => 'Hr\OnlineBundle\Entity\EducationDegree', 'required' => true, 'empty_value' => '-- Please select --', 'attr' => array('class' => 'full-width'))) 
in C:\wamp\www\hronline\vendor\symfony\symfony\src\Symfony\Component\Form\FormBuilder.php at line 257   + 
at FormBuilder ->resolveChild ('educationDegree') 
in C:\wamp\www\hronline\vendor\symfony\symfony\src\Symfony\Component\Form\FormBuilder.php at line 122   + 
at FormBuilder ->get ('educationDegree') 
in C:\wamp\www\hronline\src\Hr\OnlineBundle\Form\GenericType.php at line 43   + 
at GenericType ->modifyRequiredOption (object(FormBuilder), 'educationDegree', false) 
in C:\wamp\www\hronline\src\Hr\OnlineBundle\Form\GenericType.php at line 32   + 
at GenericType ->adjustFields (object(FormBuilder)) 
in C:\wamp\www\hronline\src\Hr\OnlineBundle\Form\EducationType.php at line 45   + 
at EducationType ->buildForm (object(FormBuilder), array('disabled' => false, 'attr' => array(), 'translation_domain' => null, 'auto_initialize' => true, 'data_class' => 'Hr\OnlineBundle\Entity\UserEducation', 'empty_data' => object(Closure), 'trim' => true, 'required' => true, 'read_only' => false, 'max_length' => null, 'pattern' => null, 'property_path' => null, 'mapped' => true, 'by_reference' => true, 'error_bubbling' => true, 'label_attr' => array(), 'virtual' => null, 'inherit_data' => false, 'compound' => true, 'method' => 'POST', 'action' => '', 'validation_groups' => null, 'error_mapping' => array(), 'constraints' => array(), 'cascade_validation' => false, 'invalid_message' => 'This value is not valid.', 'invalid_message_parameters' => array(), 'extra_fields_message' => 'This form should not contain extra fields.', 'post_max_size_message' => 'The uploaded file was too large. Please try to upload a smaller file.', 'csrf_protection' => true, 'csrf_field_name' => '_token', 'csrf_provider' => object(SessionCsrfProvider), 'csrf_message' => 'The CSRF token is invalid. Please try to resubmit the form.', 'intention' => 'unknown', 'sonata_admin' => null, 'sonata_field_description' => null, 'label' => '__name__label__', 'block_name' => 'entry')) 
in C:\wamp\www\hronline\vendor\symfony\symfony\src\Symfony\Component\Form\ResolvedFormType.php at line 158   + 
at ResolvedFormType ->buildForm (object(FormBuilder), array('disabled' => false, 'attr' => array(), 'translation_domain' => null, 'auto_initialize' => true, 'data_class' => 'Hr\OnlineBundle\Entity\UserEducation', 'empty_data' => object(Closure), 'trim' => true, 'required' => true, 'read_only' => false, 'max_length' => null, 'pattern' => null, 'property_path' => null, 'mapped' => true, 'by_reference' => true, 'error_bubbling' => true, 'label_attr' => array(), 'virtual' => null, 'inherit_data' => false, 'compound' => true, 'method' => 'POST', 'action' => '', 'validation_groups' => null, 'error_mapping' => array(), 'constraints' => array(), 'cascade_validation' => false, 'invalid_message' => 'This value is not valid.', 'invalid_message_parameters' => array(), 'extra_fields_message' => 'This form should not contain extra fields.', 'post_max_size_message' => 'The uploaded file was too large. Please try to upload a smaller file.', 'csrf_protection' => true, 'csrf_field_name' => '_token', 'csrf_provider' => object(SessionCsrfProvider), 'csrf_message' => 'The CSRF token is invalid. Please try to resubmit the form.', 'intention' => 'unknown', 'sonata_admin' => null, 'sonata_field_description' => null, 'label' => '__name__label__', 'block_name' => 'entry')) 
in C:\wamp\www\hronline\vendor\symfony\symfony\src\Symfony\Component\Form\ResolvedFormType.php at line 117   + 
at ResolvedFormType ->createBuilder (object(FormFactory), '__name__', array('label' => '__name__label__', 'block_name' => 'entry')) 
in C:\wamp\www\hronline\vendor\symfony\symfony\src\Symfony\Component\Form\FormFactory.php at line 87   + 
at FormFactory ->createNamedBuilder ('__name__', object(EducationType), null, array('label' => '__name__label__', 'block_name' => 'entry')) 
in C:\wamp\www\hronline\vendor\symfony\symfony\src\Symfony\Component\Form\FormBuilder.php at line 106   + 
at FormBuilder ->create ('__name__', object(EducationType), array('label' => '__name__label__', 'block_name' => 'entry')) 
in C:\wamp\www\hronline\vendor\symfony\symfony\src\Symfony\Component\Form\Extension\Core\Type\CollectionType.php at line 32   + 
at CollectionType ->buildForm (object(FormBuilder), array('block_name' => null, 'disabled' => false, 'attr' => array(), 'translation_domain' => null, 'auto_initialize' => true, 'data_class' => null, 'empty_data' => object(Closure), 'trim' => true, 'read_only' => false, 'max_length' => null, 'pattern' => null, 'property_path' => null, 'mapped' => true, 'error_bubbling' => true, 'label_attr' => array(), 'virtual' => null, 'inherit_data' => false, 'compound' => true, 'method' => 'POST', 'action' => '', 'validation_groups' => null, 'error_mapping' => array(), 'constraints' => array(), 'cascade_validation' => false, 'invalid_message' => 'This value is not valid.', 'invalid_message_parameters' => array(), 'extra_fields_message' => 'This form should not contain extra fields.', 'post_max_size_message' => 'The uploaded file was too large. Please try to upload a smaller file.', 'csrf_protection' => true, 'csrf_field_name' => '_token', 'csrf_provider' => object(SessionCsrfProvider), 'csrf_message' => 'The CSRF token is invalid. Please try to resubmit the form.', 'intention' => 'unknown', 'sonata_admin' => null, 'sonata_field_description' => null, 'prototype' => true, 'prototype_name' => '__name__', 'options' => array('block_name' => 'entry'), 'label' => false, 'required' => true, 'type' => object(EducationType), 'allow_add' => true, 'allow_delete' => true, 'by_reference' => false)) 
in C:\wamp\www\hronline\vendor\symfony\symfony\src\Symfony\Component\Form\ResolvedFormType.php at line 158   + 
at ResolvedFormType ->buildForm (object(FormBuilder), array('block_name' => null, 'disabled' => false, 'attr' => array(), 'translation_domain' => null, 'auto_initialize' => true, 'data_class' => null, 'empty_data' => object(Closure), 'trim' => true, 'read_only' => false, 'max_length' => null, 'pattern' => null, 'property_path' => null, 'mapped' => true, 'error_bubbling' => true, 'label_attr' => array(), 'virtual' => null, 'inherit_data' => false, 'compound' => true, 'method' => 'POST', 'action' => '', 'validation_groups' => null, 'error_mapping' => array(), 'constraints' => array(), 'cascade_validation' => false, 'invalid_message' => 'This value is not valid.', 'invalid_message_parameters' => array(), 'extra_fields_message' => 'This form should not contain extra fields.', 'post_max_size_message' => 'The uploaded file was too large. Please try to upload a smaller file.', 'csrf_protection' => true, 'csrf_field_name' => '_token', 'csrf_provider' => object(SessionCsrfProvider), 'csrf_message' => 'The CSRF token is invalid. Please try to resubmit the form.', 'intention' => 'unknown', 'sonata_admin' => null, 'sonata_field_description' => null, 'prototype' => true, 'prototype_name' => '__name__', 'options' => array('block_name' => 'entry'), 'label' => false, 'required' => true, 'type' => object(EducationType), 'allow_add' => true, 'allow_delete' => true, 'by_reference' => false)) 
in C:\wamp\www\hronline\vendor\symfony\symfony\src\Symfony\Component\Form\ResolvedFormType.php at line 117   + 
at ResolvedFormType ->createBuilder (object(FormFactory), 'educations', array('label' => false, 'required' => true, 'type' => object(EducationType), 'allow_add' => true, 'allow_delete' => true, 'by_reference' => false)) 
in C:\wamp\www\hronline\vendor\symfony\symfony\src\Symfony\Component\Form\FormFactory.php at line 87   + 
at FormFactory ->createNamedBuilder ('educations', 'collection', null, array('label' => false, 'required' => true, 'type' => object(EducationType), 'allow_add' => true, 'allow_delete' => true, 'by_reference' => false)) 
in C:\wamp\www\hronline\vendor\symfony\symfony\src\Symfony\Component\Form\FormBuilder.php at line 106   + 
at FormBuilder ->create ('educations', 'collection', array('label' => false, 'required' => true, 'type' => object(EducationType), 'allow_add' => true, 'allow_delete' => true, 'by_reference' => false)) 
in C:\wamp\www\hronline\vendor\symfony\symfony\src\Symfony\Component\Form\FormBuilder.php at line 270   + 
at FormBuilder ->resolveChildren () 
in C:\wamp\www\hronline\vendor\symfony\symfony\src\Symfony\Component\Form\FormBuilder.php at line 218   + 
at FormBuilder ->getForm () 
in C:\wamp\www\hronline\vendor\symfony\symfony\src\Symfony\Component\Form\FormFactory.php at line 39   + 
at FormFactory ->create (object(UserEditType), object(User), array()) 
in C:\wamp\www\hronline\vendor\symfony\symfony\src\Symfony\Bundle\FrameworkBundle\Controller\Controller.php at line 163   +
at Controller ->createForm (object(UserEditType), object(User)) 
in C:\wamp\www\hronline\src\Hr\OnlineBundle\Controller\UserController.php at line 268   + 
at UserController ->editAction () 
at call_user_func_array (array(object(UserController), 'editAction'), array()) 
in C:\wamp\www\hronline\app\bootstrap.php.cache at line 2844   + 
at HttpKernel ->handleRaw (object(Request), '1') 
in C:\wamp\www\hronline\app\bootstrap.php.cache at line 2818   + 
at HttpKernel ->handle (object(Request), '1', true) 
in C:\wamp\www\hronline\app\bootstrap.php.cache at line 2947   + 
at ContainerAwareHttpKernel ->handle (object(Request), '1', true) 
in C:\wamp\www\hronline\app\bootstrap.php.cache at line 2249   + 
at Kernel ->handle (object(Request)) 
in C:\wamp\www\hronline\web\app_dev.php at line 28   + 

1 个答案:

答案 0 :(得分:0)

我得到了同样的错误,但在另一种情况下。我创建了一个service并将其注入其他服务,但我没有注意到第二个服务有定义factory-service="doctrine"

<service id="xxx.my_repository" class="Xxx\Repository" factory-service="doctrine" factory-method="getRepository">
<argument type="service">xxx.my_service</argument>
</service>

所以它被视为ObjectManager。所以我必须把它改成

<service id="xxx.my_repository" class="Xxx\Repository" factory-service="doctrine" factory-method="getRepository">
  <call method="setMyService">   
    <argument type="service">xxx.my_service</argument></call>
</service>

问题已得到解决。我希望我能帮助有共同问题的人。