这是我第一次在CI 4中使用模型。下面的模型在获取数据时工作,也更新一个删除的记录。但由于某种原因,它不验证任何输入数据。我可以让字段为空或用单个字符填充它们,它们仍然会更新。同样,当它们更新$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);
下面是表格:
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
)
期待您的反馈!
1条答案
按热度按时间tvokkenx1#
正如@steven7mwesigwa细心指出的那样,控制器中有一个错字,调用了错误的(?)型号。这解决了一切!
我正在调用GroupModel,但模型名为GroupsModel。