我希望对EntityType
的选择看起来像multiple
一样好,就像Sonata使用sonata_type_model
(ModelType
)一样。
问题是,如何在自定义表单的sonata_type_model
之外使用SonataAdminBundle
?
我试过了:
->add('countries', 'sonata_type_model', array(
'label' => 'Countries',
'property' => 'name',
'multiple' => true
))
我也尝试过:
use Sonata\AdminBundle\Form\Type\ModelType;
...
->add('countries', ModelType::class, array(
'label' => 'Countries',
'property' => 'name',
'multiple' => true
))
但是不幸的是我遇到了一个错误:
可捕获的致命错误:参数1传递给
Sonata \ AdminBundle \ Form \ ChoiceList \ ModelChoiceLoader :: __ construct()
必须实现接口
Sonata \ AdminBundle \ Model \ ModelManagerInterface,给定null,在
C:\ xampp \ htdocs \ trainingexperience_symfony \ app \ cache \ dev \ classes.php
在18005行并已定义
更新
我遵循了在表单类型选项内定义
model_manager
属性的建议:protected $modelManager;
function __construct(ModelManager $modelManager)
{
$this->modelManager = $modelManager;
}
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
->add('preferredCountries', ModelType::class, array(
'label' => 'Countries',
'property' => 'name',
'multiple' => true,
'model_manager' => $this->modelManager
));
...
}
我从控制器传递它,例如:
/** @var ModelManager $modelManager */
$modelManager = $this->get('sonata.media.admin.media.manager');
$formType = new FormType($modelManager);
...
但是现在我得到一个错误:
类不存在
500内部服务器错误-ReflectionException
堆栈跟踪
in vendor\doctrine\common\lib\Doctrine\Common\Persistence\AbstractManagerRegistry.php at line 197 -
$class = $this->getAliasNamespace($namespaceAlias) . '\\' . $simpleClassName;
}
$proxyClass = new \ReflectionClass($class);
if ($proxyClass->implementsInterface($this->proxyInterfaceName)) {
if (! $parentClass = $proxyClass->getParentClass()) {
at ReflectionClass ->__construct ('')
in vendor\doctrine\common\lib\Doctrine\Common\Persistence\AbstractManagerRegistry.php at line 197 +
at AbstractManagerRegistry ->getManagerForClass (null)
in vendor\sonata-project\doctrine-orm-admin-bundle\Model\ModelManager.php at line 272 +
at ModelManager ->getEntityManager (null)
in vendor\sonata-project\doctrine-orm-admin-bundle\Model\ModelManager.php at line 64 +
at ModelManager ->getMetadata (null)
in vendor\sonata-project\doctrine-orm-admin-bundle\Model\ModelManager.php at line 377 +
at ModelManager ->getIdentifierFieldNames (null)
in vendor\sonata-project\admin-bundle\Form\ChoiceList\ModelChoiceLoader.php at line 75 +
at ModelChoiceLoader ->__construct (object(ModelManager), null, 'name', null, array(), object(PropertyAccessor))
in vendor\sonata-project\admin-bundle\Form\Type\ModelType.php at line 113 +
at ModelType ->Sonata\AdminBundle\Form\Type\{closure} (object(OptionsResolver), null)
in vendor\symfony\symfony\src\Symfony\Component\OptionsResolver\OptionsResolver.php at line 859 +
at OptionsResolver ->offsetGet ('choice_loader')
in vendor\symfony\symfony\src\Symfony\Component\Form\Extension\Core\Type\ChoiceType.php at line 320 +
at ChoiceType ->Symfony\Component\Form\Extension\Core\Type\{closure} (object(OptionsResolver), null)
in vendor\symfony\symfony\src\Symfony\Component\OptionsResolver\OptionsResolver.php at line 965 +
at OptionsResolver ->offsetGet ('choice_list')
in vendor\symfony\symfony\src\Symfony\Component\OptionsResolver\OptionsResolver.php at line 791 +
at OptionsResolver ->resolve (array('label' => 'Countries', 'property' => 'name', 'multiple' => true, 'model_manager' => object(ModelManager)))
in vendor\symfony\symfony\src\Symfony\Component\Form\ResolvedFormType.php at line 156 +
at ResolvedFormType ->createBuilder (object(FormFactory), 'preferredCountries', array('label' => 'Countries', 'property' => 'name', 'multiple' => true, 'model_manager' => object(ModelManager)))
in vendor\symfony\symfony\src\Symfony\Component\Form\Extension\DataCollector\Proxy\ResolvedTypeDataCollectorProxy.php at line 89 +
at ResolvedTypeDataCollectorProxy ->createBuilder (object(FormFactory), 'preferredCountries', array('label' => 'Countries', 'property' => 'name', 'multiple' => true, 'model_manager' => object(ModelManager)))
in vendor\symfony\symfony\src\Symfony\Component\Form\FormFactory.php at line 114 +
at FormFactory ->createNamedBuilder ('preferredCountries', 'Sonata\AdminBundle\Form\Type\ModelType', null, array('label' => 'Countries', 'property' => 'name', 'multiple' => true, 'model_manager' => object(ModelManager)))
in vendor\symfony\symfony\src\Symfony\Component\Form\FormBuilder.php at line 106 +
at FormBuilder ->create ('preferredCountries', 'Sonata\AdminBundle\Form\Type\ModelType', array('label' => 'Countries', 'property' => 'name', 'multiple' => true, 'model_manager' => object(ModelManager)))
in vendor\symfony\symfony\src\Symfony\Component\Form\FormBuilder.php at line 269 +
at FormBuilder ->resolveChildren ()
in vendor\symfony\symfony\src\Symfony\Component\Form\FormBuilder.php at line 215 +
at FormBuilder ->getForm ()
in vendor\symfony\symfony\src\Symfony\Component\Form\FormFactory.php at line 40 +
at FormFactory ->create (object(InternshipType), object(Internship), array())
in vendor\symfony\symfony\src\Symfony\Bundle\FrameworkBundle\Controller\Controller.php at line 282 +
at Controller ->createForm (object(InternshipType), object(Internship))
in src\IndexBundle\Controller\TraineeController.php at line 194 +
at TraineeController ->internshipsAction (object(Request))
at call_user_func_array (array(object(TraineeController), 'internshipsAction'), array(object(Request)))
in vendor\symfony\symfony\src\Symfony\Component\HttpKernel\HttpKernel.php at line 144 +
at HttpKernel ->handleRaw (object(Request), '1')
in vendor\symfony\symfony\src\Symfony\Component\HttpKernel\HttpKernel.php at line 64 +
at HttpKernel ->handle (object(Request), '1', true)
in vendor\symfony\symfony\src\Symfony\Component\HttpKernel\DependencyInjection\ContainerAwareHttpKernel.php at line 69 +
at ContainerAwareHttpKernel ->handle (object(Request), '1', true)
in vendor\symfony\symfony\src\Symfony\Component\HttpKernel\Kernel.php at line 185 +
at Kernel ->handle (object(Request))
in web\app_dev.php at line 30 +
最佳答案
您应该尝试为该类型提供model_manager
选项。它可能是一项服务,因此请查看debug:container
并尝试找出它。