0

I am having a [Doctrine\ORM\ORMException] Unknown Entity namespace alias 'src\AppBundle\Entity' error message.

A quick search led me to three related SO questions :

here about a problem in a user-created bundle, which I'm not using here.

here where the error message is obtained by PHP code rather using doctrine in the command line as I'm currently doing, and

there where the answer suggests doing sudo php app/console cache:clear --env=dev ; I did that followed by sudo chmod a+w app/cache/dev/annotations, but the problem stayed the same.

Here is what I did :

1) Successfully create my database with php app/console doctrine:database:create

2) Create manually a Product Entity in app/Entity/Product.php with the following content (the code below is copy-pasted from the Symfony Book) :

<?php

    namespace AppBundle\Entity;

    use Doctrine\ORM\Mapping as ORM;

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

        /**
         * @ORM\Column(type="string", length=100)
         */
        protected $name;

        /**
         * @ORM\Column(type="decimal", scale=2)
         */
        protected $price;

        /**
         * @ORM\Column(type="text")
         */
        protected $description;
    }

3) Type php app/console doctrine:generate:entities src/AppBundle/Entity:Product - which produced the "unknown entity namespace" error message.

Any help appreciated.

Community
  • 1
  • 1
Ewan Delanoy
  • 1,276
  • 2
  • 13
  • 31
  • 1
    `php app/console doctrine:generate:entities src/AppBundle/Entity:Product` This is invalid. The last argument should be `AppBundle:Product` if I'm not mistaken... – Jovan Perovic Feb 03 '16 at 07:50
  • @JovanPerovic Why ? I don't remember the documentation stating anything like that anywhere – Ewan Delanoy Feb 03 '16 at 07:51
  • @JovanPerovic You are correct though, if the last argument is replaced with `AppBundle:Product` it works – Ewan Delanoy Feb 03 '16 at 07:55
  • There are two syntaxes that are/will be used throughout your `Symfony2` app. One is `\My\Company\Namespace\Entity\Product` and other one is `MyCompanyNamespace:Product`. I believe that putting a `src` anywhere in your code/config would be in violation of `PSR-0`. `Symfony2` does a very good job of seeing everything thought a bundle. That is why you have to have at least one in your app - everything it a bundle. – Jovan Perovic Feb 03 '16 at 08:08
  • @JovanPerovic OK. Now you can put your comments in an answer and I'll accept it, or I can delete this question, which do you think is best – Ewan Delanoy Feb 03 '16 at 08:11
  • I have put up the answer for sake of anyone to stumble upon the same issue... – Jovan Perovic Feb 03 '16 at 10:33

1 Answers1

3

There are two syntaxes that are/will be used throughout your Symfony2 app.

  • \My\Company\Namespace\Entity\Product
  • MyCompanyNamespace:Product

I believe that putting a src anywhere in your code/config would be in violation with PSR-0. Symfony2 does a very good job of seeing everything thought a bundle. That is why you have to have at least one in your app - everything it a bundle

Jovan Perovic
  • 19,846
  • 5
  • 44
  • 85