无法在 codeigniter 4 中插入数据
cannot insert data in codeigniter 4
我想插入注册数据,按照现有教程操作,但是报错异常
$users = new UsersModel();
$data = [
'name' => $this->request->getPost('name'),
'email' => $this->request->getPost('email'),
'password' => password_hash($this->request->getPost('password'), PASSWORD_DEFAULT)
];
$users->insert($data);
我的模特:
<?php
namespace App\Models;
use CodeIgniter\Model;
class UsersModel extends Model
{
protected $table = "users";
protected $primarykey = "id";
protected $returnType = "object";
protected $useTimestamps = true;
protected $allowedFields = ['name', 'email', 'password'];
}
抛出异常:
mysqli_sql_exception #1054
Unknown column 'updated_at' in 'field list'
例外是
mysqli_sql_exception #1054
Unknown column 'updated_at' in 'field list'
这强烈表明它需要一个名为 updated_at.
的列
您已设置protected $useTimestamps = true;
所以根据文档:
$useTimestamps
This boolean value determines whether the current date is
automatically added to all inserts and updates. If true, will set the
current time in the format specified by $dateFormat. This requires
that the table have columns named ‘created_at’ and ‘updated_at’ in the
appropriate data type.
因此您需要创建列 created_at 和 updated_at
我想插入注册数据,按照现有教程操作,但是报错异常
$users = new UsersModel();
$data = [
'name' => $this->request->getPost('name'),
'email' => $this->request->getPost('email'),
'password' => password_hash($this->request->getPost('password'), PASSWORD_DEFAULT)
];
$users->insert($data);
我的模特:
<?php
namespace App\Models;
use CodeIgniter\Model;
class UsersModel extends Model
{
protected $table = "users";
protected $primarykey = "id";
protected $returnType = "object";
protected $useTimestamps = true;
protected $allowedFields = ['name', 'email', 'password'];
}
抛出异常:
mysqli_sql_exception #1054
Unknown column 'updated_at' in 'field list'
例外是
mysqli_sql_exception #1054
Unknown column 'updated_at' in 'field list'
这强烈表明它需要一个名为 updated_at.
的列您已设置protected $useTimestamps = true;
所以根据文档:
$useTimestamps
This boolean value determines whether the current date is automatically added to all inserts and updates. If true, will set the current time in the format specified by $dateFormat. This requires that the table have columns named ‘created_at’ and ‘updated_at’ in the appropriate data type.
因此您需要创建列 created_at 和 updated_at