1

我对 cakePHP 很陌生,而且我已经被这个问题困住了几天了。我的产品索引视图显示我们在库存中的产品列表,并包含一个“结帐”操作,每个产品指向结帐/添加视图。问题是需要签出的产品中的 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'])); ?>
4

2 回答 2

0

Cake 会将 product_id 作为您操作的第一个参数传递;默认 Cake 'Routes' 将匹配此 url;

/mycontroller/myaction/param1/param2/param3

对于这个动作:

MycontrollerController::myaction(param1, param2, param3)

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

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 下拉列表的“值”

于 2013-02-26T23:08:22.500 回答
0

从您在产品索引页面上的链接看来

public function add()

应该读

public function add($product_id)

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

于 2013-02-26T22:59:27.843 回答