无法保存记录

时间:2012-05-09 06:26:18

标签: cakephp

我无法理解它不会更新或保存我放的任何记录。你能告诉我什么是错的吗?

这是我的StockController函数add

function add()
{
    if (!empty($this->data['Stocks']))
    {
        if ($this->Stocks->Save($this->data['Stocks']))
        {
            ($this->flash('Your Inventory has been added! /Stocks/'));
        }
    }
}

这是add.ctp

 <h1>Add Inventory</h1>    
<form action="<?php echo $this->Html->url("/Stocks/add"); ?>" method="post">    

    <p>    
        ProductName:      
        <?php echo $this->Form->varchar('Stocks/ProductName', array('size' => '40'))?>    
    </p>    
    <p>    
        Price:      
        <?php echo $this->Form->integers('Stocks/Price', array('size' => '10'))?>    
    </p>    
<p>
   Quantity:
   <?php echo $this->Form->integers('Stocks/Quantity', array('size' => '5'))?>
</p>
    <p>    
        <?php echo $this->Form->submit('Save') ?>    
    </p>  

</form>  

1 个答案:

答案 0 :(得分:4)

您没有提到您正在使用的CakePHP版本,并且您的示例中有相当多的非标准代码。

模型应该是单一的:

function add()
{
    if (!empty($this->data))
    {
        if ($this->Stock->Save($this->data))
        {
            ($this->flash('Your Inventory has been added! /Stocks/'));
        }
        else {
            debug($this->Stock->validationErrors);
        }
    }
}

(我添加的其他内容将显示任何验证错误)

您的表单字段名称格式不正确:

h1>Add Inventory</h1>    
  <?php echo $this->Form->create('Stock'); ?>

    <p>    
        ProductName:      
        <?php echo $this->Form->varchar('Stocks.product_name', array('size' => '40'))?>    
    </p>    
    <p>    
        Price:      
        <?php echo $this->Form->integers('Stock.price', array('size' => '10'))?>    
    </p>    
<p>
   Quantity:
   <?php echo $this->Form->integers('Stock.quantity', array('size' => '5'))?>
</p>
    <p>    
        <?php echo $this->Form->submit('Save') ?>    
    </p>  

<?php echo $this->Form->end('Submit'); ?>