Php 如何在zf2中获取数据并以json格式返回?
我使用MongoODM查询根据id获取数据我想获取查询数据并以json格式返回数据,我是如何做到的? 这是我的密码:Php 如何在zf2中获取数据并以json格式返回?,php,json,mongodb,zend-framework2,Php,Json,Mongodb,Zend Framework2,我使用MongoODM查询根据id获取数据我想获取查询数据并以json格式返回数据,我是如何做到的? 这是我的密码: public function loadAction() { $id = (int) $this->params()->fromRoute('id', 0); $dm = $this->getServiceLocator()->get('doctrine.documentmanager.odm_default');
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;
希望这对你有所帮助你能不能对你面临的每一个小任务都提出一个问题,并最终尝试先使用谷歌?