0

ログイン後に保存するユーザーデータの選択方法がわかりません。モデルの再帰性しか変更できないことに気づきましたが、使用する個々のフィールドを選択することはできません。

たとえば、通常、Cakephpは、パスワードを除くすべてのユーザーフィールドをセッションに保存します。必要のないデータや保存したくないデータも保存します。再帰を増やすと、Cakephpは関連するモデルのすべてのフィールドを保存します。

Model findメソッドの「fields」パラメーターについての方法はありますか?

ログイン後、見逃したデータを回復してセッションに追加し、すでに保存されているデータにマージできることは知っていますが、別のクエリを作成することは避け、より洗練された解決策が存在する場合はそれを見つけたいと思います。

ありがとう。

4

2 に答える 2

2

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オブジェクトを使用_findUserし、BaseAuthenticateクラスのほとんどのメソッドコードを使用します。これは、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')
          )
        )
      )
    )
  )
);
于 2012-10-24T14:55:09.357 に答える
0

私はこの問題にしばらく時間を費やしましたが、Cake2.6の時点で「userFields」オプションが実装されていることがわかりました。

こちらのドキュメントをご覧ください:http: //book.cakephp.org/2.0/en/core-libraries/components/authentication.html

于 2016-04-22T08:59:14.260 に答える