【发布时间】:2019-04-16 15:20:48
【问题描述】:
我有这个实体:
<?php
//namespace
//use ...
class Guide
{
private $id;
//private ...
//getters
//setters
}
?>
在控制器中,我使用实体管理器来检索该实体的数据。
$guides= $em->getRepository('AppBundle:Guide')
->findAll();
我的实体有 4 个参数:id、name、pages、author。 有没有办法添加两个额外的参数,它们不在类声明中,我不想在数据库中,如果实体管理器返回例如 3 行,我想为每行添加两个额外的值并返回数据,例如添加两个布尔值:ok => true,warning => false。
我试过这个:
foreach($guides as $guide){
$guide->ok=true;
$guide->warning=false;
}
如果我转储 $guides,我会看到如下两个参数:
-id:1
-name:'Guide 1'
-pages:12
-author:'John'
+"ok":true
+"warning":false
但是当我用它来发送响应时:
return new Response($serializer->serialize($guides, 'json'));
响应中没有两个额外的参数。
【问题讨论】:
标签: symfony symfony-3.4