Session Cakephp:选择登录后要保存的数据

Session Cakephp:选择登录后要保存的数据,session,cakephp,login,Session,Cakephp,Login,我无法理解如何选择登录后要保存的用户数据。我注意到,我只能更改模型的递归性,但不能选择要使用的单个字段 例如,通常Cakephp会在会话中保存除密码以外的所有用户字段,甚至是我不需要和不想存储的数据。 如果我增加递归,Cakephp将保存相关模型的所有字段 对于模型查找方法的“字段”参数,有什么方法吗 我知道,登录后,我可以恢复丢失的数据,并将它们添加到会话中,合并到已存储的数据中,但我希望避免进行另一个查询,并找到一个更优雅的解决方案(如果存在的话) 谢谢。从Cake 2.2开始,您可以在身份

我无法理解如何选择登录后要保存的用户数据。我注意到,我只能更改模型的递归性,但不能选择要使用的单个字段

例如,通常Cakephp会在会话中保存除密码以外的所有用户字段,甚至是我不需要和不想存储的数据。 如果我增加递归,Cakephp将保存相关模型的所有字段

对于模型查找方法的“字段”参数,有什么方法吗

我知道,登录后,我可以恢复丢失的数据,并将它们添加到会话中,合并到已存储的数据中,但我希望避免进行另一个查询,并找到一个更优雅的解决方案(如果存在的话)


谢谢。

从Cake 2.2开始,您可以在身份验证选项中添加
contain
密钥以提取相关数据。由于
contain
键接受一个
fields
键,因此您可以限制那里的字段:

public $components = array(
  'Auth' => array(
    'authenticate' => array(
      'Form' => array(
        'contain' => array(
          'Profile' => array(
            'fields' => array('name', 'birthdate')
          )
        )
      )
    )
  )
);
如果要更改用户模型搜索的字段,可以扩展正在使用的身份验证对象。通常,users表包含的信息量很小,因此通常不需要这样做

不过,我还是要举个例子。我们将在这里使用FormAuthenticate对象,并使用BaseAuthenticate类中的大部分
\u findUser
方法代码。这是Cake的身份验证系统用来识别用户的功能

App::uses('FormAuthenticate', 'Controller/Component/Auth');
class MyFormAuthenticate extends FormAuthenticate {

  // overrides BaseAuthenticate::_findUser()
  protected function _findUser($username, $password) {
    $userModel = $this->settings['userModel'];
    list($plugin, $model) = pluginSplit($userModel);
    $fields = $this->settings['fields'];

    $conditions = array(
      $model . '.' . $fields['username'] => $username,
      $model . '.' . $fields['password'] => $this->_password($password),
    );
    if (!empty($this->settings['scope'])) {
      $conditions = array_merge($conditions, $this->settings['scope']);
    }
    $result = ClassRegistry::init($userModel)->find('first', array(
      // below is the only line added
      'fields' => $this->settings['findFields'],
      'conditions' => $conditions,
      'recursive' => (int)$this->settings['recursive']
    ));
    if (empty($result) || empty($result[$model])) {
      return false;
    }
    unset($result[$model][$fields['password']]);
    return $result[$model];
  }
}
然后使用该身份验证并通过我们的新设置:

public $components = array(
  'Auth' => array(
    'authenticate' => array(
      'MyForm' => array(
        'findFields' => array('username', 'email'),
        'contain' => array(
          'Profile' => array(
            'fields' => array('name', 'birthdate')
          )
        )
      )
    )
  )
);

我在这个问题上花了一段时间,结果发现在Cake 2.6中已经实现了一个“userFields”选项

请查看此处的文档:

您使用的是什么版本的蛋糕?