使用CakePHP在模型方法中使用其他模型的条件

时间:2012-04-13 13:05:19

标签: php cakephp

在我的应用中,我有三张桌子:

Users
Profiles
Friends

用户有一个个人资料,个人资料有一个用户并且有很多朋友。朋友有很多个人档案。

在Profile模型中,我有一个方法可以获取配置文件的配置文件列表(但它使用来自用户表的相关用户名。)

public function getFriends($username) {

   return $this->find('all', array(
     'conditions' => array(
       'User.username' => $username,
        'Friend.status' => 1
      )
   ));

}

所以它应该是一个匹配的用户名匹配用户和好友状态的配置文件列表1.我怎么做呢?

我还会发布我的关联,以便您了解数据库:

用户模型:

public $hasOne = 'Profile';

个人资料模型:

public $belongsTo = 'User';

public $hasMany = array(
    'ProfileFrom'=>array(
        'className'=>'Friend',
        'foreignKey'=>'profile_from'
    ),
    'ProfileTo'=>array(
        'className'=>'Friend',
        'foreignKey'=>'profile_to'
    )
);

public $hasAndBelongsToMany = array(
    'Friendship' => array(
        'className' => 'Profile',
        'joinTable' => 'friends',
        'foreignKey' => 'profile_from',
        'associationForeignKey' => 'profile_to'
    )
);

public $actsAs = array('Containable');

朋友模特:

public $belongsTo = array(
      'UserFrom'=>array(
         'className'=>'Profile',
         'foreignKey'=>'profile_from'
      ),
      'UserTo'=>array(
         'className'=>'Profile',
         'foreignKey'=>'profile_to'
      )
   );

public $actsAs = array('Containable');

1 个答案:

答案 0 :(得分:0)

这看起来很复杂,我建议看一下Containable(一个核心的cakephp行为),它可以更容易地选择要获取的数据(特别是在防止选择太多数据方面)。请考虑将其用于整个应用程序。

看起来你正试图告诉蛋糕在相关的朋友模型中搜索,但如果我是对的,那么现有的唯一关联名称为ProfileTo和ProfileFrom。所以我猜蛋糕不会知道在哪里欺骗'朋友'。但是将来发布任何错误都会有所帮助。

请参阅下面的建议,了解可能的解决方法。


现在您可以执行以下操作: 在ProfileModel中:

function getFriends($username) {
    //Start by getting User's ID
    $user = $this->User->find(
        'first',
        array(
            'conditions' => array(
                'User.username' => $username
            )
        )
    );
    //User's ID is now $user['User']['id']

    //Fetch user's profile with associated friends
    $this->Behaviors->attach('Containable');
    $profileAndFriends = $this->find(
        'first',  //Because User hasOne Profile, so we'll fetch this one profile.
        array(
            'conditions' => array(
                'Profile.user_id' => $user['User']['id']
            ),
            //We'll want to fetch all associated Friend records (which are linked to the Profile model as ProfileFrom and ProfileTo
            'contain' => array(
                'ProfileFrom' => array(
                    'conditions' => array(
                        'status' => 1
                    )
                ),
                'ProfileTo' => array(
                    'conditions' => array(
                        'status' => 1
                    )
                )
            )
        )
    );
    return $profileAndFriends;
}

它应该像这样工作。所以在FriendsController中你现在应该可以做$this->Friend->UserTo->getFriends('cameron'); 这应该使用用户名'cameron'以及与此个人资料相关联的所有朋友来获取属于该用户的个人资料。


但是有一件小事仍然困扰着我,这就是你得到一个错误,说明ProfileTo与Profile无关,这很奇怪,因为在你的例子中明显定义了关系。 原因可能是你们协会定义的错误,但我不确定。


还有一个建议,如果上面的代码不起作用,请将contain数组替换为:

            'contain' => array(
                'Friendship' => array(
                    'conditions' => array(
                        'OR' => array(
                            'Friendship.profile_from' => 'Profile.id',
                            'Friendship.profile_to' => 'Profile.id',
                        )
                    )
                )
            )
相关问题