尝试以JSON格式显示页面时出错

时间:2014-08-11 22:05:42

标签: json symfony doctrine-orm

我在尝试以JSON格式显示页面时出现此错误,但使用html格式可以正常显示

执行

时发生异常
 'SELECT t0.id AS id1, t0.libProjet AS libProjet2, t0.site AS site3, t0.partceg AS partceg4, t0.datego AS datego5, t0.dateResmisOff AS dateResmisOff6, t0.dateTransfert AS dateTransfert7, t0.nompays_id AS nompays_id8, t0.libTypeProj_id AS libTypeProj_id9, t0.nomclt_id AS nomclt_id10,t0.libContrat_id AS libContrat_id11, t0.libEtatProj_id AS libEtatProj_id12, t0.libEtatOff_id AS libEtatOff_id13 

FROM projet t0 WITH(NOLOCK)WHERE t0.id =?'用params [" index.json"]:

这是实体

class projet
{
/**
* @var integer 
*
* @ORM\Column(name="id", type="integer")
* @ORM\Id
* @ORM\GeneratedValue(strategy="AUTO")
*/
private $id;

/**
* @var string
*
* @ORM\Column(name="libProjet", type="string", length=255)
*/
private $libProjet;

/**
* @var string
*
* @ORM\Column(name="site", type="string", length=255)
*/
private $site;

/**
* @var float
*
* @ORM\Column(name="partceg", type="float")
*/
private $partceg;

//....

这是控制器

public function indexAction()
{
$em = $this->getDoctrine()->getManager();

$entities = $em->getRepository('rexBundle:projet')->findAll();

$repository = $this->getDoctrine()->getRepository('rexBundle:projet');
$query = $repository->createQueryBuilder('t')

->select('t.id','t.libProjet','t.site','t.partceg',
'IDENTITY(t.nompays) AS nompays',
'IDENTITY(t.libTypeProj) AS libTypeProj',
'IDENTITY(t.nomclt) AS nomclt',
'IDENTITY(t.libContrat) AS libContrat',
'IDENTITY(t.libEtatProj) AS libEtatProj',
'IDENTITY(t.libEtatOff) AS libEtatOff',
't.datego',
't.dateResmisOff',
't.dateTransfert')

 ->getQuery();
 $entities = $query->getResult();

 $entity = new projet();
 $form = $this->createCreateForm($entity);

 return array(
 'entities' => $entities,
 'entity' => $entity,
 'form' => $form->createView(),
);
}

1 个答案:

答案 0 :(得分:1)

如果您想发送JSON响应,请阅读文档中的这篇文章。 http://symfony.com/doc/current/components/http_foundation/introduction.html#creating-a-json-response

现在在你的控制器中你不发送JSON响应,你只渲染表单

发送JSON响应的示例:

use Symfony\Component\HttpFoundation\Response;

$response = new Response();
$response->setContent(json_encode(array(
    'data' => 123,
)));
$response->headers->set('Content-Type', 'application/json');