蛋糕PHP不理解请求对象


CakePHP Not understanding the request object

我想编写一些我认为相当简单的东西:接受用户对字段的输入,然后使用该值更新记录数组。我想由于我对请求对象的了解很少,我磕磕绊绊。我的索引视图中有一个表单

<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() 中的数据集中返回的记录。尽管我尽了最大的努力来寻找线索,但我还没有发现我是如何做到这一点的。我的猜测是,对于更有经验的开发人员来说,这太明显了,以至于他们发现没有必要写它。

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

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

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

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

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

显然,您不需要提交值,但您可以使用其他数据检索id1 的正确发票,并使用purchaseOrderNumber 3 对其进行更新。

理论更新将像这样构建:

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

与此类似,更逐字,等效:

$update = array(
  'Invoice' => array(
    'id' => 1,
    'purchaseOrderNumber' => 3
  )
);
$this->Invoice->save($update);

通过提供id和其他数据,Cake"知道"使用UPDATE而不是执行常规的INSERT。

上面的代码来自内存,可能包含错误,但它应该为你指明正确的方向,希望如此。