CI 框架中 AR 操作

Model 层中的部分代码

     /**
* CI 中的 AR 操作
* @author zhaoyingnan
**/
public function mAR()
{
/*************** 查询 *************/
//select * from mp4ba limit 21,10;
//$objResult = $this->db->get('mp4ba', 10, 21);
//echo $this->db->last_query();die; //select * from mp4ba where id =32 limit 21,10;
//select * from mp4ba where id =32 and name = '刺客聂隐娘'limit 21,10;
//$objResult = $this->db->get_where('mp4ba', array('id'=>32), 10, 21);
//echo $this->db->last_query();die;
//$objResult = $this->db->get_where('mp4ba', array('id'=>32,'name'=>'刺客聂隐娘'), 10, 21);
//echo $this->db->last_query();die; //select id,name,url from mp4ba where id =32;
//$objResult = $this->db->select('id,name,url')->get_where('mp4ba', array('id'=>32));
//echo $this->db->last_query();die; //select id,name,url from mp4ba where id =32 or id=39;
//$objResult = $this->db->select('id,name,url')->where(array('id'=>32))->or_where(array('id'=>39))->get('mp4ba');
//echo $this->db->last_query();die; //select id,name,url from mp4ba where id in(33,44,55);
//select id,name,url from mp4ba where id in(33,44,55) or sort_id in (3,4);
//select id,name,url from mp4ba where id not in(33,44,55);
//$objResult = $this->db->select('id,name,url')->where_in('id', array(33,44,55))->get('mp4ba');
//$objResult = $this->db->select('id,name,url')->where_in('id', array(33,44,55))->or_where_in('sort_id', array(3,4))->get('mp4ba');
//$objResult = $this->db->select('id,name,url')->where_not_in('id', array(33,44,55))->get('mp4ba');
//echo $this->db->last_query();die; //select id,name,url from mp4ba join user on (mp4ba.uid=user.id) order by mp4ba.dateline desc;
//$objResult = $this->db->select('id,name,url')->from('mp4ba')->join('user', 'mp4ba.uid = user.id')->order_by('mp4ba.dateline', 'desc')->get();
//echo $this->last_query();die; //select * from mp4ba where name like '%刺客%';
//select * from mp4ba where name not like '%刺客%';
//select * from mp4ba where name like '%刺客%' or url like 'eqfdf%';
//$objResult = $this->db->like('name', '刺客')->get('mp4ba');
//$objResult = $this->db->not_like('name', '刺客')->get('mp4ba');
//$objResult = $this->db->like('name', '刺客')->or_like('url', 'eqfdf', 'after')->get('mp4ba');
//echo $this->db->last_query();die; //select max(id) from mp4ba where name = '刺客聂隐娘';
//select min(id) from mp4ba where name = '刺客聂隐娘';
//$objResult = $this->db->select_max('id')->get_where('mp4ba', array('name'=>'刺客聂隐娘'));
//echo $this->db->last_query();die;
//$objResult = $this->db->select_min('id')->get_where('mp4ba', array('name'=>'刺客聂隐娘'));
//echo $this->db->last_query();die; //SELECT id,sort_id,menu,name FROM mp4ba WHERE id > 3 ORDER BY `dateline` desc LIMIT 10,100
//$objResult = $this->db->select('id,sort_id,menu,name')->from('mp4ba')->where('id >', 3)->order_by('dateline desc')->limit(100,10)->get();
//echo $this->db->last_query();
//return $objResult->result(); /*************** 插入 *************/
//生成一条基于你所提供的数据的SQL插入字符串并执行查询。你可以向函数传递 数组 或一个 对象。下面是一个使用数组的例子:
$arInsert = array(
'name' => '小黄人',
'url' => 'www.test.com',
'sort_id' => 1,
'menu' => '动画片'
);
//$this->db->insert('mp4ba', $arInsert);
//echo $this->db->insert_id();die; /*************** 修改 *************/
$arData = array(
'name' => '小黄人,好玩嘛',
'url' => 'www.test_xiaohuangren.com',
'sort_id' => 1,
'menu' => '动画片'
);
//$this->db->update('mp4ba', $arData, array('id'=>3498));
//echo $this->db->affected_rows(); #受影响的行数
//echo '<br/>';
//$objResult = $this->db->where(array('id'=>3498))->get('mp4ba');
//formatOut($objResult->result());die; /*************** 删除 *************/
$this->db->delete('mp4ba', array('id'=>3498));
echo $this->db->affected_rows(); #受影响的行数
}

CI 中 DB_active_rec.php 该类中的部分方法的标注(会持续补充)

 <?php
class CI_DB_active_record
{
/**
* get
* @author zhaoyingnan 2015-10-14 12:50
* @param string $table 操作的表
* @param int $limit limit 值
* @param int $offset offset 值
* @return object
**/
public function get($table = '', $limit = null, $offset = null)
{} /**
* get_where
* @author zhaoyingnan 2015-10-14 12:58
* @param string $table 操作的表
* @param array $where where 子句
* @param int $limit limit 值
* @param int $offset offset 值
* @return object
**/
public function get_where($table = '', $where = null, $limit = null, $offset = null)
{} /**
* select
* @author zhaoyingnan 2015-10-14 13:13
* @param string $select 查询的字段,用逗号隔开
* @param boolean $escape 如果你把它设为FALSE, CodeIgniter 将不会使用反引号保护你的字段或者表名 。这在进行复合查询时很有用。
* @return object
**/
public function select($select = '*', $escape = NULL)
{} /**
* SELECT MAX(field) portion of a query* @description
* @author zhaoyingnan 2015-10-14 13:20
* @param string $select max(field)作用列
* @param string $alias 别名
* @return object
**/
public function select_max($select = '', $alias = '')
{} /**
* SELECT MIN(field) portion of a query
* @author zhaoyingnan 2015-10-14 13:20
* @param string $select min(field)作用列
* @param string $alias 别名
* @return object
**/
public function select_min($select = '', $alias = '')
{} /**
* SELECT AVG(field) portion of a query
* @author zhaoyingnan 2015-10-14 13:20
* @param string $select AVG(field)作用列
* @param string $alias 别名
* @return object
**/
public function select_avg($select = '', $alias = '')
{} /**
* SELECT SUM(field) portion of a query
* @author zhaoyingnan 2015-10-14 13:20
* @param string $select SUM(field)作用列
* @param string $alias 别名
* @return object
**/
public function select_sum($select = '', $alias = '')
{} /**
* @description
* @author zhaoyingnan 2015-10-14 13:26
* @param string $from 表名
* @return object
**/
public function from($from)
{} /**
* where
* @author zhaoyingnan 2015-10-14 13:31
* @param mix $key 传递数组的 key 值
* @param mix $value 传递数组的 key 对应的 value 值
* @return object
**/
public function where($key, $value = NULL, $escape = TRUE)
{
//1,简单的 key/value 方法:
//$this->db->where('name', $name);
//生成: WHERE name = 'Joe' //2.自定义 key/value 方法:
//$this->db->where('name !=', $name);
//$this->db->where('id <', $id);
//生成: WHERE name != 'Joe' AND id < 45 //3.关联数组方法:
//$array = array('name' => $name, 'title' => $title, 'status' => $status);
//$this->db->where($array);
//生成: WHERE name = 'Joe' AND title = 'boss' AND status = 'active'
//使用这个方法时你也可以包含运算符:
//$array = array('name !=' => $name, 'id <' => $id, 'date >' => $date); //4.自定义字符串:
//$where = "name='Joe' AND status='boss' OR status='active'";
//$this->db->where($where);
return $this->_where($key, $value, 'AND ', $escape);
} /**
* where
* @author zhaoyingnan 2015-10-14 13:31
* @param mix $key 传递数组的 key 值
* @param mix $value 传递数组的 key 对应的 value 值
* @return object
**/
public function or_where($key, $value = NULL, $escape = TRUE)
{
//参考where
return $this->_where($key, $value, 'OR ', $escape);
} /**
* where_in
* @author zhaoyingnan 2015-10-14 13:58
* @param string $key 要查询的列
* @param string $values 列的值得范围
* @return object
**/
public function where_in($key = NULL, $values = NULL)
{
return $this->_where_in($key, $values);
} /**
* or_where_in
* @author zhaoyingnan 2015-10-14 13:58
* @param string $key 要查询的列
* @param string $values 列的值得范围
* @return object
**/
public function or_where_in($key = NULL, $values = NULL)
{
return $this->_where_in($key, $values, FALSE, 'OR ');
} /**
* where_not_in
* @author zhaoyingnan 2015-10-14 13:58
* @param string $key 要查询的列
* @param string $values 列的值得范围
* @return object
**/
public function where_not_in($key = NULL, $values = NULL)
{
return $this->_where_in($key, $values, TRUE);
} /**
* or_where_not_in
* @author zhaoyingnan 2015-10-14 13:58
* @param string $key 要查询的列
* @param string $values 列的值得范围
* @return object
**/
public function or_where_not_in($key = NULL, $values = NULL)
{
return $this->_where_in($key, $values, TRUE, 'OR ');
} /**
* order by
* @author zhaoyingnan 2015-10-14 13:35
* @param string $orderby 被排序的列
* @param string $direction asc 或者 desc
* @return object
**/
public function order_by($orderby, $direction = '')
{} /**
* join
* @author zhaoyingnan 2015-10-14 14:07
* @param string $table 表名
* @param string $cond 条件
* @param string $type 指定 JOIN 的类型可选项包括:left, right, outer, inner, left outer, 以及 right outer
* @return
**/
public function join($table, $cond, $type = '')
{} /**
* like
* @author zhaoyingnan 2015-10-14 14:28
* @param stringi $field 错作的列
* @param mix $match 规则
* @param mix $side 通配符(%)位置可用的选项是 'before', 'after' 以及 'both' (这是默认值)
* @return object
**/
public function like($field, $match = '', $side = 'both')
{
return $this->_like($field, $match, 'AND ', $side);
} /**
* not_like
* @author zhaoyingnan 2015-10-14 14:28
* @param stringi $field 错作的列
* @param mix $match 规则
* @param mix $side 通配符(%)位置可用的选项是 'before', 'after' 以及 'both' (这是默认值)
* @return object
**/
public function not_like($field, $match = '', $side = 'both')
{
return $this->_like($field, $match, 'AND ', $side, 'NOT');
} /**
* or_like
* @author zhaoyingnan 2015-10-14 14:28
* @param stringi $field 错作的列
* @param mix $match 规则
* @param mix $side 通配符(%)位置可用的选项是 'before', 'after' 以及 'both' (这是默认值)
* @return object
**/
public function or_like($field, $match = '', $side = 'both')
{
return $this->_like($field, $match, 'OR ', $side);
} /**
* or_not_like
* @author zhaoyingnan 2015-10-14 14:28
* @param stringi $field 错作的列
* @param mix $match 规则
* @param mix $side 通配符(%)位置可用的选项是 'before', 'after' 以及 'both' (这是默认值)
* @return object
**/
public function or_not_like($field, $match = '', $side = 'both')
{
return $this->_like($field, $match, 'OR ', $side, 'NOT');
} /**
* insert 单条插入
* @author zhaoyingnan 2015-10-14 14:52
* @param string $table 表名
* @param array $set 关联数组
* @return object
**/
function insert($table = '', $set = NULL)
{} /**
* insert 批量出入
* @author zhaoyingnan 2015-10-14 14:52
* @param string $table 表名
* @param array $set 关联数组
* @return object
**/
public function insert_batch($table = '', $set = NULL)
{} /**
* update
* @author zhaoyingnan 2015-10-14 15:02
* @param string $table 表名
* @param array $set 修改内容的关联数组
* @param mixed $where where条件
* @return object
**/
public function update($table = '', $set = NULL, $where = NULL, $limit = NULL)
{} /**
* delete
* @author zhaoyingnan 2015-10-14 15:12
* @param mix $table 表名
* @param mixed $where where条件
* @return object
**/
public function delete($table = '', $where = '', $limit = NULL, $reset_data = TRUE)
{
//第一个参数是表名,第二个参数是where子句。你可以不传递第二个参数,使用 where() 或者 or_where() 函数来替代它:
//$this->db->where('id', $id);
//$this->db->delete('mytable'); //如果你想要从一个以上的表中删除数据,你可以将一个包含了多个表名的数组传递给delete()函数。
//$tables = array('table1', 'table2', 'table3');
//$this->db->where('id', '5');
//$this->db->delete($tables); //如果你想要删除表中的全部数据,你可以使用 truncate() 函数,或者 empty_table() 函数。
} /**
* limit
* @author zhaoyingnan 2015-10-14 14:34
* @param int $value
* @param int $offset
* @return object
**/
public function limit($value, $offset = '')
{} /**
* Where
*
* Called by where() or or_where()
*
* @param mixed
* @param mixed
* @param string
* @return object
**/
protected function _where($key, $value = NULL, $type = 'AND ', $escape = NULL)
{} /**
* Like
*
* Called by like() or orlike()
*
* @param mixed
* @param mixed
* @param string
* @return object
**/
protected function _like($field, $match = '', $type = 'AND ', $side = 'both', $not = '')
{
if ( ! is_array($field))
{
$field = array($field => $match);
} foreach ($field as $k => $v)
{
$k = $this->_protect_identifiers($k); $prefix = (count($this->ar_like) == 0) ? '' : $type; $v = $this->escape_like_str($v); if ($side == 'none')
{
$like_statement = $prefix." $k $not LIKE '{$v}'";
}
elseif ($side == 'before')
{
$like_statement = $prefix." $k $not LIKE '%{$v}'";
}
elseif ($side == 'after')
{
$like_statement = $prefix." $k $not LIKE '{$v}%'";
}
else
{
$like_statement = $prefix." $k $not LIKE '%{$v}%'";
} // some platforms require an escape sequence definition for LIKE wildcards
if ($this->_like_escape_str != '')
{
$like_statement = $like_statement.sprintf($this->_like_escape_str, $this->_like_escape_chr);
} $this->ar_like[] = $like_statement;
if ($this->ar_caching === TRUE)
{
$this->ar_cache_like[] = $like_statement;
$this->ar_cache_exists[] = 'like';
} }
return $this;
} }
上一篇:day01<计算机基础知识&Java语言基础>


下一篇:SQLSERVER数据库中批量导入数据的几种方法