我一直试图弄清楚这个问题整个上午。尝试了其他问题的一些事情,但它要么不适用于我的情况,要么不适用。我有两个表:Cakephp保存相关模型数据创建一个字段的空白记录
用户 =(ID,姓名,用户名,密码,角色,last_edit,语言)
french_translations =(ID,french_clinical_recommendations,french_tradenames,include_drug,french_category,drug_id,USER_ID)
用户有许多法语翻译和法语翻译belongsTo用户。
当用户添加或编辑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领域,但没有users表中有相应的通用药名。 – Jonathan
真的吗?没有人?我已经取得了一些进展,现在它将用户ID放入french_translation表的user_id字段中,但它仍然会创建一个新用户。 – Jonathan
我正面临类似的问题,除非我删除它,否则(仅)一个空白用户会在用户表中创建。我无法弄清楚问题在哪里,我有太多的模型(所以控制器)与用户相关的测试。有任何想法吗? – Fr0zenFyr