php activerecord 保存在 codeigniter 中不起作用
我使用最新的代码点火器(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 echo
ing right, but I have the same problem as in the beginning and neither of two saves work.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您没有提供太多详细信息或调试信息,所以我只是猜测:尝试使用 CI 的本机后处理程序。您应该对
$_POST
数组进行var_dump()
编辑,看看是否 isset() ,因为您将其用作条件更新:
由于我们讨论的是 Post 变量,因此不要假设它们与您想要的完全一样。请记住,当索引不存在时,
$this->input->post('field')
返回FALSE;这很可能会阻止你的 if 条件。假设您需要数字来执行此操作,您可以执行类似的检查
,并且对
$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 conditionUPDATE:
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
And the same for
$oldRoute.
And yeah, OK, maybe you can write a cleaner code than mine, but you get the picture ;)