2
votes

I am trying to use doctrine to generate entities in symfony 4 from mysql database.

Firstly, I created a bundle called AppBundle.php in a directory called AppBundle

I then generate the xml of the schema using:

php bin/console doctrine:mapping:import --force AppBundle xml

This will create a folder called Resources in the AppBundle folder and then another folder called config and doctrine and all the orm.xml files are put in there.

Then I try to generate the entities using:

php bin/console doctrine:mapping:convert annotation ./src/Entity

This creates the following folders in the Entity folder:

  • Entity
    • App
    • AppBundle
      • Entity

And all the .php entity files are put in the second Entity folder.

enter image description here

The .php entity files have the following namespace

namespace App\AppBundle\Entity;

In my controller I added the name space

use App\AppBundle\Entity\User;

and use doctrine to fetch

$users = $this->getDoctrine()
        ->getRepository(User::class)
        ->findAll();

but when I run the application I get this error

enter image description here

Now, when I move the second Entity folder into the AppBundle folder. The application works.

enter image description here

Is there a way to generate entities without Symfony generating the App folder or the AppBundle folder?

1

1 Answers

0
votes
  1. AppBundle is deprecated way in Symfony 4. Recommended one is put all your code in \App namespace (\App\Entity\User). It's default behavior for all Symfony generators now.

  2. If you want use a bundle you should:

2.1. configure doctrine like orm: mappings: AppBundle: is_bundle: true type: xml prefix: 'AppBundle\Entity' 2.2. use php bin/console doctrine:mapping:convert annotation ./src

Also you should properly configure composer autoload and Symfony configs.