Van Peterson
Van Peterson

Reputation: 91

No entity manager defined for class Doctrine\ORM\PersistentCollection

I'm using Symfony 4.1 with SonataAdminBundle 3.36.

I get this error when I set ModelType::class on ManyToMany relation field call tags:

No entity manager defined for class Doctrine\ORM\PersistentCollection

Also I could not use sonata_type_model or sonata_type_model_list.

src/Admin/CategoryAdmin.php

namespace App\Admin;

use Sonata\AdminBundle\Admin\AbstractAdmin;
use Sonata\AdminBundle\Datagrid\ListMapper;
use Sonata\AdminBundle\Datagrid\DatagridMapper;
use Sonata\AdminBundle\Form\FormMapper;
use Symfony\Component\Form\Extension\Core\Type\TextType;
use Symfony\Component\Form\Extension\Core\Type\EmailType;
use Symfony\Component\Form\Extension\Core\Type\TextareaType;
use Symfony\Component\Form\Extension\Core\Type\CheckboxType;

use Symfony\Bridge\Doctrine\Form\Type\EntityType;
use Sonata\AdminBundle\Form\Type\ModelType;
use Sonata\AdminBundle\Form\Type\ModelListType;


class FactureAdmin extends AbstractAdmin
{
    protected $datagridValues = [

        // display the first page (default = 1)
        '_page' => 1,

        // reverse order (default = 'ASC')
        '_sort_order' => 'DESC',

        // name of the ordered field (default = the model's id field, if any)
        '_sort_by' => 'dateIn',
    ];

    protected function configureFormFields(FormMapper $formMapper)
    {
        $formMapper
            ->tab('Facture')
                ->with('Infos Générales', ['class' => 'col-md-8'])
                    ->add('title', TextType::class, array('label' => 'Libellé'))
                    ->add('slug', TextType::class, array('label' => 'Slug', 'required' => false))
                    // ->add('status')
                    ->add('notice', TextareaType::class, array('label' => 'Remarques', 'required' => false))
                ->end()
                ->with('Traitement', ['class' => 'col-md-4'])
                    ->add('user', EntityType::class, [
                        'class' => User::class,
                        'choice_label' => 'username',
                        'label' => 'Chargé',
                    ])
                    ->add('tags', ModelType::class, [
                        'attr' => [
                            'expanded' => true
                        ]
                    ])
                    // ->add('tags', 'sonata_type_model', array('expanded' => true, 'multiple' => true))
                    ->add('duration', null, array('label' => 'Durée (Nombre de jours)', 'required' => false))
                    ->add('status',  CheckboxType::class, array('required' => false, 'label' => 'Statut (Payée/Non payée)'), array('transform' => true))
                    ->add('active')
                ->end()
            ->end()
            ->tab('Prestataire')
                ->with('Identité', ['class' => 'col-md-6'])
                    // ->add('status')
                    ->add('prestataire', EntityType::class, [
                        'class' => Prestataire::class,
                        'choice_label' => 'name',
                        // 'label' => 'Sélectionnez le prestataire',
                    ])
                    ->add('providerName', TextType::class, array('label' => 'Nom représentant'))
                    ->add('providerPhone', TextType::class, array('label' => 'Numéro représentant', 'required' => false))
                    ->add('providerEmail', EmailType::class, array('label' => 'Email représentant', 'required' => false))
                ->end()
            ->end()
            ->tab('Media')
                ->with('Numérisation', ['class' => 'col-md-8 col-md-offset-0', 'description' => 'Veuillez uploader la facture numérisée'])
                    // ->add('status')
                    ->add('billScan', ModelListType::class)
                ->end()
            ->end()
        ;
    }
}

}

Upvotes: 7

Views: 2776

Answers (1)

cezar
cezar

Reputation: 12012

For me it worked after I added 'multiple' => true to the ModelType::class:

->add('tags', ModelType::class, [
    'multiple' => true,
    // your other options
])

After this the form was displayed properly.

Upvotes: 12

Related Questions