使用CakePHP中的数据hasAndBelongsToMany关系

时间:2012-09-29 13:21:50

标签: php cakephp

我有一个CakePHP应用程序,允许用户与其他用户成为朋友。这是通过用户名表上的HABTM关系通过友情表进行的。

friends表有: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]

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

1 个答案:

答案 0 :(得分: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; ?>

希望这有帮助。

修改

使用Containable:

$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');