4

I have some code that checks a parameter and the calls a peer method to get me items from 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

Clement Herreman
  • 10,274
  • 4
  • 35
  • 57
terrid25
  • 1,926
  • 8
  • 46
  • 87

4 Answers4

3

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;
}
Maxime Pacary
  • 22,336
  • 11
  • 85
  • 113
3

Propel 1.6:

  • object->toJSON();
  • collection->exportTo('JSON');
Evgeni Nabokov
  • 2,421
  • 2
  • 32
  • 36
1

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

Aston
  • 3,654
  • 1
  • 21
  • 18
1

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.

Jan Fabry
  • 7,221
  • 2
  • 36
  • 41
  • I have found toJSON() method for the individual object, but have not found for a whole collection. How to serialize a collection? **UPD** Ooops, this is it -- exportTo('JSON'). – Evgeni Nabokov Feb 10 '12 at 11:15