cakePHP3:在渲染之前更改实体成员名

cakePHP3: Change Entity membername before render

有没有一种优雅的方法可以在将实体成员名传递给视图之前更改它?在我的示例中,我有一个实体产品。我的产品有一个成员application_id。我把它输出到json。现在我的前端开发人员想接收此值作为 applicationId(无下划线)。

因为我要返回一个相当复杂的对象结构,包含其他对象等等,所以我不想遍历整个结果集并在循环中更改成员名称。所以我查看了该实体并希望找到类似 $_map 的东西,我可以在其中将成员名称映射到自定义名称,但我没有找到任何东西。

有没有办法在 beforeRender 中不使用循环来做到这一点?

manual

When converting an entity to an JSON the virtual & hidden field lists are applied. Entities are recursively converted to JSON as well. This means that if you eager loaded entities and their associations CakePHP will correctly handle converting the associated data into the correct format.

所以你可以通过这种方式在你的 Entoty 中创建一个虚拟 属性

protected $_virtual = ['ApplicationId'];

protected function _getapplicationId()
{
    return $this->application_id;
}

似乎有点违反蛋糕命名约定,但它有效