symfony2admingenerator / AdmingeneratorGeneratorBundle

(old-legacy) Admingenerator for Symfony2, parse generator.yml files to build classes

Home Page:http://symfony2admingenerator.org/

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Doctrine ORM Manager named "default" does not exist.

ndoulgeridis opened this issue · comments

Hello,

I have multiple entity managers I have a generator which has a field

  category:
    label: field.category
    getter: category.name
    formType: entity

When i load the page I get this error:

Doctrine ORM Manager named "default" does not exist.

I haven't found a way in new docs to add em or entity manager to generator. How can i change default to the right one?

Hi @Crash21

In the generator-XXXX.yml file, you have a parameter entity_manager under the main params key.

You can define your own entity manager name here.

Hello @sescandell,

No its not working. I have researched it deeper and found on

Guesser/DoctrineORMFieldGuesser.php line 164

 'em' => 'default', // TODO: shouldn't this be configurable?

Hello,

Any suggestions how to fix this? If possible give me some guidelines from your point of you as you are more experienced with the Bundle and I will fix it.

Thanks.

A workaround would be to:

  1. create your own Builder extending the Admingen one
  2. override the default admingenerator.fieldguesser.doctrine.class to use your class
  3. In your fieldguesser class, create the following function:
class DoctrineORMFieldGuesser extends BaseDoctrineORMFieldGuesser
{
    public function getFormOptions($formType, $dbType, $columnName)
    {
        $formOptions = parent::getFormOptions($formType, $dbType, $columnName);
        if (array_key_exists('em', $formOptions)) {
            $formOptions['em'] = $this->doctrine->getManagerForClass($formOptions['class']);
        }

        return $formOptions;
    }
}

Let me know if it works, I'll create the right PR

Hmm not really understood :(
First of all which builder to extend? There are many builders.


Well what i did is I added in my parameters.yml
admingenerator.fieldguesser.doctrine.class
to use my Own Class and then added my own DoctrineORMFieldGuesser

use Admingenerator\GeneratorBundle\Guesser\DoctrineORMFieldGuesser as  AdminDoctrineORMFieldGuesser;


class DoctrineORMFieldGuesser extends AdminDoctrineORMFieldGuesser
/**
 * @param $formType
 * @param $dbType
 * @param $columnName
 * @return array
 */
public function getFormOptions($formType, $dbType, $columnName)
{
    $formOptions = parent::getFormOptions($formType, $dbType, $columnName);
    if (array_key_exists('em', $formOptions)) {
        $formOptions['em'] = $this->doctrine->getManagerForClass($formOptions['class']);
    }

    return $formOptions;
}

What i get is the following error:

Notice: Undefined property: DoctrineORMFieldGuesser::$doctrine in DoctrineORMFieldGuesser.php line 27

So $this->doctrine seems not working, trying to figure it out.



Well we can use

$formOptions['em'] = $this->container->get("doctrine")->getManagerForClass($formOptions["class"]);

but $this->container->get("doctrine")->getManagerForClass($formOptions["class"]); returns an EntityManager object not a string with connection name...

We need find a way to get connection string not instance

Notice: Undefined property: DoctrineORMFieldGuesser::$doctrine in DoctrineORMFieldGuesser.php line 27
So $this->doctrine seems not working, trying to figure it out.

Yes, actually $doctrine is private...

Here is a workaround... waiting for a real fix in the admingen (bubbling the configuration to form generation).

// in your field guesser custom class:

public function getFormOptions($formType, $dbType, $columnName)
{
    $formOptions = parent::getFormOptions($formType, $dbType, $columnName);
    if (array_key_exists('em', $formOptions)) {
        $formOptions['em'] = $this->getObjectManagerName($formOptions['class']);
    }

    return $formOptions;
}

protected function getObjectManagerName($className)
{
    $doctrine = $this->container->get('doctrine');
    $om = $doctrine->getManagerForClass($className);
    foreach ($doctrine->getManagerNames() as $omName) {
        if ($doctrine->getManager($omName) == $om) {
            return $omName;
        }
    }
    throw new \Exception('We should never be there');
}

Of course, this is a workaround. You might consider adding some cache capacities (or create a PR to bubble the generator configuration to the form generation :) )

Let me know if it works

Hey I did some fixes and this seems to be working fine:

public function getFormOptions($formType, $dbType, $columnName)
{
    $formOptions = parent::getFormOptions($formType, $dbType, $columnName);
    if (array_key_exists('em', $formOptions)) {
        $formOptions['em'] = $this->getObjectManagerName($formOptions['class']);
    }

    return $formOptions;
}

protected function getObjectManagerName($className)
{
    $doctrine = $this->container->get('doctrine');
    $om = $doctrine->getManagerForClass($className);
    foreach ($doctrine->getManagerNames() as $emName=>$seviceName)
    {
        $instance = $doctrine->getManager($emName);
        if ($instance == $om)
        {
            return $emName;
        }
    }
    throw new \Exception('We should never be there');
}

So next step is what now? Move these changes in actual AdminBuilder and PR?

@Crash21

  • Click on Fork button (top right of github repository page) to create your own copy of the repository.
  • git clone (repository git address) (folder to clone into) your repository to your computer, eg. git clone git@github.com:crash21/AdmingeneratorGeneratorBundle.git admingenerator (will create a folder "admingenerator" and clone your repo into it)
  • cd admingenerator to get into the folder
  • git checkout -b (branch-name) to create a new branch, eg. git checkout -b fix-issue-806
  • edit the repository to apply your fixes
  • git add . then git commit -m "Your commit message" to add commits
  • when you're done then git push origin fix-issue-806 -f to push your branch (-f is for "force", meaning it will create a new branch if it does not exist)
  • go to your github repository page
  • you should see "recently pushed branches"
  • click "create pull request" button next to it