YII DAO有很多的現成的方法可以幫助我們,當然在使用之前我們首先要得到一個實例化的對象,比如:
$command = Yii::app()->db->createCommand();注意參數留空了,現在我們就可以通過對象$command
調用這些方法了,還有一點要說一下就是$command可以多次使用,但是在重復使用之前需要reset()一下
就可以了,下面我先羅列一些可供我們使用的方法:
->select() ? ? ? ? ? ? ? SELECT子句
->selectDistinct() 并保持了記錄的唯一性
->from(): ? ? ? ? ? ? ? ?構建FROM子句
->where(): ? ? ? ? ? ? 構建WHERE子句
->join(): ? ? ? ? ? ? ? ? 在FROM子句中構建INNER JOIN 子句
->leftJoin(): ? ? ? ? ? 在FROM子句中構建左連接子句
->rightJoin(): ? ? ? ? 在FROM子句中構建右連接子句
->crossJoin(): ? ? ? 添加交叉查詢片段(沒用過)
->naturalJoin(): ? ?添加一個自然連接子片段
->group(): ? ? ? ? ? ? GROUP BY子句
->having(): ? ? ? ? ? 類似于WHERE的子句,但要與GROUP BY連用
->order(): ? ? ? ? ? ? ?ORDER BY子句
->limit(): ? ? ? ? ? ? ? ?LIMIT子句的第一部分
->offset(): ? ? ? ? ? ? ?LIMIT子句的第二部分
->union(): ? ? ? ? ? ? appends a UNION query fragment
select() //function select($columns='*')
select('username, email');//指定列 ? ? ? ?select('tbl_user.id');//使用表限定
select('username name');//使用別名 ? ? select(array('id', 'count(*) as num'));// 或使用數組作為參數
form() //function from($tables)
from('tbl_user,tbl_profile');//像sql那樣 ??
from('tbl_user u, public.tbl_profile p');//使用表別名, 還可以使用完整的數據庫限定名
WHERE ?//function where($conditions, $params=array())
where(array('and', 'id=:id', 'username=:username'), array(':id'=>$id, ':username'=>$username);// 在where()中使用 AND
where( array('and', 'type=1', array('or', 'id=:id','username=abc') ),array(':id'=>$id));//在where()中使用 OR 與 AND用法相同
where(array('in', 'id', array(1,2,3)))// IN 操作符用法
where( array('like','name', '%tester%') );// LIKE 用法
where( array('like','name', array('%test%', '%sample%')) )//等于name LIKE '%test%' AND name LIKE '%sample%
// 添加了這么多,你都不知道合成后的SQL長啥樣了,可以使用->text查看
join() //function join($table, $conditions, $params=array())
leftJoin() //function leftJoin($table, $conditions, $params=array())
rightJoin() //function rightJoin($table, $conditions, $params=array())
crossJoin() //function crossJoin($table)
naturalJoin() //function naturalJoin($table)
join('tbl_user', 'user_id=:id',array(":id"=>$id))// JOIN `tbl_profile` ON user_id=id(綁定叁數)
leftJoin('tbl_post p', 'p.user_id=id AND type=1')// LEFT JOIN `tbl_post` `p` ON p.user_id=id AND type=1
group() //function group($columns)
group('name, id')// GROUP BY `name`, `id` ? ? ?group(array('tbl_user.name', 'id'))// GROUP BY `tbl_user`.`name`, `id`
having() //function having($conditions, $params=array())
having('id=:id',array(":id"=>$id))// HAVEING id=$id ? having('id=1 or id=2')// HAVING id=1 or id=2
having(array('or', 'id=1', 'id=2'))// HAVING id=1 OR id=2
order() //function order($columns)
order('name, id desc')// ORDER BY `name`, `id` DESC
order(array('tbl_profile.name', 'id desc'))// ORDER BY `tbl_profile`.`name`, `id` DESC
limit() //function limit($limit, $offset=null) and offset() //function offset($offset)
limit(10)// LIMIT 10 ? ?limit(10, 20)// LIMIT 10 OFFSET 20 ? ?offset(20)// OFFSET 20
union() //function union($sql)執行指定的sql語句
union('select * from tbl_profile')// UNION (select * from tbl_profile)
總之就是YII DAO使用起來可能有些繁瑣,所以適用于處理復雜的數據庫操作,當然DAO的操作不止這些因為我在整理CRUD
所以就記錄了這些,如果有興趣大家可以直接查看這方面的手冊?
http://www.yiichina.com/api/CDbCommand
?
更多文章、技術交流、商務合作、聯系博主
微信掃碼或搜索:z360901061

微信掃一掃加我為好友
QQ號聯系: 360901061
您的支持是博主寫作最大的動力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描下面二維碼支持博主2元、5元、10元、20元等您想捐的金額吧,狠狠點擊下面給點支持吧,站長非常感激您!手機微信長按不能支付解決辦法:請將微信支付二維碼保存到相冊,切換到微信,然后點擊微信右上角掃一掃功能,選擇支付二維碼完成支付。
【本文對您有幫助就好】元
