Using JMSSerialize to serialize Doctrine2 objects that follow the SimplifiedYamlDriver convention

The Symfony \ Doctrine \ ORM \ Mapping \ Driver \ SimplifiedYamlDriver project 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 a complete 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 docs: File names are abbreviated, "MyProject \ Entities \ User" will become User.orm.yml

But JMSSerialzer looks for YAML files in $ 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 file name that JMSSerialize is looking for? I already use addMetadataDir () to indicate its location

Note: this is not a symfony2 project

+3
source share
1 answer

Are you using the second parameter 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 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 seems that if you specify the second parameter, you can achieve what you are looking for.

+4
source

Source: https://habr.com/ru/post/981193/


All Articles