2012-06-22 40 views
0

我使用上下文切换以获得在Zend框架JSON响应Zend的上下文切换JSON响应

这是我在控制器

$this->_helper->contextSwitch() 
       ->addActionContext('index', array('xml', 'json')) 
        ->setAutoJsonSerialization(true) 
        ->initContext(); 

使用init函数。在一些其他方法中的代码我有doctrine_collection我希望作为json响应的数据。

代码是

$pm = new ProfileMessage(); 
$flirts = $pm->fetchLastMessages($this->_member->user_id, "0,1", 
              Labels_MessageType::FLIRT, 5, 0); 

$this->view->flirts = $flirts; 

但响应我得到一个空的JSON字符串。

{"flirts":{}} 

我做错了什么。 在此先感谢

+0

什么样的返回值是$调情​​? – opHASnoNAME

+0

对不起,迟到的回应,它返回Doctrine_Collection,和$ flirts-> toArray()似乎是好的。 – kukipei

回答

0

您将需要确保$调情是一个数组或序列化对象:

php > $user = new Model_User(); 
php > $user->setId(10); 
php > echo json_encode($user); 
{} //output is empty 

/* Convert the object to array */ 
php > echo json_encode($user->toArray()); 
{"_id":10} //output not empty 

/* Trying a simple object */ 
php > $simple = new stdClass(); 
php > $simple->something = 'else'; 
php > echo json_encode($simple); 
{"something":"else"} //output not empty 
+0

谢谢,$ flirts-> toArray()是可以的 – kukipei