0

我使用 Propel 很长时间了,现在我想试试 Doctrine。在 My Propel 时代,我使用PropelObjectCollection::toArray(用于集合)或PropelObject::toArray()单个记录将PropelObjectvia 数组转换为 json。

在我的公司,我们重写 toArray 方法以在数组中存储虚拟列,然后是 json 字符串。例如:

public function toArray() {
  $arr = parent::toArray();
  $arr['full_name'] = $this->getFullName(); // full_name isnt part of the table, it's just a getter
  return $arr;
}

当我把它变成 json 时,我的 json 中有我的 full_name 属性,然后在我的 Extjs 存储对象中(我们使用 extjs)。

现在我想尝试教义,但教义似乎不允许这样做。('first_name', 'last_name', 'full_name')我可以覆盖我的学说类中的函数或属性,还是可以通过注释来做到这一点,如果我的学说类只有属性 $first_name、$last_name 并且没有 $full_name 或者是否有一个属性,是否可以生成带有属性的 json解决这个问题?

谢谢你的帮助

编辑:如果您使用注释,我在 JMSSerializerBundle 中发现了一些东西:

@VirtualProperty

use JMS\Serializer\Annotation\VirtualProperty;

在我的 Doctrine 实体文件和示例方法的顶部

/**
 *
 * @VirtualProperty
 * @return string
 */
public function getFullName() {
    return $this->getName(). " mylastname";
}

然后我的 json 包含虚拟属性 full_name

4

0 回答 0