0

I'm trying to persist a simple object into the database using doctrine.

Instead I'm getting string(83) "The class 'AppBundle\Entity\User' was not found in the chain configured namespaces "

What I've done:

  • Pretty much followed Symfony docs on this.
  • Successfully set up my Symfony Enviorment. Created database using doctrine.
  • Added entity in AppBundle\Entity and generated table using Doctrine
  • Added piece of code to persist my entity to db

User.php

namespace AppBundle\Entity;

use Doctrine\ORM\Mapping as ORM;

/**
 * @ORM\Entity
 * @ORM\Table(name="user")
 */
class User
{
    /**
     * @ORM\Column(type="integer")
     * @ORM\Id
     * @ORM\GeneratedValue(strategy="AUTO")
     */
    private $id;

    /**
     * @ORM\Column(type="string", length=100)
     */
    private $username;
 ... 

DefaultController.php

...
/**
     * @Route("/", name="homepage")
     * @param Request $request
     * @return Response
     */
    public function indexAction(Request $request)
    {
        $manager = $this->getDoctrine()->getManager();

        $user = new User();
        $user->setUsername('jiro');
        $user->setPassword('fantozzi');

        try{
            $manager->persist($user);
            $manager->flush();

        }catch (\Exception $exception){
            var_dump($exception->getMessage());
        }
        return new Response("<h1>It works</h1>");
    }
...

config.yml

doctrine:
    dbal:
        driver: pdo_mysql
        host: '%database_host%'
        port: '%database_port%'
        dbname: '%database_name%'
        user: '%database_user%'
        password: '%database_password%'
        charset: utf8mb4
        default_table_options:
          charset: utf8mb4
          collate: utf8mb4_unicode_ci
    orm:
        auto_generate_proxy_classes: '%kernel.debug%'
        naming_strategy: doctrine.orm.naming_strategy.underscore
        auto_mapping: true

EDIT 0: ###########################################################

I've tried this directly on my local machine and it works fine It seems that I'm having some troubles with my docker environment something not being set up correctly maybe

EDIT 1: ##############################################################

I can confirm that my docker environment is correct. Still it might cause this somehow. Another detail is that my real machine is a Mac with Sierra Os (I don't think it matters but you never know).

After a little digging I found out someone else encountered this as well.

Cannot load entity: "class" was not found in the chain configured

Doctrine cannot map entity/repository namespace in chain

Long story short I changed this line in my app.php

$kernel = new AppKernel('prod', false); => $kernel = new AppKernel('prod', true);

This will enable 'debug mode'. Everything works like a charm now. Obviously the 'solution' feels very dirty and it's only temporally.

If anyone has any clue what is causing this behaviour please do tell.

Jonathan Hall
  • 75,165
  • 16
  • 143
  • 189
vitoriodachef
  • 113
  • 10

2 Answers2

0

Usually errors that say "The class 'your qualified class name' was not found in the chain configured namespaces" means that you have an issue with mismatching namespace and path. Check your namespaces or check the actual directory the class is in and ensure they match.

See Symfony error The class XXX was not found in the chain configured namespaces XXX

Also Symfony2 - The class 'X' was not found in the chain configured namespaces

Hyunmin Kim
  • 931
  • 2
  • 8
  • 18
  • Thank you. I took a look a the links you provided before posting my question. Naming and location is correct as Symfony expects it to be and as it can be seen here https://github.com/victortodoran/MobileApi .This is what makes the exception wierd. – vitoriodachef Nov 30 '17 at 17:17
0

Just add this line to your Default controller and it should work

use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;

but before please make composer update generate entities And Schema Update

Ali Mhanna
  • 199
  • 2
  • 15
  • Thank your for your answer. It does not work. What is the logic behind your answer? How is including a class going the change the above behaviour. From a logical point of view it makes no sense. – vitoriodachef Dec 01 '17 at 11:00
  • i did clone youre project and the only error i got was solved depinding on the solution above – Ali Mhanna Dec 01 '17 at 12:11