使用 CakePHP 在模型方法中使用来自另一个模型的条件


Using conditions from another model inside a model method using CakePHP

在我的应用程序中,我有三个表:

Users
Profiles
Friends
用户有一个个人资料

,个人资料有一个用户并有许多朋友。朋友有很多个人资料。

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

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

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

似乎您正在尝试告诉蛋糕在关联的朋友模型中搜索,但是如果我是对的,那么朋友存在的唯一关联别名为ProfileTo和ProfileFrom。所以我猜蛋糕不知道在哪里打"朋友"。但是将来发布任何错误也会有所帮助。

请参阅下面的建议以获取可能的解决方法。


现在,您可以执行以下操作:在配置文件模型中:

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