0

私はcakephp 2.3.0を使用しています。しばらくマニュアルを調べたり、インターネットで検索したりしましたが、答えが見つかりませんでした。これが私の高レベルのシナリオです:

** 基本的に、オプション配列に変数と値を追加しようとしているだけなので、cakephp が SQL insert into ステートメントを作成するときに、この必要な列と値が含まれるようになります。**

  • ユーザーは、ユーザー名とパスワードを使用して Web ページにログインします
  • コントローラー内で、モデルを使用してユーザー テーブルにクエリを実行し、ユーザーの ID 番号を取得します。
  • ID # をセッション変数に入れました
  • 次に、ユーザーがログインし、Web フォームに入力して送信します。
  • 私のコントローラーでは、セッション変数に保存されているそのIDの値を使用して、モデルのオプション配列に追加しようとしています(これが私の問題の場所です)...以下の詳細を参照してください

私の UsersController には、次のコード スニペットがあります。

public function login() {
        if ($this->request->is('post')) {
            if ($this->Auth->login()) {                    
                $condition = array("User.username" => $this->request->data['User']['username']);
                $this->Session->write('userId', $this->User->find('first', 
                            array (
                            'conditions' => array ($condition),
                            'fields' => array ('User.id'))));                   
                $this->redirect($this->Auth->redirect());                   
            } else {
                $this->Session->setFlash(__('Invalid username or password, try again'));
            }
        }
    }

私のActivitiesControllerには、これがあります:

class ActivitiesController extends AppController {

    var $components = array('Session');

    public function createActivity() {
        if ($this->request->is('post')) {
              $this->Activity->create();
             $user = $this->Session->read('userId');
            //debug($user);
            debug($user['User']['id']);
            $ownerID = ($user['User']['id']);
            $this->Activity->set(array('owner_id' => $ownerID));
            if ($this->Activity->save($this->request->data)) {
                $this->Session->setFlash(__('The user has been saved'));
                $this->redirect(array('action' => 'home'));
            } else {
                $this->Session->setFlash(__('The user could not be saved. Please, try again.'));
            }
        }

私のデバッグでは、5 という値が出力されます。これは、私が期待する値です。しかし、私が得たのはデータベースエラーで、ウェブページに SQL INSERT INTO コードを出力し、私の owner_id を NULL にすることはできないと教えてくれます。それがDBテーブルでこれをセットアップする方法であるため、これは正しいです。

そのため、ID の値をセッション変数からオプション配列に入れようとすると、NULL が返されます。私はまだ自分自身を CakePHP の初心者だと考えています。どんな助けでも大歓迎です。ありがとう。

4

2 に答える 2

2

フレームワークがすでに行っていることを実行しようとしています。

ログイン機能はこれ以上必要ありません:

public function login() {
    if ($this->request->is('post')) {
        if($this->Auth->login()){
            $this->Session->setFlash('Your logged in now.');
            $this->redirect( array('controller' => 'activities', 'action' => 'createActivity'));
        }
        else{
            $this->Session->setFlash('Username or password is incorrect', 'default', array(), 'auth');
        }
    }
}

そしてあなたのアクティビティコントローラーで

public function createActivity() {
    if ($this->request->is('post')) {
        $this->request->data['Activity']['user_id'] = $this->Auth->user('id');
        $this->Activity->create();
        if ($this->Activity->save($this->request->data)) {
            $this->Session->setFlash(__('The user has been saved'));
            $this->redirect(array('action' => 'home'));
        } else {
            $this->Session->setFlash(__('The user could not be saved. Please, try again.'));
        }
    }

AppController には以下が含まれている必要があります

App::uses('Controller', 'Controller');

class AppController extends Controller {

    public $components = array(
        'Auth',
        'Session',
    );

}

また、Cake の規則に従うことをお勧めします。Activity を User にリンクする場合は、外部キー名を owner_id ではなく user_id に設定します モデルの関連付け

于 2013-03-18T08:44:43.070 に答える
1

$_SESSION を使用してユーザー情報を保存しないでください。Auth コンポーネントはすでにすべてを処理しています。$this->Auth->user('id');その変数を使用すると問題が解決する可能性があると思います。

ここの API ドキュメントを参照してください: http://api.cakephp.org/2.3/class-AuthComponent.html#method-AuthComponentuser

于 2013-03-18T00:53:36.017 に答える