3

symfony 赞助的项目 \Doctrine\ORM\Mapping\Driver\SimplifiedYamlDriver 在我的项目中非常有用,可以保持实体文件名的简洁明了。但是,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 项目

4

1 回答 1

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

看来,如果您指定第二个参数,您可以实现您正在寻找的东西。

于 2013-11-14T19:59:57.473 回答