Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Doctrine ORM Manager named "default" does not exist. #806

Open
ndoulgeridis opened this issue Nov 7, 2014 · 8 comments
Open

Doctrine ORM Manager named "default" does not exist. #806

ndoulgeridis opened this issue Nov 7, 2014 · 8 comments

Comments

@ndoulgeridis
Copy link
Contributor

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?

@sescandell
Copy link
Member

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.

@ndoulgeridis
Copy link
Contributor Author

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?

@ndoulgeridis
Copy link
Contributor Author

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.

@sescandell
Copy link
Member

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

@ndoulgeridis
Copy link
Contributor Author

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

@sescandell
Copy link
Member

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

@sescandell sescandell added the Bug label Dec 16, 2014
@ndoulgeridis
Copy link
Contributor Author

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?

@ioleo
Copy link
Member

ioleo commented Dec 17, 2014

@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 [email protected]: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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants