我有一些代碼檢查參數,並調用對等方法從數據庫中獲取項目。symfony - 從動作中的對等方法調用返回JSON
我需要得到的是JSON中的這些項目。
我同行的方法是這樣的:
public static function searchFromRequest($word)
{
$c = new Criteria();
$c->addJoin(self::ARTICLE_ID, ArticlePeer::ID);
$c->add(self::TITLE, '%'.$word.'%', Criteria::LIKE);
$c->addAnd(self::ARTICLE_ID, null, Criteria::ISNOTNULL);
$c->addAscendingOrderByColumn(self::TITLE);
return self::doSelect($c);
}
,我的動作是:
public function executeSearch()
{
$this->word = $this->getRequestParameter('word');
$this->content_type = $this->getRequestParameter('content_type');
if($this->content_type == 'article')
{
$words = ItemPeer::searchFromRequest($this->word);
}
else
{
echo "Nothing here";
}
我可以var_dump($words)
,我得到項目的數組(集合)。問題是,我如何返回JSON中的所有項目?
我已經嘗試使用:
$this->getResponse()->setHttpHeader('Content-type', 'application/json');
$words = ItemPeer::searchFromArticleRequest($this->word);
return $this->renderText(json_encode($words));
但這只是返回空JSON支架的負荷:[{},{},{},{},{},{},{},{},{},{},{},{},{},{}]
感謝
你正在使用什麼版本的Propel和Symfony? – 2011-03-25 06:50:46
@Jan Fabry我正在使用symfony 1.4和Propel 1.4 – terrid25 2011-03-25 11:13:01