cakephp saveAll根本不工作

时间:2010-09-04 08:28:26

标签: cakephp

所以我有一个Tarif,它有很多价格(每个不同的货币 - 并不重要)

视图(删除不相关的部分)

<?php echo $this->Form->create('Tarif');
  echo $this->Form->input("Price.0.price");
  echo $this->Form->input("Price.0.currency");
  echo $this->Form->input("Price.0.sms_format");
  echo $this->Form->input("Price.0.sms_number");
  echo $html->link(__('Add currency', true), '#', array('onclick'=>'return false;', 'class' => 'tarifs-add-currency'));

  echo $this->Form->input('Tarif.valid_since', array('timeFormat' => '24'));
  echo $this->Form->input('Tarif.valid_until', array('timeFormat' => '24'));
  echo $this->Form->input('Tarif.storage_time', array('label' => __('Storage time (days)', true)));
echo $this->Form->end(__('Submit', true));?>

保存它的控制器功能如下所示

function admin_add() {
  if (!empty($this->data)) {
    $this->Tarif->create();
    if ($this->Tarif->saveAll($this->data)) {
      $this->Session->setFlash(__('The tarif has been saved', true));
      $this->redirect(array('action' => 'admin_index'));
    } else {
      $this->Session->setFlash(__('The tarif could not be saved. Please, try again.', true));
    }
  }
}

“添加货币”链接用于为新价格添加新输入,但这不是问题,因为即使不添加货币也不起作用。 当我试图保存它时,它说'tarif无法保存。请再试一次。'。难道你不知道我应该改变什么才能让它发挥作用?

谢谢EL

1 个答案:

答案 0 :(得分:2)

对我来说,这看起来就像你有验证问题 - 检查你的模型中是否对表格中没有的字段有一些验证规则。例如,如果表中有一个字段user_id,并且如果使用bake控制台构建模型,则可以将此字段创建为数字。

这种方式即使它不是经过验证的形式,而且表单返回false。尝试打印$ this-&gt; YourModel-&gt; validationErrors并查看是否存在某些内容。

另一个提示 - 您是否在模型中使用beforeSave()和beforeValidate()之前的任何机会?如果是这样,他们需要返回 bool true ,否则save函数也会返回false并且它不会保存任何内容。

相关问题