1

我在 CakePHP 中构建了一个应用程序,用户可以在其中与其他用户成为朋友。用户有个人资料,并且友谊在朋友表中的用户 ID 之间链接(模型进一步向下)

在我的 FriendsController 中,我传递了一个用户名,然后使用以下方法为用户获取好友:请注意,此方法在我的用户模型中,调用方式如下:$this->User->getFriends($username);在我的 FriendsController 中!

function getFriends($username) {
        //Start by getting User's ID
        $user = $this->find(
            'first',
            array(
                'conditions' => array(
                    'User.username' => $username
                )
            )
        );


        $profileAndFriends = $this->Profile->find(
            'first',  //Because User hasOne Profile, so we'll fetch this one profile.
            array(
                'conditions' => array(
                    'Profile.user_id' => $user['User']['id']
                ),
                'contain' => array(
                    'UserFrom' => array(
                        'conditions' => array(
                            'status' => 1
                        )
                    ),
                    'UserTo' => array(
                        'conditions' => array(
                            'status' => 1
                        )
                    )
                )             
            )
        );
        return $profileAndFriends;
    }

我的用户模型:

类用户扩展 AppModel { public $name = 'User';

public $hasOne = 'Profile';

public $hasMany = array(
    'UserFrom'=>array(
        'className'=>'Friend',
        'foreignKey'=>'user_from'
    ),
    'UserTo'=>array(
        'className'=>'Friend',
        'foreignKey'=>'user_to'
    )
);

}

和我的个人资料模型:

class Profile extends AppModel
{
    public $name = 'Profile';

    public $belongsTo = 'User';
}

和朋友模型:

class Friend extends AppModel
{
    var $name = 'Friend';

    public $belongsTo = array(
        'UserFrom'=>array(
            'className'=>'User',
            'foreignKey'=>'user_from'
        ),
        'UserTo'=>array(
            'className'=>'User',
            'foreignKey'=>'user_to'
        )
    );
}

所以在我看来,我有:

<?php foreach ($friends as $friend) : ?>

        <?php echo $friend['UserTo']['firstname']; ?>

    <?php endforeach; ?>

但是它无法理解名字部分!所以...

如果我在视图中对 $friend 进行调试,我会得到:

array(
    'Friend' => array(
        'id' => '1',
        'user_from' => '6',
        'user_to' => '8',
        'created' => '0000-00-00 00:00:00',
        'status' => '1'
    ),
    'UserFrom' => array(
        'password' => '*****',
        'id' => '6',
        'username' => 'driz',
        'email' => 'cameron@driz.co.uk',
        'status' => '1',
        'code' => '6d446abefc2f1214e561da6f93470621',
        'lastlogin' => '2012-04-23 15:22:04',
        'created' => '0000-00-00 00:00:00'
    ),
    'UserTo' => array(
        'password' => '*****',
        'id' => '8',
        'username' => 'testperson',
        'email' => 'test@testperson.com',
        'status' => '1',
        'code' => '7a794859b3a16dfc005dd7f80b9ac030',
        'lastlogin' => '2012-03-18 09:28:24',
        'created' => '0000-00-00 00:00:00'
    )
)

所以这显然是链接用户的问题,我还需要包含配置文件......但是我该怎么做,因为如果我在 UserTo 和 UserFrom 中放置一个包含它会抛出一个错误,说它们没有关联......

4

1 回答 1

0
class User extends AppModel {
    public $name = 'User';

    public $hasOne = 'Profile';
    public $belongsTo = array(
      'Friend'=>array(
        'foreignKey' => 'user_to'
      )
    );
}

class Profile extends AppModel {
    public $name = 'Profile';
    public $belongsTo = 'User';
}

class Friend extends AppModel {
    public $name = 'Friend';
    public $hasOne = 'User';
}

现在可以通过以下方式获取用户朋友:

$this->Friend->find("all", array(
  'conditions'=>array(
    'Friend.user_from'=>$user_id,
  )
  'contain'=>array(
    'User'=>array(
      'contain'=>'Profile'
    )
  )
));
于 2012-04-26T20:54:59.647 回答