【问题标题】:Not getting all the data I need for an association in CakePHP app在 CakePHP 应用程序中没有获得关联所需的所有数据
【发布时间】:2012-04-26 20:17:49
【问题描述】:

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

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

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

【问题讨论】:

    标签: php cakephp


    【解决方案1】:
    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'
        )
      )
    ));
    

    【讨论】:

    • 不完全明白。你能发布我应该在每个模型中拥有的东西吗?谢谢。
    • 嘿,好吧,我稍微改变了我的文章,请阅读更多关于HABTM实现的内容,我用你的代码写了一些例子。希望它会有所帮助。
    • +1 如果您想要自定义 Friend 模型(即,status 字段),您应该使用带有“with”键的 HABTM。
    • 那么我如何获得好友呢?或者只是请求,因为您似乎已经将关系和查询组合在一起......
    • 试过这样做,但它根本不起作用,只是抛出很多 sql 错误,说事情没有关联等等。我的代码更接近,只是没有拉朋友的个人资料通过!
    猜你喜欢
    • 2017-09-27
    • 2016-05-21
    • 1970-01-01
    • 2021-06-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-05-16
    相关资源
    最近更新 更多