【问题标题】:JMS serializer ignores doctrine entity idJMS 序列化程序忽略原则实体 ID
【发布时间】:2017-04-19 14:24:11
【问题描述】:

我尝试使用 jms 序列化程序(在单元测试中)将给定数组中的数据映射到对象以测试学说实体:

Given 是一个简单的实体类:

/**
 * CashPosition
 */
class CashPosition
{
    /**
     * @var integer
     */
    protected $cashPositionId;

    /**
     * @var \DateTime
     */
    protected $date;

    /**
     * @var float
     */
    protected $value;


    /**
     * Get cashPositionId
     *
     * @return integer
     */
    public function getCashPositionId()
    {
        return $this->cashPositionId;
    }

    /**
     * Set date
     *
     * @param \DateTime $date
     *
     * @return $this
     */
    public function setDate($date)
    {
        $this->date = $date;

        return $this;
    }

    /**
     * Get date
     *
     * @return \DateTime
     */
    public function getDate()
    {
        return $this->date;
    }

    /**
     * Set value
     *
     * @param string $value
     *
     * @return $this
     */
    public function setValue($value)
    {
        $this->value = $value;

        return $this;
    }

    /**
     * Get value
     *
     * @return float
     */
    public function getValue()
    {
        return $this->value;
    }
}

我在 Resources\config\serializer\Entity.CashPosition.yml 下定义了序列化

MyBundle\Entity\CashPosition:
  exclusion_policy: ALL
  access_type: public_method
  properties:
    cashPositionId:
      exclude: false
      expose: true
      type: integer
      access_type: property
    date:
      exclude: false
      expose: true
      type: DateTime<'Y-m-d'>
    value:
      exclude: false
      expose: true
      type: float

并试图通过序列化测试来解决这个问题:

public function testSerialization()
{
    $data = [
        'cashPositionId' => 1,
        'date'           => date('Y-m-d'),
        'value'          => 1.0,
    ];

    /* @var $serializer Serializer */
    $serializer = $this->container->get('serializer');

    $cashPosition = $serializer->fromArray($data, CashPosition::class);
    $this->assertInstanceOf(CashPosition::class, $cashPosition);
    $this->assertEquals($data, $serializer->toArray($cashPosition));
}

但测试失败,因为 fromArray 方法没有设置 cashPositionId。我尝试了一些不同的访问类型配置,但没有运气。我不确定这里有什么问题。

我正在使用以下版本的 jms 序列化程序:

jms/metadata                            1.6.0   Class/method/property metadata management in PHP
jms/parser-lib                          1.0.0   A library for easily creating recursive-descent parsers.
jms/serializer                          1.6.2   Library for (de-)serializing data of any complexity; supports XML, JSON, and YAML.
jms/serializer-bundle                   1.4.0   Allows you to easily serialize, and deserialize data of any complexity

【问题讨论】:

    标签: php symfony serialization jmsserializerbundle


    【解决方案1】:

    您好,我想您错过了 cashPositionId 的 serialized_name 属性,默认情况下 jms 会将属性从驼峰式转换为蛇形。

    JMS Doc

    【讨论】:

      猜你喜欢
      • 2015-03-16
      • 1970-01-01
      • 2013-04-06
      • 1970-01-01
      • 1970-01-01
      • 2021-03-11
      • 1970-01-01
      • 1970-01-01
      • 2016-05-17
      相关资源
      最近更新 更多