1

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:

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

But I keep getting the MappingException error:

The class 'AppBundle\Entity\MyClass' was not found in the chain configured namespaces

I also tried to manually specify the fully qualified class name. But this didn't help.

However the way I looked to it the error message seems to suggest I need to do some additional configuring: that I need to add my entities/repositories to "The chain of configured namespaces". But I have some trouble finding this chain and how to configure it (if my assumptions are correct ofcourse)

Question #1: Is there some kind of namespace chain? If there is where can I see examples of how to configure it?

All the entity information is in my AppBundle bundle. see the bundle structure below:

...
└── AppBundle
    ├── AppBundle.php
    ├── Controller
    │   └── DefaultController.php
    ├── Entity
    │   └── MyClass.php
    ├── Repository
    │   └── MyClassRepository.php
    └── Resources
        └── config
            └── doctrine
                └── MyClass.orm.yml
...

I generated the entity class / repository / orm.yml files using the symfony bin\console tool.

If i use the same console tool to check the doctrine mapping info with: doctrine:mapping:info command I show me that the classes are correctly/sucessfuly mapped:

Found 1 mapped entity:
[OK]   AppBundle\Entity\MyClass

About the process of generation of the models/entity configuration files. I generated the meta data yaml files using the symfony console tool. By running the command: doctrine:mapping:import. After the yml files where created I used 'doctrine:generate:entities' to generate the enity classes.

I was under the impression that if the files where generated and store in a working bundle in the Entity folder, the auto_mapping: true would automatically take care of this mapping thingy.

Is this assumption correct? If not please tell me what is wrong.

  • The entity files and configuration are stored under the working bundle AppBundle

  • The AppBundle is registered and working (the controller is doing his thing)

  • The command: doctrine:mapping:info seems to map the entities right away.

For the full code of the Entity Model:

<?php

namespace AppBundle\Entity;

/**
 * MyClass
 */
class MyClass
{
    /**
     * @var integer
     */
    private $id;

    /**
     * @var integer
     */
    private $customerId;

    /**
     * @var integer
     */
    private $feedId;

    /**
     * @var string
     */
    private $contentHash;

    /**
     * @var string
     */
    private $headerHash;

    /**
     * @var string
     */
    private $feedName;

    /**
     * @var integer
     */
    private $fileSize;

    /**
     * @var integer
     */
    private $totalHeaderFields;

    /**
     * @var integer
     */
    private $totalRecords;

    /**
     * @var string
     */
    private $importStatus;

    /**
     * @var \DateTime
     */
    private $datetimeCreated = 'CURRENT_TIMESTAMP';

    /**
     * @var \DateTime
     */
    private $datetimeProcessed;


    /**
     * Get id
     *
     * @return integer
     */
    public function getId()
    {
        return $this->id;
    }

    /**
     * Set customerId
     *
     * @param integer $customerId
     *
     * @return MyClass
     */
    public function setCustomerId($customerId)
    {
        $this->customerId = $customerId;

        return $this;
    }

    /**
     * Get customerId
     *
     * @return integer
     */
    public function getCustomerId()
    {
        return $this->customerId;
    }

    /**
     * Set feedId
     *
     * @param integer $feedId
     *
     * @return MyClass
     */
    public function setFeedId($feedId)
    {
        $this->feedId = $feedId;

        return $this;
    }

    /**
     * Get feedId
     *
     * @return integer
     */
    public function getFeedId()
    {
        return $this->feedId;
    }

    /**
     * Set contentHash
     *
     * @param string $contentHash
     *
     * @return MyClass
     */
    public function setContentHash($contentHash)
    {
        $this->contentHash = $contentHash;

        return $this;
    }

    /**
     * Get contentHash
     *
     * @return string
     */
    public function getContentHash()
    {
        return $this->contentHash;
    }

    /**
     * Set headerHash
     *
     * @param string $headerHash
     *
     * @return MyClass
     */
    public function setHeaderHash($headerHash)
    {
        $this->headerHash = $headerHash;

        return $this;
    }

    /**
     * Get headerHash
     *
     * @return string
     */
    public function getHeaderHash()
    {
        return $this->headerHash;
    }

    /**
     * Set feedName
     *
     * @param string $feedName
     *
     * @return MyClass
     */
    public function setFeedName($feedName)
    {
        $this->feedName = $feedName;

        return $this;
    }

    /**
     * Get feedName
     *
     * @return string
     */
    public function getFeedName()
    {
        return $this->feedName;
    }

    /**
     * Set fileSize
     *
     * @param integer $fileSize
     *
     * @return MyClass
     */
    public function setFileSize($fileSize)
    {
        $this->fileSize = $fileSize;

        return $this;
    }

    /**
     * Get fileSize
     *
     * @return integer
     */
    public function getFileSize()
    {
        return $this->fileSize;
    }

    /**
     * Set totalHeaderFields
     *
     * @param integer $totalHeaderFields
     *
     * @return MyClass
     */
    public function setTotalHeaderFields($totalHeaderFields)
    {
        $this->totalHeaderFields = $totalHeaderFields;

        return $this;
    }

    /**
     * Get totalHeaderFields
     *
     * @return integer
     */
    public function getTotalHeaderFields()
    {
        return $this->totalHeaderFields;
    }

    /**
     * Set totalRecords
     *
     * @param integer $totalRecords
     *
     * @return MyClass
     */
    public function setTotalRecords($totalRecords)
    {
        $this->totalRecords = $totalRecords;

        return $this;
    }

    /**
     * Get totalRecords
     *
     * @return integer
     */
    public function getTotalRecords()
    {
        return $this->totalRecords;
    }

    /**
     * Set importStatus
     *
     * @param string $importStatus
     *
     * @return MyClass
     */
    public function setImportStatus($importStatus)
    {
        $this->importStatus = $importStatus;

        return $this;
    }

    /**
     * Get importStatus
     *
     * @return string
     */
    public function getImportStatus()
    {
        return $this->importStatus;
    }

    /**
     * Set datetimeCreated
     *
     * @param \DateTime $datetimeCreated
     *
     * @return MyClass
     */
    public function setDatetimeCreated($datetimeCreated)
    {
        $this->datetimeCreated = $datetimeCreated;

        return $this;
    }

    /**
     * Get datetimeCreated
     *
     * @return \DateTime
     */
    public function getDatetimeCreated()
    {
        return $this->datetimeCreated;
    }

    /**
     * Set datetimeProcessed
     *
     * @param \DateTime $datetimeProcessed
     *
     * @return MyClass
     */
    public function setDatetimeProcessed($datetimeProcessed)
    {
        $this->datetimeProcessed = $datetimeProcessed;

        return $this;
    }

    /**
     * Get datetimeProcessed
     *
     * @return \DateTime
     */
    public function getDatetimeProcessed()
    {
        return $this->datetimeProcessed;
    }
}

Any words of advise is appreciated!.

5
  • What is your version of PHP? Commented Oct 4, 2017 at 18:22
  • My server is running PHP 7.1 Commented Oct 4, 2017 at 20:37
  • Can you show your Entity class and namespace? Commented Oct 5, 2017 at 4:44
  • Here is my entity class: file path: MyProject/src/AppBundle/Entity/MyClass.php ` <?php namespace AppBundle\Entity; /** * MyClass / class MyClass { /* * @var integer */ private $id; ..... and more } ` Commented Oct 5, 2017 at 9:31
  • I wanna see your Entity class all of code Commented Oct 5, 2017 at 9:42

4 Answers 4

0

try this:

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

if you want to access a particular function/method

$feedImport = $this->getDoctrine()->getRepository('AppBundle:MyClass')->sampleMethod();
1
  • Sorry I forgot to mention I already tried the AppBundle:MyClass syntax. Didnt work either. Commented Oct 4, 2017 at 20:40
0

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
1
  • I checked AppKernel.php and my config.yml for those settings.It is all in place like you described. Commented Oct 6, 2017 at 12:04
0

Try this:

/**
 *
 * @ORM\Table(name="my_table_name")
 * @ORM\Entity()
 */
class MyClass
2
  • Then php bin/console doctrine:schema:update --force Commented Oct 5, 2017 at 10:21
  • I Added the doc block the way you described and ran the doctrine:schema:update --force command. The commands was executed OK. But still the same error... :( Commented Oct 6, 2017 at 12:07
0

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?

Not the answer you're looking for? Browse other questions tagged or ask your own question.