生活随笔
收集整理的這篇文章主要介紹了
YII CRUD 例子
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
< ?? ?php ?class ?PostTest?extends ?CDbTestCase{ ?????public ?$ ?fixtures ?= ?array ?( ? ????????'posts' ?= ?> ?'Post' ?, ? ????????'tags' ?= ?> ?'Tag' ?, ? ????) ?; ? ???? ????public ?function ?testFindPost( ?) ?{ ? ????????//調用 find 時,我們使用 $condition 和 $params 指定查詢條件。 ? ????????//此處 $condition 可以是 SQL 語句中的 WHERE 字符串,$params 則是一個參數數組, ? ????????//其中的值應綁定到 $condation 中的占位符。 ? ????????$ ?post ?= ?$ ?this ?- ?> ?posts( ?'post1' ?) ?; ? ????????$ ?fPost ?= ?Post: ?: ?model( ?) ?- ?> ?find( ?'id = :id' ?, ?array ?( ?':id' ?= ?> ?$ ?post ?- ?> ?id) ?) ?; ? ????????//SELECT * FROM `tbl_post` `t` WHERE `t`.`id`=1 LIMIT 1 ? ???????? ????????$ ?fPost ?= ?Post: ?: ?model( ?) ?- ?> ?find( ?'?' ?, ?array ?( ?$ ?post ?- ?> ?id) ?) ?; ? ????????//SELECT * FROM `tbl_post` `t` WHERE '1' LIMIT 1 ? ???????? ????????//find返回符合條件的第一條記錄,而findAll會返回符合條件的所有行。 ? ????????$ ?fAllPost ?= ?Post: ?: ?model( ?) ?- ?> ?findAll( ?'id = :id' ?, ?array ?( ?':id' ?= ?> ?$ ?post ?- ?> ?id) ?) ?; ? ????????//SELECT * FROM `tbl_post` `t` WHERE id = '1' ? ???????? ????????$ ?fAllPost ?= ?Post: ?: ?model( ?) ?- ?> ?findAll( ?'?' ?, ?array ?( ?$ ?post ?- ?> ?id) ?) ?; ? ????????//SELECT * FROM `tbl_post` `t` WHERE '1' ? ???????? ????????$ ?criteria ?= ?new ?CDbCriteria( ?) ?; ? ????????$ ?criteria ?- ?> ?condition?= ?'id = :id AND title = :title' ?; ? ????????$ ?criteria ?- ?> ?params?= ?array ?( ?':id' ?= ?> ?$ ?post ?- ?> ?id, ?':title' ?= ?> ?$ ?post ?- ?> ?title) ?; ? ????????$ ?fPost ?= ?Post: ?: ?model( ?) ?- ?> ?find( ?$ ?criteria ?) ?; ? ????????//SELECT * FROM `tbl_post` `t` WHERE id = '1' AND title = 'post1' LIMIT 1 ? ???????? ????????$ ?fAllPost ?= ?Post: ?: ?model( ?) ?- ?> ?findAll( ?$ ?criteria ?) ?; ? ????????//SELECT * FROM `tbl_post` `t` WHERE id = '1' AND title = 'post1' ? ???????? ????????$ ?fPost ?= ?Post: ?: ?model( ?) ?- ?> ?findByPk( ?$ ?post ?- ?> ?id, ?'title = :title' ?, ?array ?( ?':title' ?= ?> ?$ ?post ?- ?> ?title) ?) ?; ? ????????//SELECT * FROM `tbl_post` `t` WHERE `t`.`id`=1 AND (title = 'post1') LIMIT 1 ? ???????? ????????$ ?fPost ?= ?Post: ?: ?model( ?) ?- ?> ?findByAttributes( ?array ?( ?'id' ?= ?> ?$ ?post ?- ?> ?id, ?'title' ?= ?> ?$ ?post ?- ?> ?title) ?) ?; ? ????????//SELECT * FROM `tbl_post` `t` WHERE `t`.`id`='1' AND `t`.`title`='post1' LIMIT 1 ? ???????? ????????$ ?sql ?= ?'SELECT id, title from {{post}} WHERE id = ? AND title = ?' ?; ?//必須設置表前綴 ? ????????$ ?fPost ?= ?Post: ?: ?model( ?) ?- ?> ?findBySql( ?$ ?sql ?, ?array ?( ?$ ?post ?- ?> ?id, ?$ ?post ?- ?> ?title) ?) ?; ? ???????? ????????$ ?sql ?= ?'SELECT id, title from {{post}} WHERE id = :id AND title = :title' ?; ? ????????$ ?fPost ?= ?Post: ?: ?model( ?) ?- ?> ?findBySql( ?$ ?sql ?, ?array ?( ?':id' ?= ?> ?$ ?post ?- ?> ?id, ?':title' ?= ?> ?$ ?post ?- ?> ?title) ?) ?; ? ???????? ????????//如果沒有找到符合條件的行,find返回null,findAll 返回 array()。 ? ???????? ????} ? ???? ????public ?function ?testCountPost( ?) ?{ ? ????????$ ?post ?= ?$ ?this ?- ?> ?posts( ?'post1' ?) ?; ? ???????? ????????$ ?cPost ?= ?Post: ?: ?model( ?) ?- ?> ?count ?( ?'?' ?, ?array ?( ?$ ?post ?- ?> ?title) ?) ?; ? ????????//SELECT COUNT(*) FROM `tbl_post` `t` WHERE 'post1' 無意義 ? ???????? ????????$ ?cPost ?= ?Post: ?: ?model( ?) ?- ?> ?countByAttributes( ?array ?( ?'title' ?= ?> ?$ ?post ?- ?> ?title, ?'content' ?= ?> ?$ ?post ?- ?> content) ?) ?; ? ????????//SELECT COUNT(*) FROM `tbl_post` `t` WHERE `t`.`title`='post1' AND `t`.`content`='content1' ? ???????? ????????$ ?sql ?= ?"SELECT title from {{post}} WHERE title LIKE '%" ?. ?$ ?post ?- ?> ?title?. ?"%'" ?; ? ????????$ ?cPost ?= ?Post: ?: ?model( ?) ?- ?> ?countBySql( ?$ ?sql ?) ?; ? ????????//至少有一條記錄符合查詢條件 ? ????????$ ?ePost ?= ?Post: ?: ?model( ?) ?- ?> ?exists( ?'id = ? ????AND????title = ?' ?, ?array ?( ?$ ?post ?- ?> ?id, ?$ ?post ?- ?> ?title) ?) ?; ? ????????//SELECT 1 FROM `tbl_post` `t` WHERE id = '1' ????AND????title = 'post1' LIMIT 1 ? ????} ? ???? ????public ?function ?testUpdatePost( ?) ?{ ? ????????$ ?post ?= ?$ ?this ?- ?> ?posts( ?'post1' ?) ?; ? ????????$ ?post ?- ?> ?title?= ?'update post 1' ?; ? ???????? ????????if ?( ?$ ?post ?- ?> ?isNewRecord) ?{ ? ????????????$ ?post ?- ?> ?create_time?= ?$ ?post ?- ?> ?update_time?= ?new ?CDbExpression( ?'NOW()' ?) ?; ? ????????????//UPDATE `tbl_post` SET `id`=1, `title`='update post 1', `content`='content1', `tags`=NULL, `status`=1, `create_time`=NULL, `update_time`=1302161123, `author_id`=1 WHERE `tbl_post`.`id`=1 ? ????????} ?else ?{ ? ????????????$ ?post ?- ?> ?update_time?= ?time ?( ?) ?; ? ????????} ? ???????? ????????$ ?post ?- ?> ?save( ?) ?; ? ???????? ???????? ????????//updateAll ? ????????$ ?sql ?= ?"SELECT * FROM {{post}} WHERE title LIKE '%" ?. ?"post" ?. ?"%'" ?; ? ????????//SELECT * FROM tbl_post WHERE title LIKE '%post%' ? ???????? ????????$ ?post ?= ?Post: ?: ?model( ?) ?- ?> ?findBySql( ?$ ?sql ?) ?; ? ????????$ ?post ?- ?> ?updateAll( ?array ?( ?'update_time' ?= ?> ?time ?( ?) ?) ?, ?'id <= ?' ?, ?array ?( ?'2' ?) ?) ?; ? ????????//UPDATE `tbl_post` SET `update_time`=1302161123 WHERE id <= '2' ? ???????? ????????$ ?post ?- ?> ?updateByPk( ?$ ?post ?- ?> ?id?+ ?2, ?array ?( ?'title' ?= ?> ?'update post 3' ?) ?) ?; ? ????????$ ?post ?- ?> ?updateByPk( ?$ ?post ?- ?> ?id, ?array ?( ?'title' ?= ?> ?'update post 3' ?) ?, ?'id = ?' ?, ?array ?( ?'3' ?) ?) ?; ? ???????? ????????//updateCounter 更新某個字段的數值,一般是計數器(+/-)。 ? ????????$ ?tag ?= ?$ ?this ?- ?> ?tags( ?'tag1' ?) ?; ? ????????$ ?uTag ?= ?Tag: ?: ?model( ?) ?- ?> ?updateCounters( ?array ?( ?'frequency' ?= ?> ?'3' ?) ?, ?'id = ?' ?, ?array ?( ?'1' ?) ?) ?; ? ????} ? ???? ????public ?function ?testDeletePost( ?) ?{ ? ????????$ ?post ?= ?$ ?this ?- ?> ?posts( ?'post1' ?) ?; ? ????????$ ?post ?- ?> ?delete ?( ?) ?; ? ???????? ???????? ????????$ ?this ?- ?> ?assertEquals( ?1, ?$ ?post ?- ?> ?id) ?; ?//刪除數據庫表中的記錄,但是post的這個實例還在。 ? ????????$ ?post2 ?= ?Post: ?: ?model( ?) ?- ?> ?findByPk( ?$ ?post ?- ?> ?id) ?; ? ????????$ ?this ?- ?> ?assertEquals( ?null ?, ?$ ?post2 ?) ?; ? ???????? ????????//多條記錄 ? ????????$ ?delete ?= ?Post: ?: ?model( ?) ?- ?> ?deleteAll( ?'(id = ? AND title = ?) || (id = \'4\') ' ?, ?array ?( ?1, ?'post 1' ?) ?) ?; ? ????????$ ?this ?- ?> ?assertEquals( ?0, ?$ ?delete ?) ?; ? ???????? ????????$ ?delete ?= ?Post: ?: ?model( ?) ?- ?> ?deleteAllByAttributes( ?array ?( ?'id' ?= ?> ?'2' ?) ?, ?'content = ?' ?, ?array ?( ?'content2' ) ?) ?; ? ????????//DELETE FROM `tbl_post` WHERE `tbl_post`.`id`='2' AND (content = 'content2') ? ????????$ ?this ?- ?> ?assertEquals( ?1, ?$ ?delete ?) ?; ? ????} ? } ?? ?>
總結
以上是生活随笔 為你收集整理的YII CRUD 例子 的全部內容,希望文章能夠幫你解決所遇到的問題。
如果覺得生活随笔 網站內容還不錯,歡迎將生活随笔 推薦給好友。