gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-04 02:40:47 26 4
gpt4 key购买 nike

symfony 赞助的项目\Doctrine\ORM\Mapping\Driver\SimplifiedYamlDriver 在我的项目中非常有用,它可以使实体文件名保持简洁。但是,JMSSerialize 假定每个实体的命名约定是完全限定的 namespace 。在 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 项目

最佳答案

您是否使用了 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 = '')
{
// ...
}

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

关于orm - 使用 JMSSerialize 序列化遵循 SimplifiedYamlDriver 约定的 Doctrine2 实体,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19986922/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com