So we trying to install Behat in some legacy project that is slowly moving to Symfony2.
For a test environment we intend to use in memory sqlite database and we try to reset doctrine using this example.
But unfortunettly when trying to run a test we get an error:
[Doctrine\Common\Persistence\Mapping\MappingException]
Exception has been thrown in "beforeScenario" hook, defined in Company\Features\Context\FeatureContext::prepareDbSchema()The class 'Entities\ChanceCreationRate\LevelScorelineRateCalculator' was not found in the chain configured namespaces Company\ModelBundle\Entity
After some debugging we noticed that doctrine loads metadata for this entity successfully but later it tries to load it again and we get this error then.
We have 2 entity manager and do a manual mapping in config.yml
entity_managers:
default:
connection: default
mappings:
legacy:
type: yml
prefix: Entities
dir: %kernel.root_dir%/../entities/metadata
is_bundle: false
CompanyGameBundle: ~
CompanyModelBundle: ~ # Temporary until db split
CompanyShotStatsBundle: ~
CompanySiteBundle: ~
CompanyTeamStrengthBundle: ~
CompanyTrainingBundle: ~
CompanyUserBundle: ~
CompanyWatcherBundle: ~
CompanyWorldBundle: ~
CompanyWorldCupBundle: ~
model:
connection: model
mappings:
CompanyModelBundle: ~
I have the feeling the ModelBundle being in both em maybe in fault but it has to be this way ATM or everything will brake.
The correct namespace for this entity is "Entities" and belongs to "legacy" mapping.
Any suggestion, tips on how to fix it? What is wrong?
EDIT:
So I found and reason and feel a bit stupid now... One of the entities in ModelBundle has a relationships with few entities from legacy code (lucky they don't have any further relationships), and doctrine tries to load them when going thru model EM mappings. It is that way as we are in the middle of refactoring :/
So the question now is: Can i specify mapping for explicit files in mapping instead of directory or I will not be able to proceed before we finally move all entities and refactor code related with ModelBundle?
As you've figured out, all entities associated with each other must have mapping definitions in the same EntityManager. This is because one EntityManager doesn't know of any other EntityManagers, the don't (and can't) talk to each other. And one EntityManager needs to know the mapping definitions of all entities it manages. This by design.
So I suggest you proceed the way you're doing now. Have the CompanyModelBundle
in both EntityManagers until you completely moved all associated entities out of the legacy environment.
If you're doing this because you're going to move to a new database, and both databases are supplied by the same service (a single MySQL instance on a single host containing both databases/schema's for example), there is an alternative:
Make sure both databases are accessible by the same user (so you can use a single Doctrine Connection) for both databases.
Map your entities so they specify which database the table is in. Using Annotations this would look like:
/**
* @ORM\Entity
* @ORM\Table(name="some_database.some_table")
*/
class SomeEntity
Now you can safely associate one entity in one database with another entity in another database.
This way you can (and must for this to work) use a single EntityManager for both databases, and move one entity at the time to your new database.