<legend id='4n0LB'><style id='4n0LB'><dir id='4n0LB'><q id='4n0LB'></q></dir></style></legend>

<small id='4n0LB'></small><noframes id='4n0LB'>

  1. <i id='4n0LB'><tr id='4n0LB'><dt id='4n0LB'><q id='4n0LB'><span id='4n0LB'><b id='4n0LB'><form id='4n0LB'><ins id='4n0LB'></ins><ul id='4n0LB'></ul><sub id='4n0LB'></sub></form><legend id='4n0LB'></legend><bdo id='4n0LB'><pre id='4n0LB'><center id='4n0LB'></center></pre></bdo></b><th id='4n0LB'></th></span></q></dt></tr></i><div id='4n0LB'><tfoot id='4n0LB'></tfoot><dl id='4n0LB'><fieldset id='4n0LB'></fieldset></dl></div>
    1. <tfoot id='4n0LB'></tfoot>
        <bdo id='4n0LB'></bdo><ul id='4n0LB'></ul>

      在 yii2 中对多个表使用连接

      时间:2023-10-16
          <tbody id='4AAp6'></tbody>
        <tfoot id='4AAp6'></tfoot>
        <legend id='4AAp6'><style id='4AAp6'><dir id='4AAp6'><q id='4AAp6'></q></dir></style></legend>
        1. <small id='4AAp6'></small><noframes id='4AAp6'>

        2. <i id='4AAp6'><tr id='4AAp6'><dt id='4AAp6'><q id='4AAp6'><span id='4AAp6'><b id='4AAp6'><form id='4AAp6'><ins id='4AAp6'></ins><ul id='4AAp6'></ul><sub id='4AAp6'></sub></form><legend id='4AAp6'></legend><bdo id='4AAp6'><pre id='4AAp6'><center id='4AAp6'></center></pre></bdo></b><th id='4AAp6'></th></span></q></dt></tr></i><div id='4AAp6'><tfoot id='4AAp6'></tfoot><dl id='4AAp6'><fieldset id='4AAp6'></fieldset></dl></div>

                <bdo id='4AAp6'></bdo><ul id='4AAp6'></ul>
                本文介绍了在 yii2 中对多个表使用连接的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                问题描述

                我正在使用 yii2,我有 3 个表:帖子、粉丝、评论,我想使用 joinWith() 获取带有他们评论的帖子以及帖子和评论的粉丝名称(在粉丝表中).我写的是这个查询:

                facebook_posts::find()->joinwith('fans')->joinWith('comments')->all();

                并且我为关系添加了这两个函数:

                公共函数 getfans() {return $this->hasOne(Fans::className(), ['id' => 'from_id'])->from(fans::tableName() . 'FBF');}公共函数 getComments() {return $this->hasMany(Comments::className(), ['parent_id' => 'id'])->from(comments::tableName() .'FBC');}

                这给了我写帖子的粉丝的帖子和数据及其评论,但我需要的是也写评论的粉丝的数据,那么我如何将评论加入粉丝表??

                解决方案

                确保您的 Comments 模型中有 fan 关系,然后您可以使用以下内容获取每个帖子的所有评论以及每个评论的粉丝关系:

                facebook_posts::find()->joinWith('fans')->joinWith(['comments', 'comments.fan'])->all();

                I am using yii2, I have 3 tables: posts, fans, comments and i want to use joinWith() to get the posts with their comments and the fan name (in fans table) for post and comments. what i wrote is this query:

                <pre>
                 facebook_posts::find()->joinwith('fans')->joinWith('comments')->all();
                </pre>
                

                and I added these two functions for the relations:

                <pre>
                    public function getfans() {
                        return $this->hasOne(Fans::className(), ['id' => 'from_id'])->from(fans::tableName() . ' FBF');
                    }
                    public function getComments() {
                        return $this->hasMany(Comments::className(), ['parent_id' => 'id'])->from(comments::tableName() . ' FBC');
                    }
                </pre>
                

                this gives me the posts and the data of the fan who wrote the post and its comments but what i need is the data of fan that wrote the comments also, so how can i join comments with fans table ??

                解决方案

                Make sure you have a fan relation in your Comments model, then you can use the following to get all comments for each post and the fan relation for each comment:

                facebook_posts::find()->joinWith('fans')->joinWith(['comments', 'comments.fan'])->all();
                

                这篇关于在 yii2 中对多个表使用连接的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                上一篇:使用 Yii2 的 GridView 中的默认过滤器 下一篇:在 Yii2 中从数据库中多态地查找模型

                相关文章

                1. <tfoot id='EKxYR'></tfoot>
                2. <small id='EKxYR'></small><noframes id='EKxYR'>

                    <legend id='EKxYR'><style id='EKxYR'><dir id='EKxYR'><q id='EKxYR'></q></dir></style></legend>
                    <i id='EKxYR'><tr id='EKxYR'><dt id='EKxYR'><q id='EKxYR'><span id='EKxYR'><b id='EKxYR'><form id='EKxYR'><ins id='EKxYR'></ins><ul id='EKxYR'></ul><sub id='EKxYR'></sub></form><legend id='EKxYR'></legend><bdo id='EKxYR'><pre id='EKxYR'><center id='EKxYR'></center></pre></bdo></b><th id='EKxYR'></th></span></q></dt></tr></i><div id='EKxYR'><tfoot id='EKxYR'></tfoot><dl id='EKxYR'><fieldset id='EKxYR'></fieldset></dl></div>
                      <bdo id='EKxYR'></bdo><ul id='EKxYR'></ul>