簡體   English   中英

使用來自CakePHP的數據hasAndBelongsToMany關系

[英]Using data from CakePHP hasAndBelongsToMany relationship

我有一個CakePHP應用程序,允許用戶與其他用戶成為朋友。 這是通過用於聯接的Friends表在users表上使用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]

有什么想法我做錯了嗎? 我認為這與未在視圖中正確使用數組有關。

我認為您應該嘗試一下:

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM