真的很难将模型信息保存到几个级别。
首先,我有一个费用索赔——有很多费用(费用属于费用索赔)。
费用 HABTM 费用代码。(这些是火车票价,汽油费等)。
所以,我有一个添加新费用页面 - (ExpenseClaimsController /add)。
这有点像电子表格,它允许您输入多行(每行都是费用)。在每一行上都有一个下拉菜单来选择费用代码。问题是费用代码没有写入连接表 (expenses_expense_codes)。
如果我通过未使用且即将删除的费用控制器 /add 添加单个费用,则它可以正常工作,因此问题是保存相关模型,模型。无法弄清楚如何做到这一点,因为费用代码与费用索赔模型没有直接关系,因此在我执行费用索赔控制器/添加时不保存。
希望这是有道理的……
这是我的 ExpenseClaimsController 代码:
public function add() {
if ($this->request->is('post')) {
//debug($this->request->data);
// Test
$this->loadModel('ExpenseCode');
// Create the claim and the expenses
$this->ExpenseClaim->create();
$this->ExpenseClaim->Expense->create();
// Set the user ID
$this->request->data['ExpenseClaim']['user_id'] = $this->Auth->user('id');
// Set the claim status based on which submit btn was pressed
if ($this->request->data['submit'] == 'Submit') {
$this->request->data['ExpenseClaim']['claim_status_id'] = '2';
} else {
$this->request->data['ExpenseClaim']['claim_status_id'] = '1';
}
// Set the claim status
$now = date('Y-m-d H:i:s');
$this->request->data['ExpenseClaim']['date_submitted'] = $now;
// Save both expenses and the expense claim
if ($this->ExpenseClaim->saveAll($this->request->data)) {
$this->Session->setFlash('The expense claim has been saved', 'flash_success');
$this->redirect(array('action' => 'index'));
} else {
$this->Session->setFlash('The expense claim could not be saved. Please, try again.', 'flash_failure');
}
}
$users = $this->ExpenseClaim->User->find('list');
$claimStatuses = $this->ExpenseClaim->ClaimStatus->find('list');
$expenseCodes = $this->ExpenseClaim->Expense->ExpenseCode->find('list');
$mileageRate = $this->Auth->user('mileage_rate');
$this->set(compact('users', 'claimStatuses', 'expenseCodes', 'mileageRate'));
// Set errors for top of page
$this->set('errors', $this->ExpenseClaim->validationErrors);
}
谁能指出我正确的方向?提前致谢。