I'm using Symfony4 and Doctrine and I'd like to make my app "compatible" with both ODM and ORM.
For that I changed my Documents (my app was originally ODM only) into "generic models" (move from App\Model\Documents
to App\Model\Persistence
) and I try using XML Mapping.
I changed in the doctrine.yaml
and doctrine_mongodb.yaml
config files the mapping option to xml
.
From examples like FosUserBundle and this link (that's not for symfony4 but I didn't find any similar page for symfony4), I made this in my Kernel
/**
* @param \Symfony\Component\DependencyInjection\ContainerBuilder $container
*/
protected function build(ContainerBuilder $container): void
{
$mappingsODM = array(
realpath(__DIR__.'/Model/Mapping/ODM') => 'App\Model\Persistence',
realpath(__DIR__.'/Model/Mapping/ODM/Files') => 'App\Model\Persistence\Files',
);
$mappingsORM = array(
realpath(__DIR__.'/Model/Mapping/ORM') => 'App\Model\Persistence',
realpath(__DIR__.'/Model/Mapping/ORM/Files') => 'App\Model\Persistence\Files',
);
if (class_exists('Doctrine\Bundle\DoctrineBundle\DependencyInjection\Compiler\DoctrineOrmMappingsPass')) {
$container->addCompilerPass(DoctrineOrmMappingsPass::createXmlMappingDriver($mappingsORM, [], 'app.backend_type_orm'));
}
if (class_exists('Doctrine\Bundle\MongoDBBundle\DependencyInjection\Compiler\DoctrineMongoDBMappingsPass')) {
$container->addCompilerPass(DoctrineMongoDBMappingsPass::createXmlMappingDriver($mappingsODM, [], 'app.backend_type_mongodb'));
}
//this CompilerPass is here to add my Repositories (ODM or ORM) in the ServiceContainer depending a param.
//$container->addCompilerPass(new DoctrineRepositoryCompilerPass());
}
With this, I have the following Exception message
No mapping file found named 'Chapter.mongodb.xml' for class 'App\Model\Persistence\Chapter'.
The FQCN is good, I have my mappings file in src/Model/Mapping/ODM
directory (I only set up ODM for now).
Like you saw, in the $mappingsODM
I set my mapping folder => namespace but then, when I load my Repository I have the following dump in the $driver
variable (in MetadataFactory)
"FOS\UserBundle\Model" => XmlDriver {#59 ▼
#locator: SymfonyFileLocator {#58 ▼
#paths: array:1 [▼
0 => "/var/www/html/rapp/vendor/friendsofsymfony/user-bundle/Resources/config/doctrine-mapping"
]
#prefixes: array:1 [▼
"/var/www/html/rapp/vendor/friendsofsymfony/user-bundle/Resources/config/doctrine-mapping" => "FOS\UserBundle\Model"
]
#fileExtension: ".mongodb.xml"
-nsSeparator: "."
}
#classCache: null
#globalBasename: null
}
"App\Model\Persistence" => XmlDriver {#61 ▼
#locator: SymfonyFileLocator {#60 ▼
#paths: array:2 [▼
0 => "/var/www/html/rapp/src/Model/Mapping/ODM"
1 => "/var/www/html/rapp/src/Model/Mapping/ODM/Files"
]
#prefixes: array:2 [▼
"/var/www/html/rapp/src/Model/Mapping/ODM" => "App\Model\Persistence"
"/var/www/html/rapp/src/Model/Mapping/ODM/Files" => "App\Model\Persistence\Files"
]
#fileExtension: ".mongodb.xml"
-nsSeparator: "."
}
#classCache: null
#globalBasename: null
}
I don't really see any differences between the FosUserBundle part and mine (except the folder path, of course, but they are good)
I do regularly php bin/console doctrine:mongodb:cache:clear-metadata
to see if there is a messed up cache but nothing change.
Here is one of my xml mapping file as exemple
<?xml version="1.0" encoding="UTF-8"?>
<doctrine-mongo-mapping xmlns="http://doctrine-project.org/schemas/odm/doctrine-mongo-mapping"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://doctrine-project.org/schemas/odm/doctrine-mongo-mapping
http://doctrine-project.org/schemas/odm/doctrine-mongo-mapping.xsd">
<document name="App\Model\Persistence\Chapter" repository-class="App\Model\ODM\Repository\ChapterRepository">
.... my fields
</document>
</doctrine-mongo-mapping>
Am I missing something ? some Kernel
or CompilerPass
I missed ?
Edit :
In the SymfonyFileLocator:findMappingFile()
method, here is my $this->paths
dump:
array:1 [▼
0 => "/var/www/html/rapp/src/Model/Persistence"
]
It's not my mapping folder, But my ModelClass folder. so the mapping files *.mongodb.xml
are not found.