Doctrine cannot map entity/repository namespace in chain

后端 未结 4 1800
暗喜
暗喜 2021-01-24 17:44

I\'m trying to setup symfony with doctrine, configuring doctrine is new for me.

I try to retrieve a entity from the database by using the following code in my controller

相关标签:
4条回答
  • 2021-01-24 18:12

    After a long struggle I found out what caused the problem. My symfony environment was running in prod mode. I switched to dev. And it all worked like a charm.

    But i'm still wondering why?! I checked The AppKernel.php and it looks like:

    $bundles = [
        new Symfony\Bundle\FrameworkBundle\FrameworkBundle(),
        new Symfony\Bundle\SecurityBundle\SecurityBundle(),
        new Symfony\Bundle\TwigBundle\TwigBundle(),
        new Symfony\Bundle\MonologBundle\MonologBundle(),
        new Symfony\Bundle\SwiftmailerBundle\SwiftmailerBundle(),
        new Doctrine\Bundle\DoctrineBundle\DoctrineBundle(),
        new Sensio\Bundle\FrameworkExtraBundle\SensioFrameworkExtraBundle(),
        new AppBundle\AppBundle(),
    ];
    
    if (in_array($this->getEnvironment(), ['dev', 'test'], true)) {
        $bundles[] = new Symfony\Bundle\DebugBundle\DebugBundle();
        $bundles[] = new Symfony\Bundle\WebProfilerBundle\WebProfilerBundle();
        $bundles[] = new Sensio\Bundle\DistributionBundle\SensioDistributionBundle();
    
        if ('dev' === $this->getEnvironment()) {
            $bundles[] = new Sensio\Bundle\GeneratorBundle\SensioGeneratorBundle();
            $bundles[] = new Symfony\Bundle\WebServerBundle\WebServerBundle();
        }
    }
    
    return $bundles;
    

    It seems to me no matter what environment I run, doctrine is loaded. Can somebody explain to me the reason behind this behaviour.

    EDIT:

    I discovered something more, I switched to prod environment again but now I started flipping debugging on/off.

    Now I see that is works nicely in production mode but only when debugging is enabled. Why?

    0 讨论(0)
  • 2021-01-24 18:17

    try this:

    $feedImport = $this->getDoctrine()->getRepository('AppBundle:MyClass');
    

    if you want to access a particular function/method

    $feedImport = $this->getDoctrine()->getRepository('AppBundle:MyClass')->sampleMethod();
    
    0 讨论(0)
  • 2021-01-24 18:28

    Your bundle isn't declare in the AppKernel file or your doctrine mapping isn't reconized.

    You should have in your AppKernel :

    public function registerBundles()
    {
        $bundles = array(
            ...
            new AppBundle\AppBundle(),
        );
    
        ....
    
        return $bundles;
    }
    

    And by default in your config.yml :

    orm:
        auto_generate_proxy_classes: %kernel.debug%
        auto_mapping: true
    
    0 讨论(0)
  • 2021-01-24 18:32

    Try this:

    /**
     *
     * @ORM\Table(name="my_table_name")
     * @ORM\Entity()
     */
    class MyClass
    
    0 讨论(0)
提交回复
热议问题