symfony2admingenerator / AdmingeneratorGeneratorBundle

(old-legacy) Admingenerator for Symfony2, parse generator.yml files to build classes
http://symfony2admingenerator.org/
MIT License
360 stars 125 forks source link

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

Open ndoulgeridis opened 10 years ago

ndoulgeridis commented 10 years ago

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 commented 10 years ago

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 commented 10 years ago

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 commented 9 years ago

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 commented 9 years ago

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 commented 9 years ago

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 commented 9 years ago

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

ndoulgeridis commented 9 years ago

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 commented 9 years ago

@crash21