doujiao1538 2011-10-06 07:50
浏览 12
已采纳

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

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:

users (id, name)

  • id is the auto-generated id
  • name of the user

tweets (id, content, user_id)

  • id is the auto-generated id
  • content is the text of the tweet
  • user_id is the id of the user that made the post

followers (id, user_id, following_id)

  • id is the auto-generated id
  • user_id is the user who is doing the following
  • following_id is the user that is being followed

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

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.

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?

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!

Thank you!

  • 写回答

1条回答 默认 最新

  • duanliusong6395 2011-10-06 19:17
    关注
    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';
    }
    

    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.

    Try it out.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 使用C#,asp.net读取Excel文件并保存到Oracle数据库
  • ¥15 C# datagridview 单元格显示进度及值
  • ¥15 thinkphp6配合social login单点登录问题
  • ¥15 HFSS 中的 H 场图与 MATLAB 中绘制的 B1 场 部分对应不上
  • ¥15 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配