Php Yii REST API-在查询中使用多个联接
我有一个带有RESTAPI的yii应用程序。我想得到一个post对象,包括该帖子的所有评论以及该帖子创建者的用户对象。 同样在评论中,我希望每个留下评论的用户都有一个user对象 因此,一个帖子和一个帖子用户和许多评论每个用户 为api请求提供服务的post控制器如下所示:Php Yii REST API-在查询中使用多个联接,php,mysql,rest,yii2,Php,Mysql,Rest,Yii2,我有一个带有RESTAPI的yii应用程序。我想得到一个post对象,包括该帖子的所有评论以及该帖子创建者的用户对象。 同样在评论中,我希望每个留下评论的用户都有一个user对象 因此,一个帖子和一个帖子用户和许多评论每个用户 为api请求提供服务的post控制器如下所示: public function actionIndex(){ $post = Post::find() ->joinWith('user) ->joinWi
public function actionIndex(){
$post = Post::find()
->joinWith('user)
->joinWith('comments')
->asArray()
->all();
}
return $post;
{
"id":"1",
"message":"this is a post",
"user":
[{
"id:11",
"name":"bob smith"
}],
"comments":
[{
"id:21",
"remark":"this is a comment"
}]
}
public function getComments()
{
return $this->hasMany(Comment::className(), ['object_id' => 'id'])->innerJoinWith('user u2', 'u2.id = comment.created_by');
}
然后是用户和评论的模型:
public function getUser()
{
return $this->hasOne(User::className(), ['id' => 'created_by'])->innerJoinWith('profile p1');
}
public function getComments()
{
return $this->hasMany(Comment::className(), ['object_id' => 'id'])->leftJoin('user u2', 'u2.id = comment.created_by');
}
帖子的用户返回罚款。将返回注释。但每个评论都没有返回用户。我觉得getComments()方法中的左连接应该会拉入用户。少了什么
我得到了这样的回复:
public function actionIndex(){
$post = Post::find()
->joinWith('user)
->joinWith('comments')
->asArray()
->all();
}
return $post;
{
"id":"1",
"message":"this is a post",
"user":
[{
"id:11",
"name":"bob smith"
}],
"comments":
[{
"id:21",
"remark":"this is a comment"
}]
}
public function getComments()
{
return $this->hasMany(Comment::className(), ['object_id' => 'id'])->innerJoinWith('user u2', 'u2.id = comment.created_by');
}
我想找回这个:
{
"id":"1",
"message":"this is a post",
"user":
[{
"id:11",
"name":"bob smith"
}],
"comments":
[{
"id:21",
"remark":"this is a comment",
"user":
[{
"id:41",
"name":"jane doe"
}]
}]
}
更新:如果我将getComments()
从leftJoin
更改为innerJoinWith
,如下所示:
public function actionIndex(){
$post = Post::find()
->joinWith('user)
->joinWith('comments')
->asArray()
->all();
}
return $post;
{
"id":"1",
"message":"this is a post",
"user":
[{
"id:11",
"name":"bob smith"
}],
"comments":
[{
"id:21",
"remark":"this is a comment"
}]
}
public function getComments()
{
return $this->hasMany(Comment::className(), ['object_id' => 'id'])->innerJoinWith('user u2', 'u2.id = comment.created_by');
}
…然后我得到格式正确的输出,但它只包括包含评论的帖子。我没有检查,但您可以尝试:
$post = Post::find()
->joinWith('user')
->joinWith(['comments' => function($q) {
$q->joinWith(['user']);
}])
->asArray()
->all();
}
我尚未检查,但您可以尝试:
$post = Post::find()
->joinWith('user')
->joinWith(['comments' => function($q) {
$q->joinWith(['user']);
}])
->asArray()
->all();
}
在我看来,这实际上是错误的。请阅读此内容-阅读有关展开部分和外部字段的内容。@ineersa我读到了…不知道如何实现它。如果您可以检查的话,我在问题中添加了一些代码。这不是很容易做到的,只需在您的控制器操作中使用
with()
而不是joinWith()
,然后将user
和comments
添加到extraFields
,然后在操作中使用链接expand=user,comments
。在getComments()
中,尝试使用joinWith()加载数据。应该做到这一点。@ineersa不知道如何做到这一点。我有一个有帖子的用户,还有一个有评论的用户。你能用一些密码来回答吗?我认为这是错误的。请阅读此内容-阅读有关展开部分和外部字段的内容。@ineersa我读到了…不知道如何实现它。如果您可以检查的话,我在问题中添加了一些代码。这不是很容易做到的,只需在您的控制器操作中使用with()
而不是joinWith()
,然后将user
和comments
添加到extraFields
,然后在操作中使用链接expand=user,comments
。在getComments()
中,尝试使用joinWith()加载数据。应该做到这一点。@ineersa不知道如何做到这一点。我有一个有帖子的用户,还有一个有评论的用户。你能用一些代码回答吗?这只会返回有评论的帖子。你试过了吗?joinWith生成一个左连接,因此它只返回带有注释的post似乎很奇怪。我尝试过,但Active RecordjoinWith
是一个内部连接。请查看以下文档:。。。默认情况下,最后一个参数“joinType”是LEFT JOIN。这将只返回有注释的帖子。您尝试过吗?joinWith生成一个左连接,因此它只返回带有注释的post似乎很奇怪。我尝试过,但Active RecordjoinWith
是一个内部连接。请查看以下文档:。。。默认情况下,最后一个参数“joinType”是左连接。