CakePHP:将product_id值传递给checkout添加页面

时间:2013-02-26 19:58:07

标签: cakephp

我是蛋糕PHP的新手,我已经坚持了几天这个问题了。 “我的产品”索引视图显示我们在库存中的产品列表,并包含一个“结帐”操作,其中每个产品都指向“结帐/添加”视图。问题是需要检出的产品的product_id没有传递到添加检出页面,我无法弄清楚如何实现这一点。如果有人有任何建议,我可以真正使用一些帮助。

这是我的CheckoutController添加操作:

    public function add() {
    if ($this->request->is('post')) {
        $this->Checkout->create();
        if ($this->Checkout->save($this->request->data)) {
            $this->Session->setFlash(__('The checkout has been saved'));
            $this->redirect(array('action' => 'index'));
        } else {
            $this->Session->setFlash(__('The checkout could not be saved. Please, try again.'));
        }
    }
    $products = $this->Checkout->Product->find('list');
    $users = $this->Checkout->User->find('list');
    $this->set(compact('products', 'users'));
}

结帐添加视图

<?php echo $this->Form->create('Checkout');?>
<fieldset>
    <legend><?php echo __('Add Checkout'); ?></legend>
<?php
    echo $this->Form->input('product_id');
    echo $this->Form->input('start_time');
    echo $this->Form->input('end_time');
    echo $this->Form->input('user_id');
    echo $this->Form->input('description');
?>
</fieldset>

产品索引页面链接

    <?php echo $this->Html->link(__('Checkout'), array('controller' => 'Checkouts','action' => 'add', $product['Product']['id'])); ?>

2 个答案:

答案 0 :(得分:0)

来自产品索引页面上的链接似乎

public function add()

应该阅读

public function add($product_id)

此外,您应该将$product_id设置到视图中,还可以 将其填入输入框echo $this->Form->input('product_id', array('value'=>$product_id));

答案 1 :(得分:0)

Cake将传递product_id作为您行动的第一个参数;默认Cake'Routes'将匹配此URL;

/mycontroller/myaction/param1/param2/param3

采取此行动:

MycontrollerController::myaction(param1, param2, param3)

如果表单未发布,您可以通过向add()操作添加参数并将其添加到“request”来将此值传递给表单。像这样;

public function add($productId = null) {
    if ($this->request->is('post')) {
        $this->Checkout->create();
        if ($this->Checkout->save($this->request->data)) {
            $this->Session->setFlash(__('The checkout has been saved'));
            $this->redirect(array('action' => 'index'));
        } else {
            $this->Session->setFlash(__('The checkout could not be saved. Please, try again.'));
        }
    } else {
        $this->request->data['Checkout']['product_id'] = $productId;
    }

    $products = $this->Checkout->Product->find('list');
    $users = $this->Checkout->User->find('list');
    $this->set(compact('products', 'users'));
}

这将自动传播product_id下拉列表的“值”