Cakephp将复选框数组传递给另一个控制器
问题描述:
我正在尝试的是为用户提供一种从库存中检出多个产品的方法。Cakephp将复选框数组传递给另一个控制器
我的产品索引页(列出所有可用的产品被检查出)看起来是这样的:
<?php echo $this->Form->create('multi');?>
<?php foreach ($products as $product): ?>
<tr class="hovertable">
//All the fields go here
<td style="cursor: default">
<?php echo $this->Html->link($this->Html->image('tr/Checkouts_Add.png') . " " . __('Checkout'), array('controller' => 'Checkouts','action' => 'add', $product['Product']['id']), array('escape' => false, 'class' => 'button')); ?>
<?php echo $this->Html->link($this->Html->image('tr/Edit.png'), array('action' => 'edit', $product['Product']['id']), array('escape' => false)); ?>
<?php echo $this->Form->postLink($this->Html->image('tr/Delete.png'), array('action' => 'delete', $product['Product']['id']), array('escape' => false), __('Are you sure you want to delete # %s?', $product['Product']['id'])); ?>
<?php echo $this->Form->input('Product.id.'.$product['Product']['id'] ,
array('label' => false,
'type' => 'checkbox',
'id'=>'listing_'.$product['Product']['id'])); ?>
</td>
</tr>
<?php endforeach; ?>
<?php echo $this->Form->submit(__('Submit'));?>
然后在我结账控制器我添加了一个新功能检出多个项目,我想此表由检查产品
public function multi($count = 1) {
if($this->request->is('post')) {
foreach($this->request->data['Checkout'] as $data) {
//Do not forget this line. you need to create new model for saving each time.
if ($this->request->isPost()) {
$this->Checkout->create();
$this->Checkout->save($data);
} else {
$this->request->data['Checkout']['product_id'] = $productId;
}
}
$this->redirect(array('action' => 'index'));
}
$products = $this->Checkout->Product->find('list');
$users = $this->Checkout->User->find('list');
$this->set(compact('products', 'users'));
$this->set('count', $count);
}
正如你看到的,我试图添加帽子我想可能会奏效,但在产品索引页什么也不做Submit按钮进行填充。任何帮助将不胜感激!
答
我只是检查几次,至少我明白你的失败的原因之一是你的foreach里面有一些error
或warning
,因为我不知道你的产品数组的价值,我检查这段代码和它的工作很适合我:
<?php $products=array('0'=>'11','1'=>'22','2'=>'333');
echo $this->Form->create('User');?>
<?php foreach ($products as $product): ?>
<tr class="hovertable">
//All the fields go here
<td style="cursor: default">
<?php echo $this->Form->input('Product.id.'.$product,
array('label' => false,
'type' => 'checkbox',
'id'=>'listing_'.$product)); ?>
</td>
</tr>
<?php
endforeach;
echo $this->Form->end(__('Submit'));
,如果您有任何错误或警告我建议你检查不
$这个 - >形式 - > postLink