Codeigniter - 错误 - 没有要更新的数据
Codeigniter - Error - There is no data to update
我正要更新我的数据库,但出现错误“没有要更新的数据”。这是我的脚本;
我创建了一个简单的开关来向上更新数据库。切换使用户处于活动状态 (is_active=1) 或处于非活动状态 (is_active=0)。我遇到的问题是,虽然对象从 1 变为 0 或从 0 变为 1,但当我将它传递给模型时,它返回错误“没有要更新的数据”。方法如下;
命名空间App\Controllers\Admin;
使用App\Entities\User;
class 用户扩展 \App\Controllers\BaseController
{
私人 $model;
public function __construct()
{
$this->model = new \App\Models\UserModel;
}
public function toggle_user_is_active($id)
{
$users = $this->model->where('id', $id)->first(); // get the record
// if the current user is ACTIVE, then set it to DEACTIVE
if ($users->is_active == 1) {
$users->is_active = 0;
$this->model->save($users)); // gives an error, nothing to update
return redirect()->to('/Admin/Users/index')
->with('info', 'Success - User deactivated');
} else {
// if the current used is ACTIVE(1), change to INACTIVE(0)
$users->is_active = 1;
$this->model->save($users); // same error as above
return redirect()->to('/Admin/Users/index')
->with('info', 'Success - User Activated');
}
} // end method
}
真正奇怪的是,这是另一种方法的副本,可以完美运行,如下所示;
namespace App\Controllers\Admin;
use App\Entities\CategoryEntity;
use App\Entities\PostEntity;
class Post extends \App\Controllers\BaseController
{
private $model;
public function __construct()
{
$this->model = new \App\Models\PostModel;
$this->CategoryModel = new \App\Models\CategoryModel;
$auth = new \App\Libraries\Authentication;
$this->current_user = $auth->getCurrentUser();
}
public function toggle_post_is_published($post_id)
{
$post = $this->model->where('post_id', $post_id)->first();
// if the current post is PUBLISHED, then set it to UNPUBLISHED
if ($post->post_is_published == 1) {
echo
$post->post_is_published = 0;
$this->model->save($post);
return redirect()->to('/admin/post/post_index')
->with('info', 'Success - Post unpublished');
} else {
// if the current post is UNPUBLISHED, then set it to PUBLISHED
$post->post_is_published = 1;
$this->model->save($post);
return redirect()->to('/admin/post/post_index')
->with('info', 'Success - Post published');
}
}
} // end class
你声明你的模型了吗,因为看起来你使用 $this->model 但没有在你的构造函数或其他方法中设置你的模型。
我终于明白了。在我的 UserModel 中,我没有将 'is_active' 添加到受保护的 $allowedFields 。我没有将 'is_active' 添加到 allowedFields 并且它有效。
我正要更新我的数据库,但出现错误“没有要更新的数据”。这是我的脚本;
我创建了一个简单的开关来向上更新数据库。切换使用户处于活动状态 (is_active=1) 或处于非活动状态 (is_active=0)。我遇到的问题是,虽然对象从 1 变为 0 或从 0 变为 1,但当我将它传递给模型时,它返回错误“没有要更新的数据”。方法如下;
命名空间App\Controllers\Admin;
使用App\Entities\User;
class 用户扩展 \App\Controllers\BaseController { 私人 $model;
public function __construct()
{
$this->model = new \App\Models\UserModel;
}
public function toggle_user_is_active($id)
{
$users = $this->model->where('id', $id)->first(); // get the record
// if the current user is ACTIVE, then set it to DEACTIVE
if ($users->is_active == 1) {
$users->is_active = 0;
$this->model->save($users)); // gives an error, nothing to update
return redirect()->to('/Admin/Users/index')
->with('info', 'Success - User deactivated');
} else {
// if the current used is ACTIVE(1), change to INACTIVE(0)
$users->is_active = 1;
$this->model->save($users); // same error as above
return redirect()->to('/Admin/Users/index')
->with('info', 'Success - User Activated');
}
} // end method
}
真正奇怪的是,这是另一种方法的副本,可以完美运行,如下所示;
namespace App\Controllers\Admin;
use App\Entities\CategoryEntity;
use App\Entities\PostEntity;
class Post extends \App\Controllers\BaseController
{
private $model;
public function __construct()
{
$this->model = new \App\Models\PostModel;
$this->CategoryModel = new \App\Models\CategoryModel;
$auth = new \App\Libraries\Authentication;
$this->current_user = $auth->getCurrentUser();
}
public function toggle_post_is_published($post_id)
{
$post = $this->model->where('post_id', $post_id)->first();
// if the current post is PUBLISHED, then set it to UNPUBLISHED
if ($post->post_is_published == 1) {
echo
$post->post_is_published = 0;
$this->model->save($post);
return redirect()->to('/admin/post/post_index')
->with('info', 'Success - Post unpublished');
} else {
// if the current post is UNPUBLISHED, then set it to PUBLISHED
$post->post_is_published = 1;
$this->model->save($post);
return redirect()->to('/admin/post/post_index')
->with('info', 'Success - Post published');
}
}
} // end class
你声明你的模型了吗,因为看起来你使用 $this->model 但没有在你的构造函数或其他方法中设置你的模型。
我终于明白了。在我的 UserModel 中,我没有将 'is_active' 添加到受保护的 $allowedFields 。我没有将 'is_active' 添加到 allowedFields 并且它有效。