5

I just started my first project with composer and wanted to set up the database and the classes for it. However I'm stuck. I'm getting the above error in the prod.log

I followed this tutorial here: http://symfony.com/doc/current/book/doctrine.html

I created the database

php bin/console doctrine:database:create

then wanted to create an entity

php bin/console doctrine:generate:entity

When asked for the The Entity shortcut name I entered AppBundle:Product

and then created the database fields etc.

And I'm getting this message

Entity generation

Generating entity class src/AppBundle/Entity/Product.php: OK!
Generating repository class src/AppBundle/Repository/ProductRepository.php: OK!

Everything is OK! Now get to work :).

So this sounds like everything worked, right?

Now in my ProductController I used this

$products = $this->getDoctrine()
    ->getRepository('AppBundle:Product')
    ->findAll();

and I'm getting the error

[2016-02-15 18:56:14] request.CRITICAL: Uncaught PHP Exception Doctrine\ORM\ORMException: "Unknown Entity namespace alias 'AppBundle'." at /home/vagrant/work/homestead/test/vendor/doctrine/orm/lib/Doctrine/ORM/ORMException.php line 271 {"exception":"[object] (Doctrine\\ORM\\ORMException(code: 0): Unknown Entity namespace alias 'AppBundle'. at /home/vagrant/work/homestead/test/vendor/doctrine/orm/lib/Doctrine/ORM/ORMException.php:271)"} []

I also tried

->getRepository('AppBundle\Entity\Product')

but here I'm getting the message

[2016-02-15 19:01:39] request.CRITICAL: Uncaught PHP Exception Doctrine\Common\Persistence\Mapping\MappingException: "The class 'AppBundle\Entity\Product' was not found in the chain configured namespaces " at /home/vagrant/work/homestead/test/vendor/doctrine/common/lib/Doctrine/Common/Persistence/Mapping/MappingException.php line 37 {"exception":"[object] (Doctrine\\Common\\Persistence\\Mapping\\MappingException(code: 0): The class 'AppBundle\\Entity\\Product' was not found in the chain configured namespaces  at /home/vagrant/work/homestead/test/vendor/doctrine/common/lib/Doctrine/Common/Persistence/Mapping/MappingException.php:37)"} []

How do I actually get this to work/ I just started with the project. Doctrine created the classes though. I have it like this

/src
/src/AppBundle/
/src/AppBundle/Entity
    Product.php
/src/AppBundle/Repository
    ProductRepository.php

Also, in the Product.php this is in the comments of the annotations (if it helps)

 * @ORM\Entity(repositoryClass="AppBundle\Repository\ProductRepository")

Also, when trying this command php bin/console doctrine:generate:entities AppBundle to recreate the entities I'm getting no error message.

I'm getting

Generating entities for bundle "AppBundle"
 > backing up Product.php to Product.php~
 > generating AppBundle\Entity\Product
12
  • I'm guessing you may have files under AppBundle/Resources/config/doctrine? If so then they are interfering with your annotations. And did you make any changes to the doctrine section in app/config/config.yml? Commented Feb 15, 2016 at 19:10
  • I don't have a folder AppBundle/Resources. I always selected annotations when creating the entities, so there shouldn't be any configs, right? No, I made no changes in the app/config/config.yml Commented Feb 15, 2016 at 19:14
  • After generating you need to php bin/console doctrine:schema:update --force. Than check your namespace in Controller must be AppBundle\Entity\Product. Commented Feb 15, 2016 at 19:15
  • 1
    yes, and then try again, also delete the dev folder if exist. Commented Feb 15, 2016 at 19:26
  • 1
    Only if you have been working in debug mode, in production you need to do manually. Also the most secure way is by hand :-) Commented Feb 15, 2016 at 19:30

1 Answer 1

5

The solution is delete the cache files, so it is needed if you are working in production mode. In debug mode this is done automatically. For be sure, do that manually just dropping the prod folder under var if you are in 3.x, in 2.x under app.

The command to clear the cache for 2.x is:

php app/console cache:clear --env=prod
Sign up to request clarification or add additional context in comments.

2 Comments

But also check that all the namespaces are correct; you can use/import the class and in the Doctrine annotation specify class=Product::class (without quotes!). I consider this the safest, when using mapping via annotations.
Also, you can just clear the specific Doctrine cache with php bin/console doctrine:cache:clear-metadata

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.