使用JMSSerialize序列化遵循SimplifiedYamlDriver约定的Doctrine2实体

时间:2013-11-14 19:46:06

标签: orm doctrine-orm doctrine jmsserializerbundle

symfony赞助项目\ Doctrine \ ORM \ Mapping \ Driver \ SimplifiedYamlDriver在我的项目中非常有用,可以保持Entity文件名的简洁。但是,JMSSerialize假定每个实体的命名约定是完全限定的命名空间。在Doctrine2配置中使用\ Doctrine \ ORM \ Mapping \ Driver \ SimplifiedYamlDriver时,情况并非如此。

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);

根据文档:缩短文件名,“MyProject \ Entities \ User”将成为User.orm.yml

但JMSSerialzer正在寻找$ myDir的YAML文件。 '/MyProject.Entities.User.yml'

(见:http://jmsyst.com/libs/serializer/master/configuration#configuring-metadata-locations

问题:有没有办法覆盖JMSSerialize寻找的元数据文件名?我已经在使用addMetadataDir()来指定其位置

注意:这不是Symfony2项目

1 个答案:

答案 0 :(得分:4)

您使用的是addMetadataDir的第二个参数吗?

来自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 = '')
{
    // ...
}

看来如果你指定第二个参数,你可以实现你想要的。