1
大家好我試圖將信息添加到使用的hasMany與數據庫連接關聯CakePHP的提交陣列數據庫
Invoice - id, sender_id, receiver_id, template_id
Field - id, name, description, default_value, template_id
fields_invoices - id, invoice_id, field_id, entered_value
這裏是視圖
<?php echo $this->Form->create('FieldsInvoice'); ?>
<?php foreach ($fields as $field): ?>
<?php echo $this->Form->hidden($invoice_id); ?>
<?php echo $this->Form->hidden($field['Field']['id']); ?>
<?php echo $this->Form->Input($field['Field']['name'], array('default' =>$field['Field']['default_value'])); ?>
<?php endforeach ;?>
<?php echo $this->Form->End('Submit');?>
這裏控制器
public function create($id)
{
$this->set('title_for_layout', 'Create Invoice');
$this->set('stylesheet_used', 'homestyle');
$this->set('image_used', 'eBOXLogoHome.png');
$this->layout='home_layout';
if (!is_numeric($id)) throw new BadMethodCallException('I need an ID');
$this->Invoice->id = $id;
if (!$this->Invoice->exists()) throw new NotFoundException('Invalid ID');
$this->set('invoice_id',$id);
$names = $this->Invoice->find('list',array(
'fields'=>array('template_id'),
'conditions'=>array('id'=>$id)));
$fields = $this->Field->find('all', array(
'conditions'=>array(
'template_id'=>$names)));
$this->set(compact('fields'));
$this->set(compact('invoice_id'));
$this->set('name',$names);
$this->Invoice->create();
if(empty($this->data)){
$this->data= $this->Field->read($id);
}
else{
if($this->request->is('post'))
{
die(debug($this->data));
$this->Invoice->create();
if($this->FieldsInvoice->save($this->request->data, array('deep'=>true)));
{
$this->Session->setFlash('The field has been updated');
$this->redirect(array('controller'=>'invoices', 'action'=>'index'));
}
//else{
$this->Session->setFlash('Could not be saved');
//}
}
}
}
這裏是什麼打印使用調試時
\app\Controller\InvoicesController.php (line 134)
array(
'FieldsInvoice' => array(
(int) 87 => '',
(int) 0 => '',
'invoiceno' => 'test1',
(int) 99 => '',
'duedate' => 'test2',
(int) 999 => '',
'amount' => 'test3',
(int) 9999 => '',
'description' => 'test4'
)
)
所有正確的信息是有,但沒有提交到正確的地方,第一個INT是invoice_id
,其他其他INT的是field_id
和「測試1」,「測試2」,「TEST3」,「TEST4」是entered_value
。不知何故,我需要編碼我的視圖,以便它在同一個數組中保存invoice_id
,field_id,test1
,我該如何實現這個目標?
我不希望用戶觸摸INVOICE_ID或FIELD_ID – user1393064 2012-08-13 02:32:00
我也不想id來是顯而易見的用戶 – user1393064 2012-08-13 02:35:54
@ user1393064你確實意識到,通過查看源代碼它們仍然非常可見? – 2012-08-13 02:44:03