没有获得我在 CakePHP 应用程序中关联所需的所有数据


Not getting all the data I need for an association in CakePHP app

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

在我的 FriendsController 中,

我传递一个用户名,然后使用以下方法获取用户的 Friends: 请注意,此方法在我的用户模型中,调用如下: $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;
    }

我的用户模型:

类 用户扩展应用模型{ 公共$name ="用户";

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 中放置一个包含,它会抛出一个错误,说它们没有关联......

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