Codeigniter 4 模型不验证或更新 $updatedField
Codeigniter 4 model does not validate or updates $updatedField
这是我第一次在 CI4 中使用模型。以下模型在获取数据和更新切除记录时有效。但出于某种原因,它不验证任何输入数据。我可以将字段留空或用单个字符填充它们,它们仍会更新。此外,当他们更新时,$updatedField 不会更改(保持为 NULL)。
<?php
namespace App\Models;
use CodeIgniter\Model;
class GroupsModel extends Model
{
protected $DBGroup = 'default';
protected $table = 'auth_groups';
protected $primaryKey = 'id';
protected $useAutoIncrement = true;
protected $returnType = 'array';
protected $useSoftDeletes = true;
protected $allowedFields = ['name', 'description'];
protected $useTimestamps = true;
protected $createdField = 'created_at';
protected $updatedField = 'updated_at';
protected $deletedField = 'deleted_at';
protected $validationRules = [
'name' => 'required|min_length[3]|is_unique[auth_groups.name]',
'description' => 'required|min_length[3]',
];
protected $validationMessages = [
'name' => [
'is_unique' => 'Name has to be unique',
],
];
protected $skipValidation = false;
}
控制器中的相关代码如下:
$groupmodel=new GroupModel();
$group = $groupmodel->find($_GET["id"]);
if(is_null($group)){throw \CodeIgniter\Exceptions\PageNotFoundException::forPageNotFound();}
if($_SERVER['REQUEST_METHOD'] == 'POST'){$groupmodel->update($_GET["id"],$_POST);}
helper('form');
$data = [
'username' => user()->username,
'title' => 'Groups',
'group' => $group,
'errors' => $groupmodel->errors(),
];
echo view('panel/header', $data);
echo view('panel/admin/group_edit', $data);
echo view('panel/footer', $data);
这是table:
CREATE TABLE `auth_groups` (
`id` int UNSIGNED NOT NULL,
`name` varchar(255) NOT NULL,
`description` varchar(255) NOT NULL,
`created_at` datetime NOT NULL,
`updated_at` datetime DEFAULT NULL,
`deleted_at` datetime DEFAULT NULL
)
期待您的反馈!
正如 @steven7mwesigwa 所指出的那样,控制器中有一个拼写错误,调用了错误的 (?) 模型。这解决了一切!
我正在调用 GroupModel,但该模型称为 GroupsModel。
这是我第一次在 CI4 中使用模型。以下模型在获取数据和更新切除记录时有效。但出于某种原因,它不验证任何输入数据。我可以将字段留空或用单个字符填充它们,它们仍会更新。此外,当他们更新时,$updatedField 不会更改(保持为 NULL)。
<?php
namespace App\Models;
use CodeIgniter\Model;
class GroupsModel extends Model
{
protected $DBGroup = 'default';
protected $table = 'auth_groups';
protected $primaryKey = 'id';
protected $useAutoIncrement = true;
protected $returnType = 'array';
protected $useSoftDeletes = true;
protected $allowedFields = ['name', 'description'];
protected $useTimestamps = true;
protected $createdField = 'created_at';
protected $updatedField = 'updated_at';
protected $deletedField = 'deleted_at';
protected $validationRules = [
'name' => 'required|min_length[3]|is_unique[auth_groups.name]',
'description' => 'required|min_length[3]',
];
protected $validationMessages = [
'name' => [
'is_unique' => 'Name has to be unique',
],
];
protected $skipValidation = false;
}
控制器中的相关代码如下:
$groupmodel=new GroupModel();
$group = $groupmodel->find($_GET["id"]);
if(is_null($group)){throw \CodeIgniter\Exceptions\PageNotFoundException::forPageNotFound();}
if($_SERVER['REQUEST_METHOD'] == 'POST'){$groupmodel->update($_GET["id"],$_POST);}
helper('form');
$data = [
'username' => user()->username,
'title' => 'Groups',
'group' => $group,
'errors' => $groupmodel->errors(),
];
echo view('panel/header', $data);
echo view('panel/admin/group_edit', $data);
echo view('panel/footer', $data);
这是table:
CREATE TABLE `auth_groups` (
`id` int UNSIGNED NOT NULL,
`name` varchar(255) NOT NULL,
`description` varchar(255) NOT NULL,
`created_at` datetime NOT NULL,
`updated_at` datetime DEFAULT NULL,
`deleted_at` datetime DEFAULT NULL
)
期待您的反馈!
正如 @steven7mwesigwa 所指出的那样,控制器中有一个拼写错误,调用了错误的 (?) 模型。这解决了一切!
我正在调用 GroupModel,但该模型称为 GroupsModel。