从视图添加表单数据时,表单数据未传递给Controller

时间:2014-09-19 18:05:36

标签: cakephp cakephp-1.3

我正在尝试提交一堆从.csv文件导入的表单数据,并且在提交表单时我丢失了数据。

我在Chrome开发工具中查看了POST请求,并且数据在POST请求中,但如果我在控制器中debug($this->data);,则它是空的。

以下是一些相关代码:

admin_add_template.ctp(我的观点)

<?php for ($i=0; $i < $line - 1; $i++) : ?>

  <?php echo $this->Form->create('ActiveQuoteItem');?>
  <fieldset>
    <legend>Quote Template <?= $i; ?></legend>
    <!--                  <input type="hidden" name="xxxx" id="xxxx" value="xxxx" />-->
    <input name="quote_item_id" id="quote_item_id" value="<?php echo $out[$i][2] ; ?>" />
    <input name="estimated_cost" id="estimated_cost" value="<?php echo $out[$i][3] ; ?>" />
    <input name="actual_cost" id="actual_cost" value="<?php echo $out[$i][4] ; ?>" />
    <input name="billed_at" id="billed_at" value="<?php echo $out[$i][5] ; ?>" />
    <input name="inquiry_id" id="inquiry_id" value="<?php echo $the_id; ?>" />
    <input name="active" id="active" value="<?php echo $out[$i][7] ; ?>" />
    <input name="timeline" id="timeline" value="<?php echo $out[$i][8] ; ?>" />
    <input name="timeline_rushed" id="timeline_rushed" value="<?php echo $out[$i][9] ; ?>" />
    <input name="title" id="title" value="<?php echo $out[$i][10] ; ?>" />
    <input name="description" id="description" value="<?php echo $out[$i][11] ; ?>" />
    <input name="category" id="category" value="<?php echo $out[$i][12] ; ?>" />
    <input name="quantity" id="quantity" value="<?php echo $out[$i][13] ; ?>" />
  </fieldset>
<?php endfor; ?>

<?php echo $this->Form->end(__('Save', true)); ?>

active_quote_items_controller.php(我的控制器)

function admin_add_template() {
  if (!empty($this->data)) {
    $this->ActiveQuoteItem->create();
    if ($this->ActiveQuoteItem->saveAll($this->data['ActiveQuoteItem'])) {
      $this->Session->setFlash(__('The active quote item has been saved', true));
      $this->redirect(array('action'=>'index','inquiry_id'=>$this->data['ActiveQuoteItem']['inquiry_id']));
    } else {
      $this->Session->setFlash(__('The active quote item could not be saved. Please, try again.', true));
    }
  }
}

2 个答案:

答案 0 :(得分:0)

你说

$this->ActiveQuoteItem->saveAll($this->data['ActiveQuoteItem'])

但是您的表单在我看来它会提供与

对应的格式的数据
$this->ActiveQuoteItem->save($this->data)

答案 1 :(得分:0)

好的,问题是我的输入名称。

而不是像:

这样的输入

<input name="quote_item_id" id="quote_item_id" value="<?php echo $out[$i][2] ; ?>"

需要像:

<input type="hidden" name="data[<?php echo $i; ?>][quote_item_id]" id="quote_item_id" value="<?php echo $out[$i][2] ; ?>" />

换句话说,我忘了把它变成视图中的数据数组,所以当我试图在控制器中调试$this->data时,我没有得到任何东西。