Я должен был обновить свою базу данных, но я получаю сообщение об ошибке: нет данных для обновления. Вот мой сценарий;
Я создал простой переключатель для обновления базы данных. Переключатель делает пользователя активным (is_active=1) или неактивным (is_active=0). Проблема, с которой я сталкиваюсь, заключается в том, что хотя объект изменяется с 1 на 0 или с 0 на 1, когда я передаю его в модель, он возвращается с ошибкой «Нет данных для обновления». Метод заключается в следующем;
пространство имен App\Controllers\Admin;
используйте App\Entities\User;
Класс 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
$users
как ожидалось? Во 2-м контроллере вы настроили$this->model
, а в первом нет, или по крайней мере он здесь не показан. Так что же это? - person Don't Panic   schedule 09.05.2021