注意:Undefined index: parent 在 Sonata admin 中添加多选时

Notice: Undefined index: parent when adding multiselect in Sonata admin

我正在做类似的回答:

 ->add('dependsOn', 'entity',  array(
                    'class' => ProductOption::class,
                    'attr' => array('class' => "form-control",  'multiple' => true),
                    'required' => false,
                    'multiple'=> true,

                )
            );

但是加载表单时出现错误

注意:未定义索引:parent

[1] Symfony\Component\Debug\Exception\ContextErrorException: Notice: Undefined index: parent
    at n/a
        in /home/darius/PhpstormProjects/vop/vendor/doctrine/orm/lib/Doctrine/ORM/Persisters/Entity/BasicEntityPersister.php line 1768

    at Symfony\Component\Debug\ErrorHandler->handleError('8', 'Undefined index: parent', '/home/darius/PhpstormProjects/vop/vendor/doctrine/orm/lib/Doctrine/ORM/Persisters/Entity/BasicEntityPersister.php', '1768', array('assoc' => array('fieldName' => 'dependsOn', 'mappedBy' => 'parent', 'targetEntity' => 'Vop\ProductBundle\Entity\ProductOption', 'cascade' => array(), 'orphanRemoval' => false, 'fetch' => '2', 'type' => '4', 'inversedBy' => null, 'isOwningSide' => false, 'sourceEntity' => 'Vop\ProductBundle\Entity\ProductOption', 'isCascadeRemove' => false, 'isCascadePersist' => false, 'isCascadeRefresh' => false, 'isCascadeMerge' => false, 'isCascadeDetach' => false), 'sourceEntity' => object(ProductOption), 'offset' => null, 'limit' => null, 'criteria' => array(), 'parameters' => array()))
        in /home/darius/PhpstormProjects/vop/vendor/doctrine/orm/lib/Doctrine/ORM/Persisters/Entity/BasicEntityPersister.php line 1768

    at Doctrine\ORM\Persisters\Entity\BasicEntityPersister->getOneToManyStatement(array('fieldName' => 'dependsOn', 'mappedBy' => 'parent', 'targetEntity' => 'Vop\ProductBundle\Entity\ProductOption', 'cascade' => array(), 'orphanRemoval' => false, 'fetch' => '2', 'type' => '4', 'inversedBy' => null, 'isOwningSide' => false, 'sourceEntity' => 'Vop\ProductBundle\Entity\ProductOption', 'isCascadeRemove' => false, 'isCascadePersist' => false, 'isCascadeRefresh' => false, 'isCascadeMerge' => false, 'isCascadeDetach' => false), object(ProductOption))
        in /home/darius/PhpstormProjects/vop/vendor/doctrine/orm/lib/Doctrine/ORM/Persisters/Entity/BasicEntityPersister.php line 1747

    at Doctrine\ORM\Persisters\Entity\BasicEntityPersister->loadOneToManyCollection(array('fieldName' => 'dependsOn', 'mappedBy' => 'parent', 'targetEntity' => 'Vop\ProductBundle\Entity\ProductOption', 'cascade' => array(), 'orphanRemoval' => false, 'fetch' => '2', 'type' => '4', 'inversedBy' => null, 'isOwningSide' => false, 'sourceEntity' => 'Vop\ProductBundle\Entity\ProductOption', 'isCascadeRemove' => false, 'isCascadePersist' => false, 'isCascadeRefresh' => false, 'isCascadeMerge' => false, 'isCascadeDetach' => false), object(ProductOption), object(PersistentCollection))
        in /home/darius/PhpstormProjects/vop/vendor/doctrine/orm/lib/Doctrine/ORM/UnitOfWork.php line 2826

    at Doctrine\ORM\UnitOfWork->loadCollection(object(PersistentCollection))
        in /home/darius/PhpstormProjects/vop/vendor/doctrine/orm/lib/Doctrine/ORM/PersistentCollection.php line 697

    at Doctrine\ORM\PersistentCollection->doInitialize()
        in /home/darius/PhpstormProjects/vop/vendor/doctrine/orm/lib/Doctrine/ORM/PersistentCollection.php line 214

    at Doctrine\ORM\PersistentCollection->initialize()
        in /home/darius/PhpstormProjects/vop/vendor/doctrine/collections/lib/Doctrine/Common/Collections/AbstractLazyCollection.php line 157

    at Doctrine\Common\Collections\AbstractLazyCollection->toArray()
        in /home/darius/PhpstormProjects/vop/vendor/symfony/symfony/src/Symfony/Bridge/Doctrine/Form/DataTransformer/CollectionToArrayTransformer.php line 49

    at Symfony\Bridge\Doctrine\Form\DataTransformer\CollectionToArrayTransformer->transform(object(PersistentCollection))
        in /home/darius/PhpstormProjects/vop/vendor/symfony/symfony/src/Symfony/Component/Form/Form.php line 1146

    at Symfony\Component\Form\Form->normToView(object(PersistentCollection))
        in /home/darius/PhpstormProjects/vop/vendor/symfony/symfony/src/Symfony/Component/Form/Form.php line 353

    at Symfony\Component\Form\Form->setData(object(PersistentCollection))
        in /home/darius/PhpstormProjects/vop/vendor/symfony/symfony/src/Symfony/Component/Form/Extension/Core/DataMapper/PropertyPathMapper.php line 57

    at Symfony\Component\Form\Extension\Core\DataMapper\PropertyPathMapper->mapDataToForms(object(ProductOption), object(RecursiveIteratorIterator))
        in /home/darius/PhpstormProjects/vop/vendor/symfony/symfony/src/Symfony/Component/Form/Form.php line 386

    at Symfony\Component\Form\Form->setData(object(ProductOption))
        in /home/darius/PhpstormProjects/vop/vendor/symfony/symfony/src/Symfony/Component/Form/Extension/Core/DataMapper/PropertyPathMapper.php line 57

    at Symfony\Component\Form\Extension\Core\DataMapper\PropertyPathMapper->mapDataToForms(object(PersistentCollection), object(RecursiveIteratorIterator))
        in /home/darius/PhpstormProjects/vop/vendor/symfony/symfony/src/Symfony/Component/Form/Form.php line 386

    at Symfony\Component\Form\Form->setData(object(PersistentCollection))
        in /home/darius/PhpstormProjects/vop/vendor/symfony/symfony/src/Symfony/Component/Form/Extension/Core/DataMapper/PropertyPathMapper.php line 57

    at Symfony\Component\Form\Extension\Core\DataMapper\PropertyPathMapper->mapDataToForms(object(ProductOptionGroup), object(RecursiveIteratorIterator))
        in /home/darius/PhpstormProjects/vop/vendor/symfony/symfony/src/Symfony/Component/Form/Form.php line 386

    at Symfony\Component\Form\Form->setData(object(ProductOptionGroup))
        in /home/darius/PhpstormProjects/vop/vendor/symfony/symfony/src/Symfony/Component/Form/Form.php line 478

    at Symfony\Component\Form\Form->initialize()
        in /home/darius/PhpstormProjects/vop/vendor/symfony/symfony/src/Symfony/Component/Form/FormBuilder.php line 226

    at Symfony\Component\Form\FormBuilder->getForm()
        in /home/darius/PhpstormProjects/vop/src/Vop/ProductBundle/Controller/ProductOptionGroupAdminController.php line 180

    at Vop\ProductBundle\Controller\ProductOptionGroupAdminController->createProductOptionGroupForm(object(ProductOptionGroup), 'option_group_form_0')
        in /home/darius/PhpstormProjects/vop/src/Vop/ProductBundle/Controller/ProductOptionGroupAdminController.php line 162

    at Vop\ProductBundle\Controller\ProductOptionGroupAdminController->getProductOptionGroupForms(object(Product))
        in /home/darius/PhpstormProjects/vop/src/Vop/ProductBundle/Controller/ProductOptionGroupAdminController.php line 37

    at Vop\ProductBundle\Controller\ProductOptionGroupAdminController->listAction()
        in  line 

    at call_user_func_array(array(object(ProductOptionGroupAdminController), 'listAction'), array())
        in /home/darius/PhpstormProjects/vop/vendor/symfony/symfony/src/Symfony/Component/HttpKernel/HttpKernel.php line 144

    at Symfony\Component\HttpKernel\HttpKernel->handleRaw(object(Request), '1')
        in /home/darius/PhpstormProjects/vop/vendor/symfony/symfony/src/Symfony/Component/HttpKernel/HttpKernel.php line 64

    at Symfony\Component\HttpKernel\HttpKernel->handle(object(Request), '1', true)
        in /home/darius/PhpstormProjects/vop/vendor/symfony/symfony/src/Symfony/Component/HttpKernel/DependencyInjection/ContainerAwareHttpKernel.php line 69

    at Symfony\Component\HttpKernel\DependencyInjection\ContainerAwareHttpKernel->handle(object(Request), '1', true)
        in /home/darius/PhpstormProjects/vop/vendor/symfony/symfony/src/Symfony/Component/HttpKernel/Kernel.php line 185

    at Symfony\Component\HttpKernel\Kernel->handle(object(Request))
        in /home/darius/PhpstormProjects/vop/web/app_dev.php line 28

为什么会这样?

来自composer.json的奏鸣曲版本:

"sonata-project/doctrine-orm-admin-bundle": "^2.2",

Symfony:

"symfony/symfony": "^2.8",

我试过各种方法。无法找到如何执行此操作的正常教程。

问题出在这里

https://www.doctrine-project.org/projects/doctrine-orm/en/2.6/reference/association-mapping.html#one-to-many-self-referencing

mappedBy="parent"

但是 ProductOption 实体中没有父字段。

仍然没有保存,但至少不会抛出这个错误,所以这是对这个问题的回答。