duanjian4331 2014-11-01 14:16
浏览 87
已采纳

在Doctrine结果上使用jsonModel返回JSON数据时获得空响应 - 特殊字符编码问题

I am having problems getting ZF2 jsonModel to return multi result from doctrine query. The issue appears only when I try to return the array result and not when returning individual items my code is as below:

public function mosquesAction() {

    $em = $this
            ->getServiceLocator()
            ->get('Doctrine\ORM\EntityManager');
    $dql = "select m.name from \Application\Entity\Mosque m ";
    $result = $em->createQuery($dql)->getResult();

    return new JsonModel($result);   
}

the Mosque Entity is:

namespace Application\Entity;

use Doctrine\ORM\Mapping as ORM;

/** @ORM\Entity */
class Mosque {

/**
 * @ORM\Id
 * @ORM\GeneratedValue(strategy="AUTO")
 * @ORM\Column(type="integer")
 */
private $id;

/** @ORM\Column(type="string") */
private $name;

/** @ORM\Column(type="string") */
private $address;

/** @ORM\Column(type="string") */
private $email;

/** @ORM\Column(type="string") */
private $website;

/** @ORM\Column(type="string") */
private $phone;

/** @ORM\Column(type="string") */
private $mobile;

/** @ORM\Column(type="string") */
private $fax;

/** @ORM\Column(type="string") */
private $coordinates;

/** @ORM\ManyToOne(targetEntity="Canton", inversedBy="mosques") */
private $canton;

public function getId() {
    return $this->id;
}

public function getName() {
    return $this->name;
}

public function setName($name) {
    $this->name = $name;
}

}

when I change the return value to

return new JsonModel($result[0])

I get the result

{"name":"IDTV - Mimar Sinan Camii"}

EDIT:

I think I found the origin of the issue, there is no problem with code I posted but it appears that JsonModel can't handle special characters like in

Islamische König Faysal Stiftung

So my question now is, how to encode these characters in JsonModel on php5.3?

Thank you in advance and sorry for my poor English

  • 写回答

1条回答 默认 最新

  • dongll0502 2014-11-01 19:56
    关注

    After playing with different options I got to the result that the issue resides in doctrine connection to MySQL database, So I changed my database collation to _utf8_general_i_ and added some parameters to the driver params:

    'charset' => 'utf8',
    'driverOptions' => array(
         1002 => 'SET NAMES utf8'
    )
    

    now I get the result I was expecting

    {
        "id": 2,
        "name": "Föderation Islamischer Gemeinschaften LU",
        "address": "Hauptstrasse 58",
        "email": "",
        "website": "",
        "phone": "041 260 90 98",
        "mobile": "",
        "fax": "",
        "coordinates": "47.0666986,8.2812254"
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 关于#MATLAB#的问题,如何解决?(相关搜索:信噪比,系统容量)
  • ¥500 52810做蓝牙接受端
  • ¥15 基于PLC的三轴机械手程序
  • ¥15 多址通信方式的抗噪声性能和系统容量对比
  • ¥15 winform的chart曲线生成时有凸起
  • ¥15 msix packaging tool打包问题
  • ¥15 finalshell节点的搭建代码和那个端口代码教程
  • ¥15 Centos / PETSc / PETGEM
  • ¥15 centos7.9 IPv6端口telnet和端口监控问题
  • ¥20 完全没有学习过GAN,看了CSDN的一篇文章,里面有代码但是完全不知道如何操作