Php HasAndBelongName未为associationForeignKey提取数据

Php HasAndBelongName未为associationForeignKey提取数据,php,cakephp,Php,Cakephp,在我的CakePHP应用程序中,我的用户可以与其他用户成为朋友 这是通过users表和friends\u users表完成的,该表具有以下列:id、user\u id、friends\u id、status 因此,如果用户id 1是用户id 2的朋友,则在用户id列中有1,在朋友id列中有2 用户的模型如下所示: public $hasAndBelongsToMany = array( 'Friend'=>array( 'className'

在我的CakePHP应用程序中,我的用户可以与其他用户成为朋友

这是通过
users
表和
friends\u users
表完成的,该表具有以下列:
id、user\u id、friends\u id、status

因此,如果用户id 1是用户id 2的朋友,则在用户id列中有1,在朋友id列中有2

用户的模型如下所示:

public $hasAndBelongsToMany = array(
        'Friend'=>array(
            'className'              => 'User',
            'joinTable'              => 'friends_users',
            'foreignKey'             => 'user_id',
            'associationForeignKey'  => 'friend_id'
        )
    );
当查看用户1时,它工作正常,并将用户2显示为朋友。但是当查看用户2时,它不会将用户1显示为朋友。。。因此,当反向查看时,它不会填满数据

以下是我用于为用户列出朋友的方法:

$user = $this->User->find('first', array( 
                    'conditions' => array('User.id' => $this->Auth->user('id')),
                    'contain'=>'Profile'
                ));

        $friends = $this->User->find('first', 
            array(
                'conditions'=>array(
                   'User.id'=>$user['User']['id']
                ),
                'contain'=>array(
                    'Profile',
                    'Friend'=>array(
                        'Profile',
                        'conditions'=>array(
                            'FriendsUser.status'=>1
                        )
                    )
                )
            )
        );

        $this->set('friends', $friends);
知道为什么会这样吗?我看了所有的文件,但我觉得一切都很好

理想情况下,我希望修复代码,而不是被指向其他资源


页面上的查询:

这是一个有趣的问题,几年前我在建立一个交友网站时也遇到过同样的问题

您需要做的是(在我的例子中)有4组关系:

  • 正向关系
  • 转发关系(未经批准的友谊)
  • 落后关系
  • 落后关系(未经批准的友谊)
这将确保您提取所有必要的数据,尽可能使用
containable

var$hasAndBelongsToMany=array(
“ForwardRelation”=>数组(
'className'=>'User',
'joinTable'=>'friends\u users',
“foreignKey”=>“用户id”,
“条件”=>array('approved'=>'1'),
'associationForeignKey'=>'friend_id',
),
“ForwardRelationUn”=>数组(
'className'=>'User',
'joinTable'=>'friends\u users',
“foreignKey”=>“用户id”,
“条件”=>array('approved'=>'0'),
'associationForeignKey'=>'friend\u id'
),
“反向关系”=>数组(
'className'=>'User',
'joinTable'=>'friends\u users',
'foreignKey'=>'friend_id',
'associationForeignKey'=>'用户id',
“条件”=>array('approved'=>1)
),
“BackRelationUn”=>数组(
'className'=>'User',
'joinTable'=>'friends\u users',
'foreignKey'=>'friend_id',
'associationForeignKey'=>'用户id',
“条件”=>array('approved'=>'0')
)
);
用一些
afterFind()
magic添加一个行为,您将获得金牌

这是我寻找朋友的功能:

函数getFriends($userid,$search=null){ $Friends=ClassRegistry::init('FriendsUser'); $Friends->bindModel( 排列( “belongsTo”=>数组( “用户”=>数组( 'className'=>'User', 'foreignKey'=>'user\u id' ), '友元'=>数组( 'className'=>'User', 'foreignKey'=>'friend\u id' ) ) ) ); $data=$Friends->find( “全部”, 数组( '字段'=>数组( 'DISTINCT FriendsUser.friends_id', “FriendsUser.user_id”, “FriendsUser.批准”, “User.id”, “User.username”, “Friend.id”, '朋友。用户名' ), “条件”=>数组( '和'=>数组( '或'=>数组( 'Friend.username LIKE'=>'%.$search.'%', 'User.username类似'=>'%'.$search.'% ) ) ), '包含'=>数组( “用户”, “朋友” ), 'order'=>数组('User.username ASC'、'FriendsUser.approved DESC') ) ); $friendlist=array(); $i=0; foreach($数据作为$数据){ foreach($key=>$value的数据){ if(在数组($key,数组('User','Friend'))中){ 如果($value['id']==$userid){ $friendlist[$i]['Me']=$value; }否则{ $friendlist[$i]['Friend']=$value; } } } $i++; } $matches=array(); foreach($friendlist作为$friend){ if(stripos($friend['friend']['username'],$search)!==false) $matches[$friend['friend']['id']=$friend['friend']['username']; } asort($matches); $i=0; $newmatches=array(); foreach($key=>$match){ $newmatches[$i]['Tag']['caption']=$match; $newmatches[$i]['Tag']['value']=$key; $newmatches[$i]['Tag']['image']=$this->getProfileImage($key); $i++; } 返回$newmatches; }
当您为用户6检索好友时,您将获得好友8。当您为用户#8检索朋友时,您应该不会收到任何信息,因为在您的
friends\u users
表中没有用户#8的记录。因此,您需要在
friends\u users
表中插入额外的数据(
user\u id=8,friends\u id=6
),或者需要创建
friends
模型(不使用表)并通过
friend
检索用户。这就是心理体操——你需要意识到用户就是朋友,朋友就是用户

        $friends = $this->User->Friend->find('first', 
            array(
                'conditions'=>array(
                   'Friend.id'=>$id // user id whose friends you need to find
                ),
                'contain'=>array(
                    'Profile',
                    'User'=>array( // here you will get friends
                        'Profile',
                        'conditions'=>array(
                            'FriendsUser.status'=>1
                        )
                    )
                )
            )
        );

您的代码生成什么查询?您能否发布与这些查询相关的
friends\u用户
表的内容?什么协会有朋友模型?为什么不使用它?没有朋友模型!模型在用户模型中是虚拟的,这一点很清楚。你能回答前两个问题吗?我不明白你的要求。你能澄清一下吗。将应用程序的调试设置为2,从底部复制查询