从yii2中的表获取数据的问题

发布于 2025-02-13 20:51:23 字数 635 浏览 0 评论 0原文

我已经在YII2模型用户中创建了方法,以获取当前用户

public函数getallRepliesForusers(){return $ this-> hasmany的 所有答复]) - > viatable('requeies_links',['requeies_id'=> 'id'])) - >其中(['entity'=>'用户']); } 我的回复表 “在此处输入图像描述” 我的用户表 “在此处输入图像描述” 和链接这两个表

我的方法正确吗?

I have created method in the Yii2 model Users to get all the replies for the current user

public function getAllRepliesForUsers() { return $this->hasMany(Replies::class, ['user_id' => 'id'])->viaTable('replies_links', ['replies_id' => 'id'])->where(['entity'=>'user']); }
My replies tableenter image description here
My users table enter image description here
and the final table that links these two tables enter image description here

Is my method is correct?

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

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

发布评论

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

评论(1

街角迷惘 2025-02-20 20:51:25

这是用户与答复的关系。您可以使用GII模块的型号生成器,这样您就不会通过手动键入它们而感到困惑。

  public function getReplies()
{
    return $this->hasMany(Replies::className(), ['id' => 'reply_id'])->viaTable('rply_links', ['user_id' => 'id']);
}

(我可以知道您打算如何处理条件 - >在哪里(['entity'=>'user'])?)。

Here's the relationship of Users to the Replies. You can use the Model generator of Gii module so you won't get confused by manually typing them.

  public function getReplies()
{
    return $this->hasMany(Replies::className(), ['id' => 'reply_id'])->viaTable('rply_links', ['user_id' => 'id']);
}

(May I know what do you intend to do with the condition ->where(['entity'=>'user'])?).

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