YII获得关系模型(如何使用cjson渲染关系)

时间:2012-08-01 06:09:34

标签: php yii

我有一个像Story这样的模特。它有一个键将其链接到作者。我有这种关系,当我这样做。

storyHolder = Story->findByPK(id)

返回故事,然后

storyHolder ->author

返回作者对象。问题是我自己获取两个数据集我希望它们结合起来。示例returnObject {story {},author {}}。我想要returnObject {story {author {}}}

1 个答案:

答案 0 :(得分:0)

对不起,如果问题不清楚,

看起来真正的问题不在于故事 - >作者,而在于CJSON。

关注问题,在回答的问题中,评论是聊天的链接,在聊天中是该论坛帖子的链接。

http://www.yiiframework.com/forum/index.php/topic/12952-cjsonencode-for-an-ar-object-does-not-include-its-related-objects/

我结束了把它放在我的控制器中

    /**
     * @return CMapIterator the iterator for the foreach statement
     */
    public function getIterator()
    {
            $attributes=$this->getAttributes();
            $relations = array();

            foreach ($this->relations() as $key => $related)
            {
                    if ($this->hasRelated($key))
                    {
                            $relations[$key] = $this->$key;
                    }
            }

            $all = array_merge($attributes, $relations);

            return new CMapIterator($all);
    }

来自Yii论坛的benjaminbytheway代码。

只是尝试清除在您的视图中使用CJSON时出现的问题,它不包含关系对象。