CakePHP Twitter-clone:无法使系统工作 [英] CakePHP Twitter-clone: Can't get follow-system to work

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

问题描述

对于非描述性标题,我抱歉,但我不知道如何对其进行短语。目前,我正在尝试使用cakePHP开发Twitter克隆,因为我刚开始使用Web编程。我有3个表:


用户(id,名称)





  • id是自动生成的ID

  • 用户名称




tweets(id,content,user_id)





  • id是自动生成的ID

  • content是tweet的文本

  • user_id是用户的ID






blockquote>


  • id是自动生成的ID

  • user_id是执行以下操作的用户

  • follow_id是正在关注的用户



现在,几乎一切都基本上可以工作,功能有些。但是,我认为我可能设置了模型关系错误。



现在,这些都是模型关系:




  • 用户:有许多推文,有很多关注者

  • 推文
  • 关注者属于用户



是错误的,但这是我一直在一起工作。现在,什么工作是用户登录/注销,会话,发布,删除您自己的帖子,以及其他用途(然后能够看到他们的tweets)。



目前没有什么工作是当我尝试得到一个我跟踪的人或跟随我的人的列表。问题是我不知道如何访问该数据。



我想要一个屏幕,我可以点击链接查看我的所有人



例如,我相信我可以使用find()查询来获取所有人的列表。 m,但是从上面的Follower表中可以看出,该查询只返回从属者和followee的用户ID。从这些数据,我如何获得他们的名字和最新的tweet?



我要做两次查找吗?是我可以在我的模型关系中重新定义,所以,当我在我的ctp文件,我可以只是php看看,并使用像:

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

很抱歉,不确定我是否正确解释了自己...



我希望我的意图清楚。欢迎任何建议!



谢谢!

解决方案

  app / models / user.php 

类用户扩展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

类追随者扩展AppModel {
var $ name ='Follower';
var $ useTable ='users';
}

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




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!

解决方案

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.

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

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆