opencart mysql优化_opencart之数据库查询便捷查询

本文介绍了如何通过 ocxdm.php 文件简化 Opencart 的 MySQL 数据库操作,包括添加、删除、更新和查询方法的使用示例,以及对 like、>、< 等条件的查询支持。
摘要由CSDN通过智能技术生成

本篇文章给大家分享一个opencart查询数据库简单方法,这个方法当然不是自带的啦,只需要添加一个文件(ocxdm.php)和一行代码就可以使用了。

首先将ocxdm.php 上传到系统类库目录下 system/library

edf3b11bcf90e8b9bd5cc253e6f24bc3.png

然后 打开 system 文件夹下的 framework.php文件,并在// Session之前加上下面代码,让opencart加在这个类。$registry->set('ocxdm',new ocxdm($registry));

2.使用方法

①.增加方法(所有表名无需添加前缀)$data['字段名'] = 对应的值;$this->ocxdm->table('表名')->add($data);$data['name'] = $this->request->post['name'];

$data['price'] = $this->request->post['price'];

$data['quantity'] = $this->request->post['quantity'];

$this->ocxdm->table('product')->add($data);

②.删除方法$data['字段名'] = 对应的值;//where条件数组$this->ocxdm->table('表名')->where($data)->delete();$data['product_id'] = 1;

$this->ocxdm->table('product')->where($data)->delete();

③.修改方法$map['字段名'] = 对应的值;//where条件数组$data['字段名'] = 对应的值;$map['product_id'] = $this->request->get['product_id'];

$data['name'] = $this->request->post['name'];

$data['price'] = $this->request->post['price'];

$data['quantity'] = $this->request->post['quantity'];

$this->ocxdm->table('product')->where($map)->update($data);

④.查询方法

普通查询

$filter_data['product_id'] = $product_id;//查询条件关于非相等条件的表达式 $filter_data['name'] = array('like','%'.$this->request->get[$filters].'%');更多表达式方法可在ocxdm.php文件里查看里查看    $results = $this->ocxdm->table(‘表名’)->where(‘条件’)->page('page',‘limit数’)->order("排序方式")->select();            //find()方法也可使用$filter_data['product_id'] = $product_id;

$results = $this->ocxdm->table('product')

->where($filter_data)

->page($sort_data['page'],$this->sort['limit'])

->order("date_added DESC")

->select();

左联表查询

$results = $this->ocxdm->table(‘表名’)->join('表名','左联条件')->where(‘条件’)->page('page',‘limit数’)->order("排序方式")->select();   //find()方法也可使用$filter_data['product_id'] = $product_id;

$results = $this->ocxdm->table('product')

->where($filter_data)

->join('product_description','product.product_id = product_description.product_id')

->page($sort_data['page'],$this->sort['limit'])

->order("date_added DESC")

->select();

like,>,< 查询$filter_data['字段名'] = array('条件符号',变量);$results = $this->ocxdm->table(‘表名’)->where(‘条件’)->page('page',‘limit数’)->order("排序方式")->select();like 查询

$filter_data['name'] = array('like','%'.$filter_name.'%')

$results = $this->ocxdm->table('product')

->where($filter_data)

->page($sort_data['page'],$this->sort['limit'])

->order("date_added DESC")

->select();

'>' 查询

$filter_data['date_time'] = array('egt',$filter_date_name)

$results = $this->ocxdm->table('product')

->where($filter_data)

->page($sort_data['page'],$this->sort['limit'])

->order("date_added DESC")

->select();

'in'多个查询

$filter_data['product_id'] = array('in',$priduct_ids)

$results = $this->ocxdm->table('product')

->where($filter_data)

->page($sort_data['page'],$this->sort['limit'])

->order("date_added DESC")

->select();

更多非等号查询符号代码

private $exp = array('eq'=>'=','neq'=>'<>','gt'=>'>','egt'=>'>=','lt'=>''<=','notlike'=>'NOT LIKE','like'=>'LIKE','in'=>'IN','notin'=>'NOT IN','not in'=>'NOT IN','between'=>'BETWEEN','not between'=>'NOT BETWEEN','notbetween'=>'NOT BETWEEN');

count查询$filter_data['add_time'] = array('>',$filter_add_time)

$results = $this->ocxdm->table('product')

->where($filter_data)

->page($sort_data['page'],$this->sort['limit'])

->order("date_added DESC")

->count();

查询字段的和$results = $this->ocxdm->table("order_product")

->where(array('order_id' => array('in',$order_id_array),))

->field(" name,SUM(quantity) AS quantity")

->group("product_id")

->select()

输出SQL功能

在写的sql方法中加上is_echo(1)即可。$results_total = $this->ocxdm->table('product')->where($filter_data)->select()->is_echo(1);

3.这个方法在一些简单的查询中 是比较方便的,但是面对一些比较复杂的查询方法就不是特别好用。如果大家在用的过程中发现了哪些可以优化的地方普天同庆欢迎您给出宝贵的建议。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值