問題描述
抱歉沒有描述性的標題,但我不知道如何措辭.目前,我正在嘗試使用 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 是自動生成的 id
- content 是推文的文本
- user_id 是發布帖子的用戶的 ID
- id 是自動生成的 id
- user_id 是執行以下操作的用戶
- following_id 是被關注的用戶
- 用戶:有很多推文,有很多粉絲
- 推文:屬于用戶
- 關注者:屬于用戶
- User: has many Tweets, has many Followers
- Tweets: belongs to User
- Followers: belongs to User
推文(id、內容、user_id)
tweets (id, content, user_id)
關注者(id、user_id、follow_id)
followers (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:
我很確定 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:無法讓跟隨系統工作的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!