0

我有一个 CakePHP 应用程序,它允许用户与其他用户成为朋友。这是使用用户表上的 HABTM 关系通过好友表进行连接来完成的。

朋友表有:id、user_id、friend_id 和状态字段。

关系模型如下所示:

public $hasAndBelongsToMany = array(
        'User'=>array(
            'className'              => 'User',
            'joinTable'              => 'friends',
            'with'                   => 'Friend',
            'foreignKey'             => 'user_id',
            'associationForeignKey'  => 'friend_id'
        )
    );

因此,例如,如果我想列出登录用户的朋友,我有以下内容:

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

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

    $this->set('friends', $friends);

现在我遇到的问题是在尝试显示此列表时,获取朋友信息!

如果我对 $friends 进行调试,我会得到:

array(
    'User' => array(
        'password' => '*****',
        'id' => '6',
        'username' => 'test',
        'email' => 'test@test.com',
        'firstname' => 'Test',
        'lastname' => 'User',
        (int) 0 => array(
            'password' => '*****',
            'id' => '8',
            'username' => 'johndoe',
            'email' => 'john@doe.com',
            'firstname' => 'John',
            'lastname' => 'Doe',
            'Friend' => array(
                'id' => '1',
                'user_id' => '6',
                'friend_id' => '8',
                'created' => '0000-00-00 00:00:00',
                'status' => '1'
            )
        )
    )
)

那么如何显示好友列表呢?正如我所做的那样:

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

        <li><?php echo $this->Html->link($friend['User']['firstname']. ' '. $friend['User']['lastname'], array('controller'=>'users','action'=>'view','userName'=>$friend['User']['username'])); ?></li>

    <?php endforeach; ?>

但我只是得到:Undefined index: User [APP/View/Friends/index.ctp, line 44]

任何想法我做错了什么?我假设这与视图中未正确使用数组有关。

4

1 回答 1

0

我认为你应该尝试一下:

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

你会:

array(
    'User' => array(
        'password' => '',
        'id' => '',
        'username' => '',
        'Friend' => array(
            0 => array(),
            1 => array(),
            2 => array()
        )
    )
)

在您看来:

<?php foreach ($friends['User']['Friend'] as $friend): ?>

    <li><?php echo $this->Html->link($friend['User']['firstname']. ' '. $friend['User']['lastname'], array('controller'=>'users','action'=>'view','userName'=>$friend['User']['username'])); ?></li>

<?php endforeach; ?>

希望这可以帮助。

编辑

使用可包含:

$this->User->Behaviors->load('Containable');

$this->User->contain('Friend' => array(
    'conditions' => array('Friend.status' => 1),
    'User'
));

$user = $this->User->read(null, $this->Auth->user('id'));

$this->User->Behaviors->unload('Containable');
于 2012-09-29T14:03:39.190 回答