开发者

symfony - returning JSON from a peer method call in an action

I have some code that checks a parameter and the calls a peer method to get me items fro开发者_JS百科m the DB.

What I need to get is these items in JSON.

My peer method is like:

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 is:

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 items. The problem is, how do I return all of the items in JSON?

I've tried using:

        $this->getResponse()->setHttpHeader('Content-type', 'application/json');
        $words = ItemPeer::searchFromArticleRequest($this->word);
        return $this->renderText(json_encode($words));

But this just returns loads of empty JSON brackets: [{},{},{},{},{},{},{},{},{},{},{},{},{},{}]

Thanks


It seems that json_encode() doesn't like the way Propel Objects are built.

Another solution could be forcing Propel to returnin basic associative objects, using XXXPeer::doSelectStmt()

public static function searchFromRequest($word, $returnPropelObjects = true)
{
    $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);

    if ($returnPropelObjects)
      return self::doSelect($c);

    $stmt = self::doSelectStmt($c);
    $results = array();
    while ($row = $stmt->fetch(PDO::FETCH_ASSOC)) {
      $results[] = $row;
    }
    return $results;
}


Propel 1.6:

  • object->toJSON();
  • collection->exportTo('JSON');


The json_encode/json_decode can only encode/decode PHP arrays not objects. The variable

$words

will be an array of Item objects, that's why the output you wrote.

There are basically two solutions. You write your own json encoder that works for objects, like the first comment here:

http://php.net/manual/en/function.json-encode.php

or you write a function that converts your Item objects into PHP arrays like here:

http://www.phpro.org/examples/Convert-Object-To-Array-With-PHP.html


You could also call toArray() on your objects.

$words = ItemPeer::searchFromArticleRequest($this->word);
$wordsArray = array();
foreach ($words as $word)
{
    $wordsArray[] = $word->toArray();
}
return $this->renderText(json_encode($wordsArray));

Propel 1.6 will have a toJSON() method for the individual objects or for a whole collection of objects.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜