[英]How i get data and returned in json format in zf2?
我使用mongo odm查詢根據ID獲取數據,我想獲取查詢數據並以json格式返回此數據,我該怎么做? 這是我的代碼:
public function loadAction()
{
$id = (int) $this->params()->fromRoute('id', 0);
$dm = $this->getServiceLocator()->get('doctrine.documentmanager.odm_default');
$data = $this->dm->createQueryBuilder('Calendar\Document\Event')
->field('calendar_id')->equals($id)->getQuery()->execute();
//$count_tags = $eventdata->count();
$array = array();
if($data && !is_null($data) && is_object($data)){
foreach($data as $key=>$value) {
$array[] = $value;
}
}
return $this->getResponse()->setContent(Json::encode($array));
}
您可以按照以下步驟完成此操作
return new JsonModel($arrayVaraible);
您可能需要導入
Zend\View\Model\JsonModel;
希望有幫助
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.