symfony - 从操作中的对等方法调用返回 JSON

发布于 2024-10-26 09:57:21 字数 1256 浏览 6 评论 0 原文

我有一些代码检查参数并调用对等方法以从数据库获取项目。

我需要得到的是 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 括号: [{},{},{},{},{},{},{},{},{},{}, {},{},{},{}]

谢谢

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

救星 2024-11-02 09:57:21

似乎 json_encode() 不喜欢 Propel 对象的构建方式。

另一种解决方案可能是使用 XXXPeer::doSelectStmt() 强制 Propel 返回基本关联对象

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;
}

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;
}
稚气少女 2024-11-02 09:57:21

Propel 1.6:

  • 对象->toJSON();
  • 集合->exportTo('JSON');

Propel 1.6:

  • object->toJSON();
  • collection->exportTo('JSON');
西瑶 2024-11-02 09:57:21

json_encode/json_decode 只能对 PHP 数组进行编码/解码,不能对对象进行编码/解码。该变量

$words

将是 Item 对象的数组,这就是您编写的输出的原因。

基本上有两种解决方案。您编写自己的适用于对象的 json 编码器,如此处的第一条评论:

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

或者您编写一个函数将 Item 对象转换为 PHP 数组,如下所示:

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

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

墨洒年华 2024-11-02 09:57:21

您还可以在对象上调用toArray()

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

Propel 1.6 将有一个 toJSON() 方法 对于单个对象或整个对象集合。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文