CodeIgniter 版本更新 - 查询生成器的 limit() 生成空数组

CodeIgniter version update - Query builder's limit() producing empty array

需要说明的是,我以前完全没有使用过 CodeIgniter。

被分配到一个旧网站,其中 CI 版本最近更新为最新版本,并且某些循环没有显示任何内容。注意到问题始于使用 limit() 函数的查询构建器行。

Home.php 控制器中,__construct() 函数执行:

$this->load->orm('db_et_comu_news', 'news');

在此之后,index() 函数有:

$this->_data['news'] = $this->news->where('status',1)->where('id_et_cat_comu_news',2)->order_by('data','DESC')->limit(7)->find_all();

这在过去的 CI 版本中有效,并返回了一个完美的结果数组,但是在更新之后,我得到的只是一个空数组。如果我从查询中删除 limit(),则如下所示:

$this->_data['news'] = $this->news->where('status',1)->where('id_et_cat_comu_news',2)->order_by('data','DESC')->find_all();

查询 returns 一个很好的健康数组,但没有根据需要将我的行限制为 7。

我已经检查了 CI 3.1.3 文档,似乎 函数被正确应用。谁能帮我找出问题所在?

用这个替换你的控制器代码。

<?php
defined('BASEPATH') OR exit('No direct script access allowed');

class Home extends CI_Controller
{
    function __construct() {
        parent::__construct();
    }

    function index()
    {
        $data['news'] = $this->news();

        if(!empty($data['news']))
        {
           foreach($data['news'] as $objects)
           {
           echo $objects->data;
           }
        }

    }

    function news()
    {
        $query = $this
                ->db
                ->select('*')
                ->from('db_et_comu_news')
                ->where('status',1)
                ->where('id_et_cat_comu_news',"2")
                ->order_by("data", "DESC")
                ->limit(7)
                ->get();
        if($query->num_rows()>0)
        {
            return $query->result();  // return an array of objects
        }
        else
        {
            return null;
        }
    }

}