I have code that checks a parameter and calls the peer method to get items from the database.
I need to get these elements in JSON.
My peer method is similar:
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); }
and my action:
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"; }
I can var_dump($words) , and I get an array (collection) of elements. The problem is how to return all elements in JSON?
I tried using:
$this->getResponse()->setHttpHeader('Content-type', 'application/json'); $words = ItemPeer::searchFromArticleRequest($this->word); return $this->renderText(json_encode($words));
But it just returns loads of empty JSON brackets: [{},{},{},{},{},{},{},{},{},{},{},{},{},{}]
thanks