0
class AnalisiType extends AbstractType {

    /**
     * {@inheritdoc}
     */
    public function buildForm(FormBuilderInterface $builder, array $options) {

        if($options['data']->getIdProprietario()==NULL){
            $data_proprietario='';
        }else{
            $data_proprietario=$options['data']->getIdProprietario();
        }
 $em = $this->getDoctrine()->getEntityManager();
        $builder   ->add('idProprietario', EntityType::class, array(
                'label' => false,

                'placeholder' => 'Seleziona Anagrafica se presente',
                'class' => 'AppBundle:anagrafica',
                'query_builder' => function (anagraficaRepository $er ) {

                    return $er->createQueryBuilder('u')
                ->where('u.idTipologia = 1');
                },
                'choice_label' => 'ragione_sociale',
                        'data'=>$em->getReference("AppBundle:anagrafica",$options['data']->getIdProprietario()->getId())    ,

                'attr' => array(
                    'class' => 'chosen-select'
                ),
            ))

MY goal is in edit form set tha default data . Searching in forum i find that

'data'=>$em->getReference("AppBundle:anagrafica",$options['data']->getIdProprietario()->getId()) ,

is the correct syntax but i don't understand how to use doctrine in form . This form always give me error

Attempted to call an undefined method named "getDoctrine" of class "AppBundle\Form\AnalisiType"

i need to add use ??????

My only solution is create a service??

my services.yml

Learn more about services, parameters and containers at

http://symfony.com/doc/current/service_container.html

parameters: #parameter_name: value

services:
    #service_name:
    #    class: AppBundle\Directory\ClassName
    #    arguments: ['@another_service_name', 'plain_value', '%parameter_name%']

1 Answer 1

1

If you are on Symfony 2.8 you have to define your form as service and inject EntityManger:

services:
    form:
        class: YourBundle\Form\Type\YourType
        arguments: ["@doctrine.orm.entity_manager"]
        tags:
            - { name: form.type }

if you are on Symfony 3.3 your form is already a service and you can just typehint EntityManagerInterface in your construct:

private $entityManager;

public function __construct(EntityManagerInterface $entityManager)
{
    $this->entityManager = $entityManager;
}

also be sure that you have enabled autoconfigure in 3.3 or you will have to register your form as service:

services:
    _defaults:
        autowire: true
        autoconfigure: true
        public: false

    AppBundle\:
        resource: '../../src/AppBundle/*'
        exclude: '../../src/AppBundle/{Entity,Repository}'

    AppBundle\Controller\:
        resource: '../../src/AppBundle/Controller'
        public: true
        tags: ['controller.service_arguments']
Sign up to request clarification or add additional context in comments.

13 Comments

Thank's for help .. i have symfony 3.3 but now i have this error Catchable Fatal Error: Argument 1 passed to AppBundle\Form\AnalisiType::__construct() must implement interface Doctrine\ORM\EntityManagerInterface, and i have added use Doctrine\ORM\EntityManagerInterface;
Catchable Fatal Error: Argument 1 passed to AppBundle\Form\AnalisiType::__construct() must implement interface Doctrine\ORM\EntityManagerInterface, none given, called in C:\xampp\htdocs\richiesta_analisi\vendor\symfony\symfony\src\Symfony\Component\Form\FormRegistry.php on line 85 and defined
do you use autoconfigure? can you link me your service.yml?
# Learn more about services, parameters and containers at # symfony.com/doc/current/service_container.html parameters: #parameter_name: value services: #service_name: # class: AppBundle\Directory\ClassName # arguments: ['@another_service_name', 'plain_value', '%parameter_name%']
@FrancescoArreghini I edited my answer with my service.yml and maybe take a look at symfony.com/doc/current/service_container/3.3-di-changes.html
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.