[英]Symfony2 json response
我在使用以下代碼時遇到問題
/** @Route("/{collection}/{id}", name="object", defaults={"_format" = "json"}) */
public function retrieveOne($collection, $id)
{
$collection = Inflector::classify($collection);
$object = $this->get('doctrine_mongodb.odm.document_manager')
->getRepository('NameBundle:' . $collection)
->find($id);
$response = new Response(json_encode($object));
return $response;
}
文獻
use Doctrine\ODM\MongoDB\Mapping\Annotations as Mongo;
/**
* Entrada
*
* @Mongo\Document
*/
class Entrada
{
/**
* @Mongo\Id
*/
protected $id;
/**
* @Mongo\String
* @var int $type
*/
private $type;
/**
* @Mongo\String
* @var string $nombre
*/
private $nombre;
//.. setters and getters
}
我總是在響應體中得到{}
。
注意 :查詢正確返回一個對象
json_encode
只會編碼公共屬性。
所以你可以:
json_encode($object->toArray())
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.