使用JMSSerialize序列化遵循SimplifiedYamlDriver约定的Doctrine2实体 [英] Using JMSSerialize to serialize Doctrine2 Entities that follow SimplifiedYamlDriver convention

查看:147
本文介绍了使用JMSSerialize序列化遵循SimplifiedYamlDriver约定的Doctrine2实体的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

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

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

But JMSSerialzer is looking for the YAML files at $myDir . '/MyProject.Entities.User.yml'

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

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

注意:这不是Symfony2项目

推荐答案

您是否使用第二个参数 addMetadataDir

Are you using the second parameter of 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 = '')
{
    // ...
}

看来如果您指定第二个参数,您可以实现您要查找的内容。

It appears that if you specify the second parameter, you can achieve what you're looking for.

这篇关于使用JMSSerialize序列化遵循SimplifiedYamlDriver约定的Doctrine2实体的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆