【发布时间】:2013-09-27 15:59:46
【问题描述】:
如何获取当前用户发表评论的所有帖子。我有表之间的关系,但情况对我来说很难。应该是这样的:
$posts = Yii::app()->user->comments->posts->findAll(); // don't think that is my code, it just for explanation of query chain
所以我需要获取用户发表评论的所有帖子。
在 sql 中我的查询工作正常:
SELECT tc.title, tc.content, t.post_id
FROM tbl_comment t
JOIN tbl_post tc
ON t.post_id =tc.id
WHERE author_id =43
GROUP BY t.post_id
$CD = new CDbCriteria;
$CD->condition = 'tc.author_id='.Yii::app()->user->id;
$CD->join = 'JOIN tbl_comment tc ON t.id=tc.post_id';
$posts = Post::model()->findAll($CD);
就是这样。
【问题讨论】:
-
您意识到您发布的示例 SQL 非常不同,因为它使用 WHERE 子句而不是 IN 子句进行过滤。 . .你想要那个 SQL 的 PHP,还是你的第一个例子显示的?
-
我知道,因为我正在尝试找到最佳解决方案,没有
IN。 -
如果是这种情况,听起来您应该确保您的模型包含 author_id,如果您知道 author_id,请使用
addColumnCondition(array('author_id'=>$value))或类似的东西编写标准。 . . -
我的评论模型和帖子有作者(id)。
-
好的,那你试过什么?你有模特吗?您是否创建了标准?您是否应用了标准?
标签: php activerecord yii