mysqli_sql_exception 使用 findAll() 时 codeigniter 中的未知列

mysqli_sql_exception unknown column in codeigniter when using findAll()

我正在学习 CodeIgniter4,但在使用函数 findAll() 时卡住了,它是这样说的:mysqli_sql_exception #1054 'where clause'

中的未知列 'cursos.deleted_at'
<?php namespace App\Models;

use CodeIgniter\Model;

class Codigofacilito_model extends Model
{
  protected $table      = 'cursos';
  protected $primaryKey = 'idCurso';

  protected $returnType     = 'array';
  protected $useSoftDeletes = true;

  protected $allowedFields = ['nombreCurso','videosCurso'];

  protected $useTimestamps = false;
  protected $createdField  = 'created_at';
  protected $updatedField  = 'updated_at';
  protected $deletedField  = 'deleted_at';

  protected $validationRules    = [];
  protected $validationMessages = [];
  protected $skipValidation     = false;

  function __construct()
  {
    if (is_callable('parent::__construct')) {
      parent::__construct();
    }
  }

  function crearCurso($arr)
  {
    $this->insert
    (array(
        'nombreCurso' => $arr['nombre'],
        'videosCurso' => $arr['videos']
      )
    );
  }
}

控制器:

<?php namespace App\Controllers;

use App\Models\codigofacilito_model;

class Cursos extends BaseController{
  function __construct(){
    if (is_callable('parent::__construct')) {
      parent::__construct();
    }
    helper('form');
  }

  function index(){
    $modelo1=new Codigofacilito_model($db);
    $data=$modelo1->findAll();
    echo view('codigofacilito/headers');
    echo view('cursos/cursos',$data);
  }

}

连接正确,所有 table 名称和其他都正确。

如错误所述,您缺少 cursos table 中的 deleted_at 列。

在这里,您告诉了 Codeigniter 两件主要的事情:

  • 对于 protected $useSoftDeletes = true; 你告诉他:我不想使用 SQL 删除语句,而是更新 deleted_at 字段
    请注意,deleted_at 可以是日期格式(您可以在模型中使用 $dateFormat 参数指定的格式)或 整数。
  • 使用 protected $deletedField = 'deleted_at';,您正在设置将用于软删除的字段的名称。

调用 findAll() 方法时会发现您的错误,因为框架会使用您提供的字段名称将记录过滤为未软删除的记录。

因此,如果您想解决错误,请在 table 中添加 deleted_at 列,或者使用已存在的软删除列更改 $deletedField 值。

有关 CI4 模型配置的更多信息:https://codeigniter.com/user_guide/models/model.html#configuring-your-model

此外,由于您有 $useTimestamps = false,因此您不需要为 created_at 和 updated_at.

设置名称