user1041440
user1041440

Reputation: 211

How to generate document with Symfony 3.4 using Symfony flex

With Symfony flex, one of the change is that there is no default bundle when using the symfony/skeleton.

I don't find a way to use the command doctrine:mongodb:generate:documents in this context.

Could you please tell me how to use it ?

Exemple:

php bin\console doctrine:mongodb:generate:documents App

2018-02-17T18:35:22+00:00 [error] Error thrown while running command "doctrine:mongodb:generate:documents AppBundle --document Test". Message: "No bundle AppBundle was found."

In DoctrineODMCommand.php line 87:

  No bundle AppBundle was found.

doctrine:mongodb:generate:documents [--document [DOCUMENT]] [--no-backup] [-h|--help] [-q|--quiet] [-v|vv|vvv|--verbose] [-V|--version] [--ansi] [--no-ansi] [-n|--no-interaction] [-e|--env ENV] [--no-debug] [--] <command> <bundle>

This is how I setup my project

composer create-project symfony/skeleton:3.4 test

composer config "platform.ext-mongo" "1.6.16" && composer require "alcaeus/mongo-php-adapter"

composer require doctrine/mongodb-odm-bundle

Thanks

Regards,

Chris

Upvotes: 0

Views: 1594

Answers (2)

Kamil Zabdyr
Kamil Zabdyr

Reputation: 1

The MongoDBBundle requires you have "Bundle" but with Symfony flex you are using FrameworkBundle and not "AppBundle" unless you created it before, I think you have not created it, so it tells you that it does not exist.

You can not use the "FrameworkBundle" either beacause FrameworkBundle have other namspace and other base path, so the command to generate the documents does not know where the files are.

After a lot of time figuring out how to diry-fix it:

  1. Create custom Bundle inside your project (EX: AppCoreBundle)
  2. Define custom mapping inside packages/doctrine_mongodb.yaml (change type value if you use xml or yaml)

doctrine_mongodb: auto_generate_proxy_classes: '%kernel.debug%' auto_generate_hydrator_classes: '%kernel.debug%' connections: default: server: '%env(MONGODB_URL)%' options: {} default_database: '%env(MONGODB_DB)%' document_managers: default: auto_mapping: true mappings: AppCoreBundle: is_bundle: true type: annotation dir: '/Document' prefix: App\CoreBundle\Document alias: AppCoreBundle

  1. Change the directory "src" to "App" to avoid issues then update psr-4 namaspace inside composer.json: "App\\": "App/"
  2. Move all Documents to the Bundle Document directory and change package names.
  3. You will also have to change ALL references to "src" directory in your project, for example in services.yaml.
  4. Then execute: rm -rf var/cache/* && composer install to force to clear cache and refs.
  5. Then execute: php bin/console doctrine:mongodb:generate:documents AppCoreBundle

Important! This solution solves compatibility problems with MongoODMBundle with Symfony 4 but it is not a correct way. The right fix involves modifying the behavior of the bundle or Symfony 4.

Upvotes: 0

dbrumann
dbrumann

Reputation: 17166

There is a similar issue with doctrine orm and doctrine:generate:entities. The Doctrine team discourages users from using these commands and therefore does no longer want to maintain them.

I think the easiest workaround I've seen so far is:

  1. Create a Symfony 3.3 style application using the Symfony installer.
  2. Generate the entities in the AppBundle as you did before
  3. Change the namespace from AppBundle to App.
  4. Move the files into your Symfony 4 project.

Some of the Symfony team also set out to provide similar code generation in a MakerBundle. As far as I can tell there is nothing for generating ODM-style entities, but you could open an issue or contribute something for this yourself.

For reference see: https://github.com/doctrine/DoctrineBundle/issues/729

Upvotes: 1

Related Questions