php – Symfony 2 Doctrine导出到JSON
发布时间:2020-12-13 16:01:32 所属栏目:PHP教程 来源:网络整理
导读:我正在使用Symfony 2和Doctrine 2为iOS应用程序创建Web服务( JSON). 要获取我的实体我做: $articles = $this-getDoctrine()-getRepository('UdoPaddujourBundle:MenuArticle')-findAll(); 我必须告诉你: $article = array();$article = $articles-toArray(
我正在使用Symfony 2和Doctrine 2为iOS应用程序创建Web服务(
JSON).
要获取我的实体我做: $articles = $this->getDoctrine()->getRepository('UdoPaddujourBundle:MenuArticle')->findAll(); 我必须告诉你: $article = array(); $article = $articles->toArray(); 给我以下错误: Fatal error: Call to a member function toArray() on a non-object 同样的事情发生了 $article = $articles->exportTo('json'); 我怎样才能创建一个json响应? 亲切的问候, 编辑: array(18) { [0]=> object(UdoPaddujourBundleEntityMenuArticle)#50 (4) { ["id":"UdoPaddujourBundleEntityMenuArticle":private]=> int(1) ["name":"UdoPaddujourBundleEntityMenuArticle":private]=> string(17) "My Article Name 1" ["description":"UdoPaddujourBundleEntityMenuArticle":private]=> string(26) "My Article Description 1" ["price":"UdoPaddujourBundleEntityMenuArticle":private]=> float(20) } [1]=> ... – 稍后编辑 我怎样才能遍历所有“属性名称”? $myarray=array(); $myArray["name"]=array(); $myArray["description"]=array(); foreach($articles in $article) { array_push($myArray["name"],$article->getName()); array_push($myArray["description"],$article->getDescription()); }
如果您使用学说查询,您也可以这样做:
$em = $this->getDoctrine()->getEntityManager(); $query = $em->createQuery('SELECT ma FROM UdoPaddujourBundle:MenuArticle ma ...etc'); $myArray = $query->getArrayResult(); 然后是json_encode($myArray);有关详细信息,请参见here (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |