CakePHP Twitter-clone:无法让跟随系统工作

时间:2023-01-18
本文介绍了CakePHP Twitter-clone:无法让跟随系统工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

问题描述

抱歉没有描述性的标题,但我不知道如何措辞.目前,我正在尝试使用 cakePHP 开发 Twitter 克隆,因为我是网络编程的新手.我用了 3 张桌子:

Sorry for the non-descript title, but I'm not sure how to phrase it. Currently, I am trying my hand at developing a Twitter clone with cakePHP, since I'm new to web programming. I have gone with 3 tables:

用户(ID,姓名)

  • id 是自动生成的 id
  • 用户名
  • 推文(id、内容、user_id)

    tweets (id, content, user_id)

    • id 是自动生成的 id
    • content 是推文的文本
    • user_id 是发布帖子的用户的 ID
    • 关注者(id、user_id、follow_id)

      followers (id, user_id, following_id)

      • id 是自动生成的 id
      • user_id 是执行以下操作的用户
      • following_id 是被关注的用户
      • 现在,几乎一切都基本正常了,即使是follow功能也有点.但是,我想我可能错误地设置了模型关系.事实上,我很确定这是错误的.

        Now, almost everything is basically working, even the follow function somewhat. However, I think I may have set up the model relationships wrong. In fact, I'm pretty sure it's wrong.

        现在,这些是模型关系:

        Right now, these are the model relationships:

        • 用户:有很多推文,有很多粉丝
        • 推文:属于用户
        • 关注者:属于用户
        • User: has many Tweets, has many Followers
        • Tweets: belongs to User
        • Followers: belongs to User

        我很确定 Followers 关系是错误的,但这就是我一直在处理的问题.现在,有效的是用户登录/注销、会话、发帖、删除您自己的帖子以及关注其他用途(然后能够看到他们的推文).

        I'm pretty sure the Followers relationship is wrong, but that's what I've been working with. Right now, what works is the user login/logout, sessions, posting, deleting your own posts, and following other uses (and then being able to see their tweets).

        现在行不通的是,当我尝试获取我关注的人或关注我的人的列表时.问题是我不确定如何访问这些数据.

        What doesnt work right now is when I try to get a list of people I'm following or people that is following me. The problem is I'm not sure how to access that data.

        我想要一个屏幕,我可以在其中单击链接查看我关注的所有人,然后查看他们的所有最新推文.

        I want to have a screen where I can click on a link to see all the people I'm following and then see all of their latest tweets.

        例如,我相信我可以使用 find() 查询来获取我关注的所有人的列表,但正如您从上面的 Follower 表中看到的,该查询仅返回以下用户的用户 ID追随者和被追随者.从这些数据中,我如何获得他们的姓名和最新推文?

        For example, I believe I can do a use a find() query to get a list of all the people I'm following, but as you can see from the Follower table above, that query only returns the user IDs of the follower and followee. From that data, how do I get access to their names and latest tweet?

        我要进行两次查找吗?我可以在我的模型关系中重新定义它,以便在我的 ctp 文件中,我可以通过 php 查看并使用类似的东西:

        Do I do two lookups? Is it something I can redefine in my model relationships, so that when in my ctp file, I can just php look through and use something like:

        $this->$data['Tweet']['userID']
        $this->$data['User']['name']
        $this->$data['Tweet']['content']
        

        对不起,我不确定我是否正确地解释了自己...

        I'm sorry, I'm not sure if I explained myself correctly...

        不过,我确实希望我的意图很明确.任何建议表示赞赏!

        I do hope my intention is clear however. Any advice is appreciated!

        谢谢!

        推荐答案

        app/models/user.php
        
        class User extends AppModel{
              var $name = 'User';
        
              var $hasAndBelongsToMany = array(
                    'Follower' => array(
                          'className'             => 'Follower',
                          'joinTable'             => 'user_users',
                          'foreignKey'            => 'user_id',
                          'associationForeignKey' => 'child_user_id'
                    )
              )
        }
        
        app/models/follower.php
        
        class Follower extends AppModel{
              var $name     = 'Follower';
              var $useTable = 'users';
        }
        

        您必须创建一个包含 user_id 和 child_user_id 字段的表 user_users.child_user_id 将是关注者 ID,如果您愿意,您可以将其命名为关注者 ID,但如果您将来需要建立类似的关系,可能会造成混淆.

        You will have to make a table user_users with the fields user_id and child_user_id. child_user_id would be the follower ID, you could name it follower id if you want but it might be confusing if you need to make a similar relation in the future.

        试试看.

        这篇关于CakePHP Twitter-clone:无法让跟随系统工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

上一篇:CakePHP:不能做简单的登录 下一篇:如何使 CakePHP 的 HABTM 复选框按字母顺序排列在列中

相关文章

最新文章