在CI4中努力paginate
。用户指南中的所有示例都显示了直接链接到控制器中$model
变量(不带参数(的->paginate()
方法。
我的所有查询都在模型中,所以如果paginate
方法进入查询的末尾,或者在控制器中调用它的位置,我有点困惑。我都试过了 - 都不起作用。
型:
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();
}
它返回错误:Call to undefined method CodeIgniterDatabaseMySQLiResult::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 CodeIgniterExceptionsPageNotFoundException('Cannot find the news item: '. $slug);
}
echo view('templates/header', $data);
echo view('shop/view', $data);
echo view('templates/footer', $data);
}
返回错误:Call to a member function paginate() on array
谁能给我任何导航的指示?
您必须在没有get()
或getResult()
的情况下使用该函数pagination()
请注意,它是自定义模型扩展的模型类中的函数。所以你应该直接从模型中调用它,而不是从$this->db
调用它(这就是为什么他不知道它(。
这应该是模型中的函数:
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
];