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.

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

报告相同问题?

悬赏问题

  • ¥20 机器学习能否像多层线性模型一样处理嵌套数据
  • ¥20 西门子S7-Graph,S7-300,梯形图
  • ¥50 用易语言http 访问不了网页
  • ¥50 safari浏览器fetch提交数据后数据丢失问题
  • ¥15 matlab不知道怎么改,求解答!!
  • ¥15 永磁直线电机的电流环pi调不出来
  • ¥15 用stata实现聚类的代码
  • ¥15 请问paddlehub能支持移动端开发吗?在Android studio上该如何部署?
  • ¥20 docker里部署springboot项目,访问不到扬声器
  • ¥15 netty整合springboot之后自动重连失效