Symfony2 - Change Migration Directory - symfony

How can I change the default migration dirctory in the config.yml?
Right now I am using 2 bundles with different db-connections and I would like to create migrations files and store them in different directories to use the doctrine:migrations:migrate --em=whatever function in depency of the bundle.
For example:
doctrine:migrate:diff --em=whatever #creating a version file in the DoctrineMigrationsWhatever directory
php app/console doctrine:migrations:status --em=whatever # shows only the version files, that belong to the bundle

If you'll create separate entity manager for this second connection/bundle you will get another directory in your DoctrineMigrations dir. For example:
app/
DoctrineMigrations/
entityManager1/
entityManager2/
If you want put all migrations to another directory, you can set it inside your config.yml:
doctrine_migrations:
dir_name: '%kernel.root_dir%/../Acme/CommonBundle/DoctrineMigrations'
namespace: 'Acme\CommonBundle\DoctrineMigrations'
If you want some more complex thing like put migrations from em1 to dir1 inside bundle1 and put migrations from em2 to dir2 inside bundle2 you will need an additional two configuration files where you'll specify dirs for particular bundles:
http://docs.doctrine-project.org/projects/doctrine-migrations/en/latest/reference/introduction.html#configuration
And then you run your migrations like this:
doctrine:migrations:status --em=em1 --configuration=./path/to/bundle1/Resources/config/migrations.yml
doctrine:migrations:status --em=em2 --configuration=./path/to/bundle2/Resources/config/migrations.yml
By https://github.com/doctrine/DoctrineMigrationsBundle/pull/46
the migrations.yml file should look like:
name: Doctrine Postgres Migrations
migrations_namespace: Application\Migrations
table_name: migration_versions
migrations_directory: PostgreSqlMigrations

For other people who found this page and spent hours trying to implement Cyprian's solution, it doesn't work.
First, --configuration gets clobbered, and second, the doctrine migrations bundle doesn't support multiple entity managers.
See https://github.com/doctrine/DoctrineMigrationsBundle/issues/18 for information about --configuration and see https://github.com/doctrine/DoctrineMigrationsBundle/pull/46 for an open pull request to support multiple entity managers.
If and when PR #46 goes through, this will be a trivial configuration:
doctrine_migrations:
default:
dir_name: ...
namespace: ...
em2:
dir_name: ...
namespace: ...
The only tweaks available right now, are:
doctrine_migrations:
dir_name: '%kernel.root_dir%/../Acme/CommonBundle/DoctrineMigrations'
namespace: 'Acme\CommonBundle\DoctrineMigrations'
But that will update the configuration for all migrations across all entity managers.

For Symfony 4, the recommended approach is to use %kernel.project_dir% instead, and put it in the src/ folder namespaced by App\:
doctrine_migrations:
dir_name: '%kernel.project_dir%/src/DoctrineMigrations'
namespace: 'App\DoctrineMigrations'

Related

generate initial migration after creating entity and repository for an existing database

After creating an Entity and Repository from an existing pre-doctrine database, I am unable to make an initial migration. It gave me this error [ERROR] The version "latest" couldn't be reached, there are no registered migrations. Any idea how to do an initial migration without starting fresh? And for some reason, the migration folder exists outside the src folder, why is it so? In a previous project, the migration folder exists inside the src folder.
Any insight would be appreciated. Thank you for reading.
EDIT: doctrine_migrations.yaml:
doctrine_migrations:
migrations_paths:
# namespace is arbitrary but should be different from App\Migrations
# as migrations classes should NOT be autoloaded
'DoctrineMigrations': '%kernel.project_dir%/migrations'
The commands I used to generate the Entity and its Repository is as follows:
php bin/console doctrine:mapping:import "App\Entity" annotation --path=src/Entity
modified the #ORM\Entity => #ORM\Entity(repositoryClass="App\Repository\UserRepository") in the entity .php
php bin/console make:entity --regenerate
Then when I run bin/console doctrine:migrations:migrate, the error pops up.
Work for me
doctrine_migrations:
migrations_paths:
'App\Migrations': '%kernel.project_dir%/src/Migrations'
and use for migration classes
namespace App\Migrations;
THis is my migrations config.You can test it :
doctrine_migrations:
dir_name: '%kernel.project_dir%/src/Migrations'
# namespace is arbitrary but should be different from App\Migrations
# as migrations classes should NOT be autoloaded
namespace: DoctrineMigrations
For migration commande i use : php bin/console d:m:diff and after this you can use the migration number with this commande :
php bin/console d:m:e --up the_migration_number
I tried most of the methods but it seems that it is possible to generate the migration. However, changes to the entity will not be detected by doctrine.
For example, if I change the field of name to username, php bin/console doctrine:migration:diff does not detect the changes.
What I found worked was exporting the database as .sql, creating the entity the normal way, and manually typing in the fields. Delete the generated table in phpmyadmin, and importing the data back in. Only then would it be working as I want it to be.

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
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:
Create a Symfony 3.3 style application using the Symfony installer.
Generate the entities in the AppBundle as you did before
Change the namespace from AppBundle to App.
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
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:
Create custom Bundle inside your project (EX: AppCoreBundle)
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
Change the directory "src" to "App" to avoid issues then update psr-4 namaspace inside composer.json: "App\\": "App/"
Move all Documents to the Bundle Document directory and change package names.
You will also have to change ALL references to "src" directory in your project, for example in services.yaml.
Then execute: rm -rf var/cache/* && composer install to force to clear cache and refs.
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.

ORM convert-mapping is creating the wrong file structure

I currently got orm set up on my silex application. Everything seems to work as expected except for when I run the command to generate the entities from my database (reverse-engineering).
../../../vendor/bin/doctrine orm:convert-mapping --namespace="Random\MyApp\Model\Random\Entities" --force --from-database annotation ./../random/entities
This command will create all of my entities perfectly but they will be under the folder structure
./../random/entities/Random/MyApp/Model/Random/Entities/(files here)
which is wrong since I am expecting to have
./../random/entities/(files here)
A namespace is actually the path under your entities are grouped. So it's perfectly logic that doctrine generates files under this namespace.
If you want a file structure like it just do :
../../../vendor/bin/doctrine orm:convert-mapping --namespace="random/entities" --force --from-database annotation ./
But this is definitely not conform with the PSR-0

How to use a different console configuration in Symfony

I followed the guideline on how to expose a semantic configuration for a bundle and configured it in my app/config.yml (through parameters.yml).
My bundle also contains some console commands. Right now this command either uses the dev or prod configuration, which is fine.
But how can I make the console commands use an additional configuration file that sets some things different than in config.yml?
E.g.
#app/config.yml
imports:
- { resource: parameters.yml }
foo:
view_mode: %view_mode%
and
#app/parameters.yml
parameters:
view_mode: 1
How can I make it e.g. use a different parameters.yml
#app/parameters_console.yml
parameters:
view_mode: 2
when called through the console? A new environment is not what I want here.
I think you need to create a custom environement
You just have to create a config_console.yml in your app/config folder and override the configuration you need.
imports:
- { resource: config_dev.yml }
foo:
view_mode: 2
Then in your application, just run
php app/console --env=console
This will run your application with default configuration of dev and with foo.view_mode = 2
You may want to note that it will create a new cache folder named console

Nothing to update "Doctrine"

I'm using Symfony2 with Doctrine to try and update a table schema. I was able to create the table. I was also able to populate the table. However after updating the comments in the Entity (I wanted some fields to become nullable), those changes did NOT get picked up.
I did create the entity with the "Annotations" option chosen. But when I added this line "nullable=true" to the Entity on the field imageName nothing happens. ie: when I run "./app/console doctrine:schema:update" I get the following output "Nothing to update - your database is already in sync with the current entity metadata."
Note, I have tried deleted the table via: ./app/console doctrine:database:drop --force and then recreating it via: ./app/console doctrine:database:create and then also ./app/console doctrine:schema:create but it STILL does not add my updated nullable field to imageName.
I was able to figure this out. I first of all created my entity "Foobar" using yml as the Configuration format. I then wanted to use "annotation" as the configuration format so I manually deleted the Entity folder (I only had one table created), however I did NOT delete the configuration yml in the Resources/config/doctrine/Foobar.orm.yml.
Thus when I created the entity again, this time using the annotation as the configuration format, it was still linking to the yml configuration. Removing that solved all the troubles.
I have however decided to stick to yml as I feel it is a little easier to read than the Doctrine Metadata found in the comments.
I had been stuck with this for almost 2 days. Removing all the file in /src/AppBundle/Resources/config/doctrine resolve my issue.
For me, the key was to clear Redis cache.
php app/console redis:flushdb
I has this problem too. Have you right annotation before class declaration?
/**
*
* #ORM\Entity <- this does the trick
*/
class MyEntityName
{
...
Check doctrine.yaml config file for orm mappings:
mappings:
App:
is_bundle: false
type: annotation
dir: '%kernel.project_dir%/src/Entity'
prefix: 'App\Entity'
alias: App
As you see here all entities should have prefix (namespace) App\Entity
You have to check your entities namespace, it should be App\Entity or whatever you want in config

Resources