I'm trying to make migrations based on a diff of my entities.
I'm expecting for the initial run (and following runs) that doctrine generates a migration with the diff.
I've tried the following solution but this results in "No changes detected in your mapping information."
I've poked and went thru the doctrine migrations source code to see if I can extend/alter the behavior to no results.
I also found related issues in doctine dbal like this one and this one.
Is doctrine migrations still the way or is there another lib which is better suited for this problem?
I'm running PHP 7.4.4, Symfony 5.0.6, Doctrine Migrations 2.2.6 (latest stable), Postgres 12.1.
Doctrine config:
doctrine:
dbal:
driver: 'pdo_pgsql'
server_version: '12.1'
charset: utf8
url: '%env(resolve:DATABASE_URL)%'
dbname: storage001
default_dbname: storage001
Entity example:
/**
* #ORM\Table(schema="storage001", name="entityName")
*/
class EntityClass1
{
...
}
I have generated an entity named 'MyEntity' with annotation type mapping information using the doctrine:mapping:convert command with the --from-database option.
The entity is in a non-satandard folder which is defined in the doctrine ORM configuration as:
doctrine:
orm:
entity_managers:
default:
MyEntity:
mapping: true
type: annotation
dir: '%kernel.root_dir%/../src/Path/To/Entity'
prefix: 'Path\To\Entity'
is_bundle: false
The class appears in the appropriate directory and has all of the correct properties and annotations however when I try to use the doctrine:migrations:diff command the outcome is a migration that drops the table that the entity was generated from in the first place. This seems to imply that the mapping information generated by the doctrine:mapping:convert command is not being picked up by the doctrine:migrations:diff command. Any insight on this issue would br greatly appreciated.
After returning to the problem I noticed that the generated entity class was in the global namespace as opposed to the one specified by the config file, correcting this immediately fixed the problem.
This is my orm config for my Doctrine in Symfony2
orm:
default_entity_manager: default
auto_generate_proxy_classes: false
entity_managers:
default:
connection: default
auto_mapping: false
mappings:
MyMappings:
type: yml
is_bundle: false
dir: %kernel.root_dir%/../src/Bundle/Entity/orm
prefix: Bundle\Entity
My directory structure is set up like this:
src
-- Bundle
--- Entity
---- orm
----- Bundle.Entity.(TableName).orm.yml
My question is why when I run:
php app/console doctrine:generate:entities MyMappings
I would get the following exception:
php app/console doctrine:generate:entities MyMappings
[Doctrine\Common\Persistence\Mapping\MappingException]
Invalid mapping file 'Bundle.Entity.(TableName).orm.yml' for class 'Bundle\Entity\(TableName)'
the orm files were named (TableName).orm.yml
And the yml:
TableName:
type: entity
When I used the import from database I added the namespace:
php app/console doctrine:mapping:convert yml ./src/Bundle/Entity/orm --force --from-database --namespace="Bundle\\Entity\\"
which generated the Bundle.Entity.(TableName).orm.yml files.
And the yml:
Bundle\Entity\TableName:
type: entity
Now, I keep getting
[Doctrine\Common\Persistence\Mapping\MappingException]
Invalid mapping file 'Bundle.Entity.Bundle.Entity.(TableName).orm.yml' for class 'Bundle\Entity\Bundle\Entity\(TableName)'
I feel like I am missing something obvious as to how to properly generate these entities in the correct namespace. If I am not clear please let me know and I will try to rephrase this issue.
More Information:
I am trying to generate these entities in a non-bundle namespace
After creating entity with:
php app/console doctrine:generate:entity
and while using:
php app/console doctrine:schema:update --force
I encountered:
No Metadata Classes to process.
Entity
namespace ISLab\AdminBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
/**
* #ORM\Entity
* #ORM\Table(name="menu_items")
*/
class MenuItem
{
/**
* #ORM\Column(name="id", type="integer")
* #ORM\Id
* #ORM\GeneratedValue(strategy="AUTO")
*/
private $id;
/**
* #ORM\Column(name="parent", type="integer")
*/
private $parent;
// ...
}
I had a similar problem and took me a while until I find out how to clear out the cache. Clearing only doctrine's cache did not work for me. Use the command below to clear the entire environment (production one in this case)
php app/console cache:clear --env=prod
I had the same problem when I generate an Entity through the interactive generator from command line. When I first created I set the Configuration Format to PHP and that didn't work. So I deleted it and tried again but this time I selected format = annotation. Then it worked! Maybe this could solve someone's problem.
So entity manager didn't create metadata for your entity. Here are things I would check first:
Clear your cache
Make sure your AdminBundle is registered in AppKernel.
Make sure that entity manager's automapping is true. Or set it up correctly if you're using custom EM.
A bit offtopic:
I see you're making a menu system and I would strongly suggest you to check out Doctrine Extensions Tree which should allow you to query and edit items much more efficiently than with your structure.
I had the same problem when i update the database.
So i follow this solution and i tried to adapt in my case and it works.
I think that is a problem of automapping indeed also with auto_mapping setted true, symfony wasn't able to find the entities.
I hope that it can help.
orm:
auto_generate_proxy_classes: "%kernel.debug%"
naming_strategy: doctrine.orm.naming_strategy.underscore
auto_mapping: true
mappings:
AppBundle:
type: annotation
is_bundle: false
dir: %kernel.root_dir%/../src/AppBundle/Entity/
prefix: AppBundle\Entity
alias: AppBundle
for anyone on the same thing, these are few steps to go:
1. Clear the cache:
bin/console c:c --env=dev
bin/console c:c --env=prod
If you see any errors meanwhile, try to cover it by inspecting source of problem.
2. Inspect your configuration:
According to this sample directory structure:
Application
|
└- src
|
└- Acme
|
└- Bundle
|
└- BlogBundle
|
└- Model
|
└- Entity
You should have this configuration inside your app/config/config.yml file:
doctrine:
...
orm:
auto_generate_proxy_classes: '%kernel.debug%'
naming_strategy: doctrine.orm.naming_strategy.underscore
auto_mapping: true
mappings:
AcmeBlogBundle:
mapping: true
type: annotation
dir: Model/Entity
alias: Blog
prefix: Acme\Bundle\BlogBundle\Model\Entity
is_bundle: true
3. Update your database schema:
You can now update your database schema without any problems:
bin/console doctrine:schema:update
despite of this being the final step for most cases, you may still use --verbose to find any further possible problems.
bin/console doctrine:schema:update --verbose
Make sure your entities are placed in your bundle's Entity directory. In my case they were in Entities instead.
For me generating all the entities from the cmd did the work.
In my case, i solve the problem creating the Entity folder. For some reason it had been eliminated. I hope that it can help.
I've had the same issue. For me it was that my directory called 'Entity' was not in the 'AppBundle' but in the 'src' directory. Moving the 'Entity' directory into the 'AppBundle' directory fixed the problem for me!
Ok I have one last problem with doctrine:generate:entities command
I run the below command and I get the expected file(s) in
/src/MyNamespace/Bundle/MyNamespaceBundle/Resources/config/doctrine/metadata/orm
comamnd:
php app/console doctrine:mapping:convert yml ./src/MyNamespace/Bundle/MyNamespaceBundle/Resources/config/doctrine/metadata/orm --from-database --em=my_manager --filter=TblReports --verbose
I see the TblReports.orm.yml file(s) and the first line is:
TblReports
command: ( should this be annotation instead of yml? )
php app/console doctrine:mapping:import MyNamespaceBundle yml --em=my_manager --filter=TblReports
I run the above command I get the files here
/src/MyNamespace/Bundle/MyNamespaceBundle/Resources/config/doctrine/
Same name as the first files that were generated from the first command, just in a different location and the first line ( Which I'm assuming is the namespace )
TblReports.orm.yml
and now the first line is:
MyNamespace\Bundle\MyNamespaceBundle\Entity\TblReports
but I think it needs to be
MyNamespace\Bundle\MyNamespaceBundle\Entity\Reports\TblReports
Now I run the last command
php app/console doctrine:generate:entities MyNamespaceBundle --path=src --no-backup
I get this error
[RuntimeException]
Bundle "MyNamespaceBundle" does not contain any mapped entities.
If I run the command like this
php app/console doctrine:generate:entities MyNamespaceBundle:Reports --path=src --no-backup
I get this error ( but the namespace looks correct )
[RuntimeException]
Namespace "MyNamespace\Bundle\MyNamespaceBundle\Entity\Reports" does not contain any mapped entities.
Here is my_manager ( config.yml )
# Doctrine Configuration
doctrine:
dbal:
default_connection: my_database
connections:
my_database:
driver: pdo_pgsql
port: 5432
dbname: tbl_reports
user: foo_user
password: foo_pass
charset: UTF8
mapping_types:
bit: string
orm:
auto_generate_proxy_classes: "%kernel.debug%"
default_entity_manager: my_manager
entity_managers:
my_manager:
connection: my_database
mappings:
MyNamespaceBundle:
mapping: true
dir: Entity/Reports
in config_dev.yml ( I use the dev and prod yml files to control the host(s) I can connect to )
# Doctrine Configuration
doctrine:
dbal:
connections:
my_database:
host: 172.0.0.1
Questions:
Why am I getting this error?
How can I fix it?
Related Questions:
Generate Entities with Doctrine into separate namespace
Entity Generation for Single Table
UPDATE #1:
Ok well I ran the second command as annotation instead of yml and the files were generated in:
MyNamespace\Bundle\MyNamespaceBundle\Entity
command:
php app/console doctrine:mapping:import MyNamespaceBundle annotation --em=my_manager --filter=TblReports
I ran the doctrine:generate:entities ( both ways ) and still got errors. I decided to move the files into this directory
MyNamespace\Bundle\MyNamespaceBundle\Entity\Reports
I ran the doctrine:generate:entities agin ( both ways ) and still got errors.
I looked at the namespace in the files and saw it was pointing to the work namespace. I updated from:
MyNamespace\Bundle\MyNamespaceBundle\Entity\TblReports
to
MyNamespace\Bundle\MyNamespaceBundle\Entity\Reports\TblReports
ran this command
php app/console doctrine:generate:entities MyNamespaceBundle:Reports --path=src --no-backup
and in now works
Generating entities for namespace "MyNamespace\Bundle\MyNamespaceBundle\Entity\Reports"
So I guess question #3 is:
How can I get the second command to add the correct namespace on the import?
I tried this but no dice
php app/console doctrine:mapping:import MyNamespaceBundle:Reports annotation --em=my_manager --filter=TblReports
Docs:
http://symfony.com/doc/master/reference/configuration/doctrine.html
Source:
https://github.com/doctrine/DoctrineBundle/blob/master/Command/ImportMappingDoctrineCommand.php
https://github.com/doctrine/DoctrineBundle/blob/master/Command/GenerateEntitiesDoctrineCommand.php
Your first line needs to end with a : as in:
MyNamespace\Bundle\MyNamespaceBundle\Entity\TblReports:
<yml> data.
You seem to be mixing annotations with yml. Pick one and stick with it. I don't use annotations, so the rest is yml comments. doctrine:mapping:import/convert with yml option generates yml files puts the yml files in Bundle/Resources/config/doctrine.
The collection of yml files are then used to make your entity classes, and it will put them in the location specified in each yml file, which be default is NamespaceBundle\Entity.