0

我想做的是为用户提供一种从库存中结帐多种产品的方法。

我的产品索引页面(列出所有要签出的可用产品)如下所示:

<?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);
}

如您所见,我尝试添加我认为可能有效的帽子,但产品索引页面中的提交按钮没有任何作用。任何帮助将不胜感激!

4

1 回答 1

0

我只是检查了几次,至少我知道您失败的原因之一是在您的 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')); 

如果您没有错误或警告,我建议您检查

$this->Form->postLink

于 2013-03-21T17:33:41.997 回答