CakePHP不了解请求对象

时间:2012-04-11 22:17:26

标签: php controller cakephp-2.0

我想编写一些我觉得相当简单的东西:接受字段上的用户输入,然后使用该值更新记录数组。我认为我因为我的最小化而陷入困境了解Request对象。我在索引视图中有一个表单

<div class="<?php echo $this->request->params['action']; ?>">

<?php
echo $this->Form->create('Invoice', array('action' => 'edit'));
echo $this->Form->input('id', array('type' => 'hidden'));
echo $this->Form->input('purchaseOrderNumber');
echo $this->Form->submit('Update Invoices', array('div' => false, 'name' => 'submit'));
?> 

<table>
    <tr>
        <th>Invoice Number</th>
        <th>Customer Name</th>
        <th>Invoice Date</th>
    </tr>

    <!-- Here is where we loop through our $invoices array, printing out invoice info -->

    <?php foreach ($invoices as $invoice): ?>
        <tr>        
            <td>
                <?php echo $this->Html->link($invoice['Invoice']['invoiceNumber'], array('action' => 'edit', $invoice['Invoice']['id'])); ?>
            </td>
            <td>
                <?php echo $invoice['Invoice']['customerName']; ?>
            </td>        
            <td>
                <?php echo $invoice['Invoice']['invoiceDate']; ?>
            </td>          
        </tr>
        <?php
    endforeach;
    echo $this->Form->end();
    ?>

</table>
</div>

足够简单。我想从 purchaseOrderNumber 中获取值,并使用它来更新在后续foreach()中的数据集中返回的记录。尽管我最好的网络搜索努力,但我还没有发现我是如何做到这一点的。我的猜测是,对于经验丰富的开发人员来说,他们发现没有必要写下它是如此明显。

任何帮助将不胜感激。如果您需要更多解释,请询问。

1 个答案:

答案 0 :(得分:1)

我不确定您对Request对象有什么不了解,但这是您可以做的。

提交发票表单后,您的edit方法可以使用表单数据。您可以在 InvoicesController 中使用$this->data(只读)或$this->request->data(可能会更改)来执行更新查询。

表单在$this->data中返回的数据具有以下结构:

array(
    'submit' => 'Update Invoices',
    'Invoice' => array(
        'id' => '1',
        'purchaseOrderNumber' => '3'
    )
)

显然,您不需要提交值,但您可以使用其他数据检索id 1 的正确发票,并使用{{1}更新 3

理论更新将如下构建:

purchaseOrderNumber

与此类似,更加逐字,相当于:

$this->Invoice->save($this->data['Invoice']);

通过提供$update = array( 'Invoice' => array( 'id' => 1, 'purchaseOrderNumber' => 3 ) ); $this->Invoice->save($update); 以及其他数据,Cake“知道”使用UPDATE而不是执行常规INSERT。

上面的代码来自内存并且可能包含错误,但它应该指向正确的方向。

相关问题