问题
The symfony sponsored project \Doctrine\ORM\Mapping\Driver\SimplifiedYamlDriver is really useful in my project to keep Entity file names clean and simple. However, JMSSerialize assumes that the naming convention for each Entity is the fully qualified namespace. This is not true when using \Doctrine\ORM\Mapping\Driver\SimplifiedYamlDriver in your Doctrine2 Configuration.
(http://docs.doctrine-project.org/en/latest/reference/yaml-mapping.html)
<?php
$namespaces = array(
'/path/to/files1' => 'MyProject\Entities',
'/path/to/files2' => 'OtherProject\Entities'
);
$driver = new \Doctrine\ORM\Mapping\Driver\SimplifiedYamlDriver($namespaces);
According to the docs: Filenames are shortened, “MyProject\Entities\User” will become User.orm.yml
But JMSSerialzer is looking for the YAML files at $myDir . '/MyProject.Entities.User.yml'
(see: http://jmsyst.com/libs/serializer/master/configuration#configuring-metadata-locations)
Question: Is there a way to override the metadata filename JMSSerialize looks for? I'm already using addMetadataDir() to specify its location
Note: this is not a Symfony2 project
回答1:
Are you using the second parameter of addMetadataDir
?
From JMS\Serializer\SerializerBuilder.php
:
/**
* Adds a directory where the serializer will look for class metadata.
*
* The namespace prefix will make the names of the actual metadata files a bit shorter. For example, let's assume
* that you have a directory where you only store metadata files for the ``MyApplication\Entity`` namespace.
*
* If you use an empty prefix, your metadata files would need to look like:
*
* ``my-dir/MyApplication.Entity.SomeObject.yml``
* ``my-dir/MyApplication.Entity.OtherObject.xml``
*
* If you use ``MyApplication\Entity`` as prefix, your metadata files would need to look like:
*
* ``my-dir/SomeObject.yml``
* ``my-dir/OtherObject.yml``
*
* Please keep in mind that you currently may only have one directory per namespace prefix.
*
* @param string $dir The directory where metadata files are located.
* @param string $namespacePrefix An optional prefix if you only store metadata for specific namespaces in this directory.
*
* @return SerializerBuilder
*
* @throws InvalidArgumentException When a directory does not exist
* @throws InvalidArgumentException When a directory has already been registered
*/
public function addMetadataDir($dir, $namespacePrefix = '')
{
// ...
}
It appears that if you specify the second parameter, you can achieve what you're looking for.
来源:https://stackoverflow.com/questions/19986922/using-jmsserialize-to-serialize-doctrine2-entities-that-follow-simplifiedyamldri