Symfony Flex - Doctrine generate entity and entities runtime exception

1.8k views Asked by At

I am testing the new Symfony 4 schema with Flex and I'm having a big trouble while creating a Doctrine Entity or creating the Entity Entities.

In previous versions of Symfony, you can create the entities with the command:

php bin/console doctrine:generate:entities

and

php bin/console doctrine:generate:entities App:MyBundle:MyEntity

In this version (Symfony 3.3-dev with the new Flex skeleton) the new folder structure "removes" the old Bundle structure, and when executing the above commands to create an Entity in ./src/Entity it returns and error like this:

[RuntimeException] Can't find base path for "App\Entity\ExampleEntity" (path: "/mnt/c/.../src/Entity", destination: "/mnt/c/.../src/Entity").

The question is, that is any way to generate an Entity, or the Entity Entities focusing to a path and not with the PSR-4 autoload directive.

Thanks in advance!!!

2

There are 2 answers

4
Doug On

Not a fix by any means but a workaround till they catch up. Very much a cowboy method.

  1. Setup a new project with the standard Symfony file structure.
  2. Copy your basic objects into the entity folder.
  3. Generate the entitles there php bin/console doctrine:generate:entities AppBundle
  4. Copy and paste them into your new Symfony Flex project entity folder.
  5. Using an IDE do a find and replace in the folder for AppBundle with App

Sorry, it's far from ideal, but the easiest method I've found rather than having to generate all the getter/setters by hand.


UPDATE: 25/01/2017

As of 4.x the whole idea of doctrine:generate:entities has been dropped (In this issue after a long read you'll notice that Doctrine has dropped the commands completely, hence the new Symfony maker bundle).

Apparently, it's viewed as bad practice. Instead, they've released a new package called maker, which essentially creates the entity class for you but without the getters/setters, and the new suggested practice is to rely on your IDE to auto-generate the Getters and Setters for you.

This works except for the constructor for ArrayCollections for @OneToMany, but it doesn't take much to add these by hand.

What I've said above is also reflected in the Symfony documentation.

Generating Getters and Setters

Doctrine now knows how to persist a Product object to the database. But the class itself isn't useful yet. All of the properties are private, so there's no way to set data on them!

For that reason, you should create public getters and setters for all the fields you need to modify from outside of the class. If you use an IDE like PhpStorm, it can generate these for you. In PhpStorm, put your cursor anywhere in the class, then go to the Code -> Generate menu and select "Getters and Setters":

1
Rémi Leclerc On

I had the same problem. Here an other solution.

Temporarily, change Doctrine\Bundle\DoctrineBundle\Mapping:getBasePathForClass

After

$namespace = str_replace('\\', '/', $namespace);

add:

$namespace = str_replace('App/', '', $namespace);

Entities will be created in src/App/Entity/. After the copy-paste, remove src/App.