错误:调用未定义的方法CodeIgniter \ Database \ MySQLi \ Result :: paginate() [英] Error: Call to undefined method CodeIgniter\Database\MySQLi\Result::paginate()
问题描述
在CI4中苦苦挣扎环绕 paginate
.用户指南中的所有示例均显示-> paginate()
方法直接链接到Controller中的 $ model
变量(不带参数).
Struggling to wrap my head around paginate
in CI4. All examples in the user guide shows the ->paginate()
method linked directly onto the $model
variable (without arguments) in the Controller.
我所有的查询都在模型中,所以如果 paginate
方法进入查询的末尾或在Controller中调用的地方,我会感到有些困惑.我都尝试过-都行不通.
I have all my queries in the Model, so I'm a bit confused if the paginate
method goes onto the end of my query there, or where it's called in the Controller. I've tried both - neither work.
型号:
public function brand_name($brand_name_slug)
{
return $this
->db
->table('shop a')
->select()
->join('(SELECT sku, MIN(sale_price) AS sale_price FROM shop GROUP BY sku) AS b', 'a.sku = b.sku AND a.sale_price = b.sale_price')
->where('availability', 'in stock')
->where('a.sku !=', '')
->where('brand_name_slug', $brand_name_slug)
->groupBy('a.sku')
->orderBy('brand_name, subbrand_name, product, size, unit')
->get()
->paginate(10) // not working
->getResult();
}
它返回错误:对未定义方法CodeIgniter \ Database \ MySQLi \ Result :: paginate()
我的控制器:
class Shop extends Controller
{
public function brand_name($brand_name_slug)
{
$model = new ShopModel();
$data = [
'category_menu' => $model->category_menu(),
'brand_menu' => $model->brand_menu(),
'nav' => $model->nav(),
'subnav' => $model->subnav(),
'shop' => $model->brand_name($brand_name_slug)->paginate(10), // also doesn't work
'pager' => $model->pager
];
if (empty($data['shop']))
{
throw new \CodeIgniter\Exceptions\PageNotFoundException('Cannot find the news item: '. $slug);
}
echo view('templates/header', $data);
echo view('shop/view', $data);
echo view('templates/footer', $data);
}
返回错误:在数组上调用成员函数paginate()
任何人都可以给我一些导航的指示吗?
Can anyone give me any pointers to navigating around this?
推荐答案
您必须在没有 get()
或 getResult()的情况下使用
,请注意,它是您的自定义模型所继承的Model类中的一个函数.因此,您应该直接从模型中调用它,而不是从 pagination()
函数 $ this-> db
中调用它(这就是为什么他不知道它的原因).
You got to use pagination()
function without get()
or getResult()
and note that it is a function from the Model class your custom model extend from. So you should call it directly from the model, not from $this->db
(this is why he doesn't know it).
这应该是您模型中的函数:
This should be the function in your model :
public function brand_name($brand_name_slug)
{
return $this
->table('shop a')
->select()
->join('(SELECT sku, MIN(sale_price) AS sale_price FROM shop GROUP BY sku) AS b', 'a.sku = b.sku AND a.sale_price = b.sale_price')
->where('availability', 'in stock')
->where('a.sku !=', '')
->where('brand_name_slug', $brand_name_slug)
->groupBy('a.sku')
->orderBy('brand_name, subbrand_name, product, size, unit')
->paginate(10);
}
和 $ data
在您的控制器中:
$data = [
'category_menu' => $model->category_menu(),
'brand_menu' => $model->brand_menu(),
'nav' => $model->nav(),
'subnav' => $model->subnav(),
'shop' => $model->brand_name($brand_name_slug),
'pager' => $model->pager
];
这篇关于错误:调用未定义的方法CodeIgniter \ Database \ MySQLi \ Result :: paginate()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!