2012-05-04 4 views
2

私はこの問題を全日で解消しようとしています。他の質問からいくつかのことを試してみましたが、それは実際に私の状況に適用されなかったか、うまくいかなかったかのいずれかです。Cakephp関連するモデルデータを保存して、1つのフィールドで空白レコードを作成する

ユーザー =(ID、名前、ユーザ名、パスワード、役割、last_edit、言語)
french_translations =(ID、french_clinical_recommendations、french_tradenames、include_drug、french_category、drug_id、user_idの)

:私は2つのテーブルを持っています

ユーザーhasMany french_translationsおよびfrench_translations belongsTo User。

ユーザーがfrench_translationを追加または編集すると、ユーザーIDをfrench_translationsテーブル、そのレコードのuser_idフィールドに保存してから、薬剤の総称名をusersテーブルのlast_editフィールドに保存します。今は各テーブルに新しいレコードを作成します。ユーザーテーブルでは、IDとlast_editフィールド(フィールドに正しい薬物名が表示されます)を除いて、すべてが空白です。また、french_translationsテーブルにはブランクのレコードがあり、user_idはusersテーブルで作成された空白と同じです。

コントローラー:

function add($id = null) { 
    $userid = $session->read('Auth.User.id'); 
    $drug = $this->FrenchTranslation->Drug->read(
     array(
      'Drug.id','Drug.generic','Drug.ahl','Drug.aap','Drug.rid','Drug.oral','Drug.mw','Drug.clinical_recommendations', 
      'Drug.category','Drug.lrc' 
    ), 
     $id 
    ); 
    $this->set('user',$userid); 
    $this->set('drug',$drug); 

    if (!empty($this->data)) { 
     $french_translation['FrenchTranslation']['id'] = $this->Session->read('id'); 
      $this->FrenchTranslation->create(); 
      if ($this->FrenchTranslation->save($this->data)) { 
       $this->Session->setFlash(__('The french translation has been saved', true)); 
       $this->redirect(array('controller'=>'drugs','action' => 'index')); 
      } else { 
       $this->Session->setFlash(__('The french translation could not be saved. Please, try again.', true)); 
      } 
     } 
     $drugs = $this->FrenchTranslation->Drug->find('list'); 
     $this->set(compact('drugs')); 
    } 

    function edit($id = null) { 
    //$this->FrenchTranslation->id = $id; 
    $userid = $this->Auth->user('id'); 
    $username = $this->Auth->user('name'); 
    //$this->FrenchTranslation->user_id = $id; 
    $drug = $this->FrenchTranslation->Drug->read(
     array(
      'Drug.id','Drug.generic','Drug.ahl','Drug.aap','Drug.rid','Drug.oral','Drug.mw','Drug.clinical_recommendations', 
      'Drug.category','Drug.lrc' 
    ), 
     $id 
    ); 

    $this->set('drug',$drug); 
    $this->set('user',$userid); 
    $this->set('username',$username); 

     if (!$id && empty($this->data)) { 
      $this->Session->setFlash(__('Invalid french translation', true)); 
      $this->redirect(array('action' => 'index')); 
     } 
     if (!empty($this->data)) { 
      if ($this->FrenchTranslation->saveAll($this->data)) { 
       $this->Session->setFlash(__('The french translation has been saved', true)); 
       $this->redirect(array('controller'=>'drugs','action' => 'index')); 
      } else { 
       $this->Session->setFlash(__('The french translation could not be saved. Please, try again.', true)); 
      } 
     } 
     if (empty($this->data)) { 
      $this->data = $this->FrenchTranslation->read(null, $id); 
     } 
     $drugs = $this->FrenchTranslation->Drug->find('list'); 
     $this->set(compact('drugs')); 
    } 

    function delete($id = null) { 
     if (!$id) { 
      $this->Session->setFlash(__('Invalid id for french translation', true)); 
      $this->redirect(array('action'=>'index')); 
     } 
     if ($this->FrenchTranslation->delete($id)) { 
      $this->Session->setFlash(__('French translation deleted', true)); 
      $this->redirect(array('action'=>'index')); 
     } 
     $this->Session->setFlash(__('French translation was not deleted', true)); 
     $this->redirect(array('action' => 'index')); 
    } 

編集ビュー:それを行うのより良い、より安全な、方法がありますように

<?php echo $this->Form->input('User.last_edit',array('type'=>'hidden','value'=>$drug['Drug']['generic'])); ?>  
    <?php echo $this->Form->input('user_id', array('type'=>'hidden','value'=>$user)); ?> 
+0

は、それが正しくuser_idのフィールドを保存しますが、ユーザーがテーブル内の対応するジェネリック薬の名前はありません。 – Jonathan

+0

本当ですか?誰も?私はちょっと進歩しました。ユーザーIDをfrench_translationテーブルのuser_idフィールドに入れますが、まだ新しいユーザーが作成されています。 – Jonathan

+0

私はそれを削除しない限り、私は同様の問題に直面している、(のみ)1つの空のユーザーがユーザーのテーブルに作成されます。私は問題がどこにあるのか理解できません。テストするユーザーに関連するモデルが多すぎます(コントローラなので)。何か案は? – Fr0zenFyr

答えて

0

私はそれを考え出したが、しかし、私は感じています。私はセキュリティコンポーネントを使用しています。サイトのいずれかを見るためにログインする必要があるので、おそらく十分安全です。

ユーザーIDにuser.id入力を追加するだけで、その時点でlast_editを格納する適切なユーザーを見つけることができます。コントローラには、現在のユーザIDを$userid = $this->Auth->user('id');で取得する$ useridという変数があり、$ths->set('user',$userid);を使用してビューに渡されました。私は、同じuser id値をdrug user_idフィールドの入力に使用しました。

ビュー:

<?php echo $this->Form->input('User.id',array('type'=>'hidden','value'=>$user));//The user ID last edit will be stored in ?> 
<?php echo $this->Form->input('User.last_edit',array('type'=>'hidden','value'=>$drug['Drug']['generic']));//The drug edited last by the specified user ?> 
1

これは後半の答えのビットですが、あなたはuser_idのようなセキュアな変数を設定するには、ビューを使用しないでください。代わりに、あなたはあなたのコントローラでこれらの変数を設定することができます。私はSAVEALLを保存するために変更すると

$this->request->data['User']['last_edit'] = $this->request->data['Drug']['generic']; 
関連する問題