本篇文章给大家分享一个opencart查询数据库简单方法,这个方法当然不是自带的啦,只需要添加一个文件(ocxdm.php)和一行代码就可以使用了。
首先将ocxdm.php 上传到系统类库目录下 system/library
然后 打开 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.这个方法在一些简单的查询中 是比较方便的,但是面对一些比较复杂的查询方法就不是特别好用。如果大家在用的过程中发现了哪些可以优化的地方普天同庆欢迎您给出宝贵的建议。