php activerecord 保存在 codeigniter 中不起作用

发布于 2024-12-14 11:46:07 字数 1232 浏览 3 评论 0原文

我使用最新的代码点火器(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() 解决了这个问题,但是当我取消注释代码中的注释时,问题又出现了。

现在的代码是:

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->routeid -= 1;) 是 echo 正确的,但我有与开始时相同的问题,两者都无法节省工作量。

I use the latest code igniter (2.0.3) and php-active 0.0.1.

All are working fine except save();

Code:

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

Here's my problem: for some reason that I cannot understand the above code does not work, but if I take the code out of if ($_POST), it works fine.

What I am doing wrong?


EDIT:

Thanks Damien Pirsy $this->input->post() does the trick, but when I uncomment the comments in the code the problems returns.

The code now is:

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();
    }
  }
  */
}

The elements new IDs ($element->routeid -= 1;) are echoing right, but I have the same problem as in the beginning and neither of two saves work.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

两人的回忆 2024-12-21 11:46:07

您没有提供太多详细信息或调试信息,所以我只是猜测:尝试使用 CI 的本机后处理程序。您应该对 $_POST 数组进行 var_dump() 编辑,看看是否 isset() ,因为您将其用作条件

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

更新:

由于我们讨论的是 Post 变量,因此不要假设它们与您想要的完全一样。请记住,当索引不存在时,$this->input->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. 进行同样的 检查。
是的,好吧,也许你可以编写比我的更干净的代码,但你明白了;)

You didn't provide much details or debug info, so I'll just guess: try using the CI's native post handler instead. You should have var_dump()ed the $_POST array, see if isset() or not, also, since you're using it as a condition

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

UPDATE:

Since we're talking about Post variables, don't assume they're exactly as you want them. Keep in mind that $this->input->post('field') returns FALSE when the index is not present; that might well brake your if condition.
Assuming you need numbers to do this, you can do a check like

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?
}

And the same for $oldRoute.
And yeah, OK, maybe you can write a cleaner code than mine, but you get the picture ;)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文