2011-08-10 97 views
0

下面是一个用户表和一个友谊表。 当给定的用户登录时,我检索所有的友谊,其中$ id == user_a_id或$ id == user_b_id。cakephp模型关系

| Friendships | 
| id | user_a_id | user_b_id | created | modified | 
| 1 |  1  |  2  | created | modified | 
| 2 |  1  |  5  | created | modified | 
| 3 |  3  |  1  | created | modified | 
| 4 |  2  |  4  | created | modified | 
| 5 |  3  |  2  | created | modified | 

| Users | 
| id | first_name | last_name | created | modified | 
| 1 | Barrack | Obama | -- | -- | 
| 2 | John  | McCain | -- | -- | 

当奥巴马登录时,我想显示他的朋友的名单,所以查询结果需要还含有从用户表2行。

你如何设定这种关系与蛋糕?

这是Friendship hasMany Users?有没有关于这个地方的任何教程?

感谢

编辑:

我只是希望能够指定以下内容:

$this->set('friendships', $this->Friendship->find('all' , IncludeBothUsersInfoInQuery)); 

所以,在视图中的每个友谊如下所示,我可以有机会给用户两端的数据:

[Friendship] 
    [id] 
    [user_a_id] 
    [user_b_id] 
    [created] 
    [modified] 
     [User_A_Info] 
      [id] 
      [first_name] 
      [last_name] 
      [created] 
      [modified] 
     [User_B_Info] 
      [id] 
      [first_name] 
      [last_name] 
      [created] 
      [modified] 

回答

1

友谊belongsTo User_A和User_B(你需要2个belongsTo关系),记得设置外键。你熟悉易于理解的,对吗?如果不是,查看它,你会看到如何查询你需要的数据。你可能也想设置User hasMany Friendship。