簡體   English   中英

Symfony2 - 在包外使用Sonata字段類型

[英]Symfony2 - use Sonata field type outside the bundle

我希望有一個好看的EntityType用於multiple選,因為Sonata使用sonata_type_modelModelType )。

問題是,如何在我的自定義表單上使用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
))

但不幸的是我收到了一個錯誤:

可捕獲致命錯誤:傳遞給Sonata \\ AdminBundle \\ Form \\ ChoiceList \\ ModelChoiceLoader :: __ construct()的參數1必須實現接口Sonata \\ AdminBundle \\ Model \\ ModelManagerInterface,null給定,在C:\\ xampp \\ htdocs \\ trainingexperience_symfony \\ app \\ cache中調用\\ dev \\ classes.php在18005行並定義

UPDATE

我按照建議在表單類型選項中定義了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並嘗試發現它。

您收到此錯誤是因為sonata_type_model需要目標管理員的modelmanager來操作實體。

結論:您不能在未定義管理服務的實體上使用sonata_type_model

這是我使用的解決方法:

表格類型:

<?php

namespace Blast\BaseEntitiesBundle\Form\Type;

use Symfony\Component\Form\FormInterface;
use Symfony\Component\Form\FormView;
use Symfony\Component\OptionsResolver\OptionsResolver;
use Symfony\Component\OptionsResolver\Options;
use Blast\CoreBundle\Form\AbstractType;

class NestedTreeableType extends AbstractType
{   
    public function configureOptions(OptionsResolver $resolver)
    {
        $queryBuilder = function (Options $options) 
        {
            return $options['em']
                ->getRepository($options['class'])
                ->getNodesHierarchyQueryBuilder();
        };

    $choiceLabel = function($choice)
    {
         return str_repeat(' - ', $choice->getTreeLvl()) . (string) $choice;
    };

    $resolver->setDefaults([
        'query_builder' => $queryBuilder,
        'choice_label'  => $choiceLabel,
        'btn_add' => 'link_add',
        'btn_list' => 'link_list',
        'btn_delete' => 'link_delete',
        'btn_catalogue' => 'SonataAdminBundle',
    ]);
}

public function buildView(FormView $view, FormInterface $form, array $options)
{
    parent::buildView($view, $form, $options);

    $object_id = ($view->vars['name'] == 'treeParent') ? $form->getParent()->getData()->getId() : null;

    $choices = [];

    foreach ($view->vars['choices'] as $choice) {
            if ( $object_id && $choice->data->getId() == $object_id )
                $choice->attr['disabled'] = 'disabled';

        $choices[] = $choice;
    }

    $view->vars['choices'] = $choices;
    $view->vars['btn_add'] = $options['btn_add'];
    $view->vars['btn_list'] = $options['btn_list'];
    $view->vars['btn_delete'] = $options['btn_delete'];
    $view->vars['btn_catalogue'] = $options['btn_catalogue'];
}

public function getParent()
{
    return 'entity';
}

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

}

正如您所看到的,我正在定義選項並查看sonata_type_model需要的變量,現在是表單類型的模板

{% extends 'SonataDoctrineORMAdminBundle:Form:form_admin_fields.html.twig' %}

{% block blast_nested_treeable_widget %}
    {{ block('sonata_type_model_widget') }}
{% endblock %}

只需渲染一個sonata_type_model塊就可以了。 如果你想看看真實的例子: https//github.com/blast-project/BaseEntitiesBundle

希望有所幫助

我不知道有人還在搜索那個,但是,我發現你需要這個才能擁有modelManager(至少在2.7版本的symfony中):

/** @var ModelManager $modelManager */
$modelManager = $this->get('sonata.admin.manager.orm');
$formType = new FormType($modelManager);

...

byez

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM