我有一个名为 Price.ctp 的视图,代码如下。它创建一个表单,该表单回显来自名为 $products 的变量的所有数据,该变量包含名为 Products 的表中的所有数据。
<?php echo $this->Form->create('Product', array('action' => 'changePrice')); ?>
<fieldset>
<h3>Products</h3>
<?php
foreach($products as $k=>$v){
echo $this->Form->hidden('id', array('value'=> $v["Product"]['id']));
echo $this->Form->input('name', array('value' => $v["Product"]["name"] ));
echo $this->Form->hidden('slug', array('value'=>$v["Product"]['slug']));
echo $this->Form->hidden('description', array('value'=>$v["Product"]['description']));
echo $this->Form->hidden('cateID', array('value'=>$v["Product"]['cateID']));
echo $this->Form->input('price', array('value' => $v["Product"]['price']));
echo $this->Form->hidden('photo', array('value'=>$v["Product"]['photo']));
echo $this->Form->hidden('photo_dir', array('value'=>$v["Product"]['photo_dir']));
echo $this->Form->hidden('active', array('value'=>$v["Product"]['active']));
echo $this->Form->hidden('views', array('value'=>$v["Product"]['views']));
echo $this->Form->hidden('created', array('value'=>$v["Product"]['created']));
echo $this->Form->hidden('modified', array('value'=>$v["Product"]['modified']));
}?>
</fieldset>
<?php echo $this->Form->end(__('Submit')); ?>
它发送到这个控制器方法,称为 changePrice
public function changePrice(){
$this->Product->saveMany($this->request->data['Product']);
$this->Session->setFlash( "Prices Saved.");
$this->redirect ( "/admin/products/" );
return;
}
但是,当我使用 debug() 检查 $this->request->data 的内容时,它显示仅发送视图中 foreach 循环的最终迭代。
换个说法,如果原始的 $products 变量(传递到视图价格.ctp)有 4 个产品:product1、product2、product3 和 product4,它们都有来自 Product 表的自己的数据(id、名称等),当在页面上按下提交按钮时,只有 product4 的变量会被传递到 $this->request->data 中。
为什么会这样?
干杯