【问题标题】:php activerecord save does not work in codeigniterphp activerecord保存在codeigniter中不起作用
【发布时间】:2011-11-10 16:41:13
【问题描述】:

我使用最新的代码点火器 (2.0.3) 和 php-active 0.0.1。

除了save()之外,其他都工作正常;

代码:

if($_POST)
{               
  $entry= Customers::find_by_routeid('4');
  $entry->routeid=5;
  $entry->save();              
}

这是我的问题:由于某种我无法理解的原因,上面的代码不起作用,但是如果我将代码从if ($_POST) 中取出,它就可以正常工作。

我做错了什么?


编辑:

感谢 Damien Pirsy $this->input->post() 解决了这个问题,但是当我取消注释代码中的 cmets 时,问题又回来了。

现在的代码是:

if($this->input->post())
{
  $id = $this->input->post('id');
  $oldRoute = $this->input->post('oldRoute');
  $newRoute = $this->input->post('newRoute');

  $entry=  Customers::find_by_routeid($this->input->post('oldRoute'));
  $entry->routeid=$this->input->post('newRoute');
  $entry->save();

  /*
  if($oldRoute<$newRoute)
  {
    for ($i=$newRoute; $i>$oldRoute; $i--)
    {
      $element = Customers::find_by_routeid($i);
      echo $element->routeid -= 1;
      $element->save();
    }
  }
  */
}

元素新 ID ($element-&gt;routeid -= 1;) 是 echoing 对,但我遇到了与开始时相同的问题,两个保存都没有工作。

【问题讨论】:

    标签: php codeigniter activerecord save


    【解决方案1】:

    您没有提供太多细节或调试信息,所以我只是猜测:尝试改用 CI 的本机 post 处理程序。你应该有var_dump()ed $_POST 数组,看看是否 isset(),因为你使用它作为条件

    if($this->input->post())
    {
      //...
    }
    

    更新:

    既然我们谈论的是 Post 变量,请不要假设它们完全符合您的要求。请记住,当索引不存在时,$this-&gt;input-&gt;post('field') 返回 FALSE;这很可能会破坏您的 if 条件。 假设您需要数字来执行此操作,您可以进行如下检查

    if($this->input->post('newRoute') AND is_numeric($this->input->post('newRoute'))
    {
      $newRoute = $this->input->post('newRoute');
    }
    else
    {
      // give it a default value, or raise an error, for example. If you need this
      // variables, and need them to be numbers, you cannot go on in case these 
      // conditions are not met, right?
    }
    

    $oldRoute. 也是如此 是的,好吧,也许你可以编写比我更简洁的代码,但你明白了;)

    【讨论】:

    • 你之前可以这么说...请说得更详细一些,这样人们才能更好地帮助你!
    • $_POST 肯定不是空的...我添加了 echo 之后它正在回显...
    • 对不起,我认为这很明显,但给我分钟这个->输入->帖子似乎工作
    • 我已发布保存的上述代码(编辑部分)工作正常,但更新条目的行必须放在 for ...
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-05-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多