Search code examples
mongodbsymfonysonata-admin

Sonata Admin MongoDB sonata_type_model_autocomplete


Situation

I have two documents

Company
- id
- staff (ReferenceOne Staff)

Staff
- id
- firstName (string)
- lastName (string)

There are a lot of possible staff so, all over the board I'm trying to convert the select2 boxes to use the 'sonata_type_mpde_autocomplete' but, Sonata is always halting on the complaing that the model_manager is null.

I followed the document from Sonata Admin at the following: https://sonata-project.org/bundles/admin/master/doc/reference/form_types.html#sonata-type-model-autocomplete

I see that there is a property to set but, in all honesty what would I put in the there? I will try to use the __toString() function once this is working but for the moment below is what I have

I have looked around and created a model_manager for staff

namespace AppBundle\ModelManager;

use Doctrine\ODM\MongoDB\DocumentManager;
use AppBundle\Repository\IndividualRepository;

class IndividualManager
{
  protected $dm;
  protected $repo;
  protected $class;

  public function __construct(DocumentManager $dm, $class)
  {
    $this->dm = $dm;
    $this->class = $class;
    $this->repo = $dm->getRepository($class);
  }
}

In the company admin

->add('staff', 'sonata_type_model_autocomplete', array(
        'property' => 'firstName'))

In the individual admin, I've gone and added 'firstName' to the dataGrid filter.

$dataGridMapper
    ->add('firstName');

Solution

  • just add

    'model_manager' => $datagridMapper->getAdmin()->getModelManager(),
    

    to options array