Codeigniter - Ошибка - Нет данных для обновленияPhp

Кемеровские программисты php общаются здесь
Ответить
Anonymous
 Codeigniter - Ошибка - Нет данных для обновления

Сообщение Anonymous »

Я хотел обновить свою базу данных, но получил сообщение об ошибке «нет данных для обновления». Вот мой скрипт;
Я создал простой переключатель для обновления базы данных. Переключатель делает пользователя активным (is_active=1) или неактивным (is_active=0). Проблема, с которой я сталкиваюсь, заключается в том, что, хотя объект изменяется с 1 на 0 или с 0 на 1, когда я передаю его модели, он возвращается с ошибкой «Нет данных для обновления». Способ заключается в следующем:
namespace App\Controllers\Admin;

use App\Entities\User;

class Users extends \App\Controllers\BaseController
{
private $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


Подробнее здесь: https://stackoverflow.com/questions/674 ... -to-update
Ответить

Быстрый ответ

Изменение регистра текста: 
Смайлики
:) :( :oops: :roll: :wink: :muza: :clever: :sorry: :angel: :read: *x)
Ещё смайлики…
   
К этому ответу прикреплено по крайней мере одно вложение.

Если вы не хотите добавлять вложения, оставьте поля пустыми.

Максимально разрешённый размер вложения: 15 МБ.

Вернуться в «Php»