gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-05 03:47:43 26 4
gpt4 key购买 nike

我正在学习 CodeIgniter4,我在使用函数 findAll() 时卡住了,它说:mysqli_sql_exception #1054“where 子句”中的未知列“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']
)
);
}
}

Controller :

<?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);
}

}

连接正确,所有表名等都正确。

最佳答案

正如错误所说,您在 cursos 表中缺少 deleted_at 列。

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

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

当调用 findAll() 方法时,您的错误被捕获,因为框架将使用您提供给他的字段名称将记录过滤为那些未被软删除的记录。

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

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

此外,由于您拥有 $useTimestamps = false,因此无需为 created_at 和 updated_at 设置名称。

关于php - 使用 findAll() 时 codeigniter 中的 mysqli_sql_exception 未知列,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64764082/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com