How to generate document with Symfony 3.4 using Symfony flex

老子叫甜甜 提交于 2019-12-24 19:46:38

问题


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


回答1:


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




回答2:


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.



来源:https://stackoverflow.com/questions/48844807/how-to-generate-document-with-symfony-3-4-using-symfony-flex

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!