Tom Belknap
Tom Belknap

Reputation: 23

Doctrine MongoDB bundle and Symfony Forms: associating a DocumentType to a field

I'm wanting to refer to documents within documents in MongoDB. I've discovered via this page that the MongoDB bundle includes a DocumentType which can be used as a Form Type, similar to the EntityType for table based ORM queries. https://www.doctrine-project.org/projects/doctrine-mongodb-bundle/en/5.0/form_validation.html

However, telling you that it even exists is the beginning and end of the documentation. When I go to implement it, I get an error message: Symfony\Bridge\Doctrine\Form\Type\DoctrineType::getCachedIdReader(): Argument #1 ($manager) must be of type Doctrine\Persistence\ObjectManager, null given, called in /var/www/clim_rep/vendor/symfony/doctrine-bridge/Form/Type/DoctrineType.php on line 199.

I'm not sure what to do next. The documentation points out that, "DocumentType expects the DocumentManager name or instance passed with the document_manager option instead of em." But that's all it says. No examples, no further explanation, just that. Here is the content of my Form Type class:

UPDATED My method of including the class was flawed and I've figured out how to correctly include it through $options, however now I'm getting a new message, "Doctrine MongoDB Manager named 'Doctrine\ODM\MongoDB\DocumentManager' does not exist" when it very much does?

<?php
/**
 * ReportType - This class is responsible for creating the form for all Reports
 **/
declare(strict_types=1);

namespace App\Form;

use App\Document\Partner;
use App\Document\Report;
use App\Document\State;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\Extension\Core\Type\NumberType;
use Symfony\Component\Form\Extension\Core\Type\TextType;
use Symfony\Component\Form\Extension\Core\Type\DateType;
use Symfony\Component\Form\Extension\Core\Type\ChoiceType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolver;
use Symfony\Component\Form\FormEvent;
use Symfony\Component\Form\FormEvents;
use Symfony\Component\Form\Event\PreSubmitEvent;
use Doctrine\Bundle\MongoDBBundle\Form\Type\DocumentType;
use Doctrine\ODM\MongoDB\DocumentManager;
use Doctrine\Persistence\ObjectManager;

/**
 * ReportType
---------------------
 * This class is responsible for creating the form for the rental entity.
 **/
class ReportType extends AbstractType
{
    /**
     *  buildForm -  This function is responsible for building the form for the rental entity.
     *
     * @param FormBuilderInterface $builder - The form builder interface
     * @param array                $options - The options for the form
     */
    public function buildForm(FormBuilderInterface $builder, array $options): void
    {
        $today = new \DateTime();
        $builder
            ->add('name', TextType::class)
            ->add('date', TextType::class)
            ->add('author', TextType::class)
            ->add('report_type', TextType::class)
            ->add('description', TextType::class)
            ->add('partners', DocumentType::class, [
                'class' => Partner::class,
                'choice_label' => 'name',
                'expanded' => true,
                'multiple' => true,
                'document_manager' => DocumentManager::class,
            ])
//          ->add('states', DocumentType::class, [
//              'class' => State::class,
//              'choice_label' => 'name',
//              'expanded' => true,
//              'multiple' => true,
//          ])
            ->add('dews_region', TextType::class)
            ->add('climate_region', TextType::class);
    }

    /**
     *  configureOptions - This function is responsible for setting the default options for the form.
     *
     * @param OptionsResolver $resolver - The options resolver
     * */
    public function configureOptions(OptionsResolver $resolver): void
    {
        $resolver->setDefaults(
            [
                'data_class' => Report::class,
                'entityManager' => null,
            ]
        );
    }
}

Upvotes: 1

Views: 39

Answers (0)

Related Questions