我有一个输入字段,其中包含来自控制器(ControllerA)的值。现在我想只将输入字段保存到另一个表中。
ControllerA:
public function makeentry($id = null, $value){
//Here i get the correctly value in the input field
$controllerA = $this->ControllerA->get($id);
//Connect to the other table (?)
$controllerBTable = TableRegistry::get('ControllerB');
$controllerB = $controllerBTable->newEntity();
//Dosn't work here and no error message
if ($this->request->is('post')) {
$controllerB->name = $this->request->data;
if ($controllerBTable->save($controllerB)) {
$this->Flash->success(__('Saved.'));
//Go back to ConntrolerA
return $this->redirect(['action' => 'index']);
}else{
$this->Flash->error('Could not be saved.');
}
}
//set the value into the input-field
$this->set('controllerA', $controllerA);
}
}
在model->表中
表A:
public function initialize(array $config)
{
$this->table('tableA');
$this->displayField('name');
$this->primaryKey('id');
$this->belongsTo('TableB');
}
表B:
public function initialize(array $config)
{
$this->table('tableA');
$this->displayField('name');
$this->primaryKey('id');
$this->belongsTo('TableA');
}
有人可以用简单的单词或示例代码解释它如何实现它
答案 0 :(得分:1)
您可以按照以下步骤进行操作 保存表1中的数据
$saveData = $this->request->data; $this->loadModel('Table1'); $this->Table1->save($saveData);
同样,您可以在任何表中保存数据
$saveData = $this->request->data; $this->loadModel('Table2'); $this->Table2->save($saveData);
你也可以选择这个
$saveData = array(); $saveData['field_name_1'] = $this->data[$this->modelClass]['field_name_1']; $saveData['field_name_2'] = $this->data[$this->modelClass]['field_name_2']; $this->{$this->modelClass}->save($saveData);