cakephp 将数据保存到另一个 table
cakephp save data into another table
我有一个输入字段,其中包含来自控制器 (ControllerA) 的值。现在我只想将输入字段保存到另一个 table.
控制器A:
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);
}
}
模型中->table
表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');
}
谁能用简单的文字或示例代码解释一下如何实现它
您可以按照以下步骤操作
保存数据 table 1
$saveData = $this->request->data;
$this->loadModel('Table1');
$this->Table1->save($saveData);
同样,您可以将数据保存在任何 table
$saveData = $this->request->data;
$this->loadModel('Table2');
$this->Table2->save($saveData);
你也可以select这个
$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);
我有一个输入字段,其中包含来自控制器 (ControllerA) 的值。现在我只想将输入字段保存到另一个 table.
控制器A:
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);
}
}
模型中->table
表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');
}
谁能用简单的文字或示例代码解释一下如何实现它
您可以按照以下步骤操作
保存数据 table 1
$saveData = $this->request->data; $this->loadModel('Table1'); $this->Table1->save($saveData);
同样,您可以将数据保存在任何 table
$saveData = $this->request->data; $this->loadModel('Table2'); $this->Table2->save($saveData);
你也可以select这个
$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);