是否可以在codeigniter中做这样的事情:
publi function getcat($category_id)
{
$query = "(SELECT cat.id, cat.title ";
$query = $query . "FROM bf_categories cat ";
$query = $query . "WHERE cat.id=" . $category_id;
$this->db->limit(2, 4);
$records = $this->db->query($query);
return $records->result();
}
我已经简化了查询只是为了演示目的......但它实际上非常复杂,这就是我决定使用该query()
方法的原因。
但是查询中没有包含限制子句...我通过在控制器中启用 codeigniter 分析器进行了验证,我可以看到查询在没有任何限制子句的情况下运行。
您能告诉我如何使用该query()
方法完成此操作吗?
编辑 1
我已将模型修改为如下所示:
public function get_categories_and_products($limit=5, $offset=0, $category_id=null)
{
print "<BR>the function got the following offeset: $offset and limit: $limit";
$query = "(SELECT cat.category_id, cat.title, cat.image_thumb, cat.deleted, cat.display_weight ";
$query = $query."FROM bf_categories cat ";
$query = $query."WHERE cat.parent_id=".$category_id;
$query = $query." AND cat.category_id <>".$category_id;
$query = $query.") UNION (";
$query = $query."SELECT p.product_id, p.name, p.image_thumb, p.deleted , p.display_weight";
$query = $query." FROM bf_product p ";
$query = $query."Inner join bf_product_category cp ";
$query = $query."on p.product_id=cp.product_id ";
$query = $query."Where cp.category_id=".$category_id.") ?";
$records = $this->db->query($query,array($this->db->limit(2, 4)));
return $records->result();
}
我现在已经硬编码了限制值......但最终,我将使用传递给方法的值。不幸的是,这段代码仍然不起作用。根据分析器,这是正在执行的内容:
(SELECT cat.category_id, cat.title, cat.image_thumb, cat.deleted, cat.display_weight FROM bf_categories cat WHERE cat.parent_id=3 AND cat.category_id <>3) UNION (SELECT p.product_id, p.name, p.image_thumb, p.deleted , p.display_weight FROM bf_product p Inner join bf_product_category cp on p.product_id=cp.product_id Where cp.category_id=3)
SELECT * FROM (`bf_categories`) WHERE `category_id` = '3' LIMIT 4, 2
所以它创建了两个单独的选择语句。