私はこの問題を全日で解消しようとしています。他の質問からいくつかのことを試してみましたが、それは実際に私の状況に適用されなかったか、うまくいかなかったかのいずれかです。Cakephp関連するモデルデータを保存して、1つのフィールドで空白レコードを作成する
ユーザー =(ID、名前、ユーザ名、パスワード、役割、last_edit、言語)
french_translations =(ID、french_clinical_recommendations、french_tradenames、include_drug、french_category、drug_id、user_idの)
ユーザー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)); ?>
は、それが正しくuser_idのフィールドを保存しますが、ユーザーがテーブル内の対応するジェネリック薬の名前はありません。 – Jonathan
本当ですか?誰も?私はちょっと進歩しました。ユーザーIDをfrench_translationテーブルのuser_idフィールドに入れますが、まだ新しいユーザーが作成されています。 – Jonathan
私はそれを削除しない限り、私は同様の問題に直面している、(のみ)1つの空のユーザーがユーザーのテーブルに作成されます。私は問題がどこにあるのか理解できません。テストするユーザーに関連するモデルが多すぎます(コントローラなので)。何か案は? – Fr0zenFyr