免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 1001 | 回复: 0

Yii framework CRUD code sytax (all) [复制链接]

论坛徽章:
0
发表于 2011-12-20 09:48 |显示全部楼层
  1. <?php
  2. class PostTest extends CDbTestCase{
  3.     public $fixtures = array(
  4.         'posts' => 'Post',
  5.         'tags' => 'Tag',
  6.     );
  7.     
  8.     public function testFindPost(){
  9.         //调用 find 时,我们使用 $condition 和 $params 指定查询条件。
  10.         //此处 $condition 可以是 SQL 语句中的 WHERE 字符串,$params 则是一个参数数组,
  11.         //其中的值应绑定到 $condation 中的占位符。
  12.         $post = $this->posts('post1');
  13.         $fPost = Post::model()->find('id = :id', array(':id' => $post->id));
  14.         //SELECT * FROM `tbl_post` `t` WHERE `t`.`id`=1 LIMIT 1
  15.         
  16.         $fPost = Post::model()->find('?', array($post->id));
  17.         //SELECT * FROM `tbl_post` `t` WHERE '1' LIMIT 1
  18.         
  19.         //find返回符合条件的第一条记录,而findAll会返回符合条件的所有行。
  20.         $fAllPost = Post::model()->findAll('id = :id', array(':id' => $post->id));
  21.         //SELECT * FROM `tbl_post` `t` WHERE id = '1'
  22.         
  23.         $fAllPost = Post::model()->findAll('?', array($post->id));
  24.         //SELECT * FROM `tbl_post` `t` WHERE '1'
  25.         
  26.         $criteria = new CDbCriteria();
  27.         $criteria->condition = 'id = :id AND title = :title';
  28.         $criteria->params = array(':id' => $post->id, ':title' => $post->title);
  29.         $fPost = Post::model()->find($criteria);
  30.         //SELECT * FROM `tbl_post` `t` WHERE id = '1' AND title = 'post1' LIMIT 1
  31.         
  32.         $fAllPost = Post::model()->findAll($criteria);
  33.         //SELECT * FROM `tbl_post` `t` WHERE id = '1' AND title = 'post1'
  34.         
  35.         $fPost = Post::model()->findByPk($post->id, 'title = :title', array(':title' => $post->title));
  36.         //SELECT * FROM `tbl_post` `t` WHERE `t`.`id`=1 AND (title = 'post1') LIMIT 1
  37.         
  38.         $fPost = Post::model()->findByAttributes(array('id' => $post->id, 'title' => $post->title));
  39.         //SELECT * FROM `tbl_post` `t` WHERE `t`.`id`='1' AND `t`.`title`='post1' LIMIT 1
  40.         
  41.         $sql = 'SELECT id, title from {{post}} WHERE id = ? AND title = ?'; //必须设置表前缀
  42.         $fPost = Post::model()->findBySql($sql, array($post->id, $post->title));
  43.         
  44.         $sql = 'SELECT id, title from {{post}} WHERE id = :id AND title = :title';
  45.         $fPost = Post::model()->findBySql($sql, array(':id' => $post->id, ':title' => $post->title));
  46.         
  47.         //如果没有找到符合条件的行,find返回null,findAll 返回 array()。
  48.         
  49.     }
  50.     
  51.     public function testCountPost(){
  52.         $post = $this->posts('post1');
  53.         
  54.         $cPost = Post::model()->count('?', array($post->title));
  55.         //SELECT COUNT(*) FROM `tbl_post` `t` WHERE 'post1' 无意义
  56.         
  57.         $cPost = Post::model()->countByAttributes(array('title' => $post->title, 'content' => $post->content));
  58.         //SELECT COUNT(*) FROM `tbl_post` `t` WHERE `t`.`title`='post1' AND `t`.`content`='content1'
  59.         
  60.         $sql = "SELECT title from {{post}} WHERE title LIKE '%" . $post->title . "%'";
  61.         $cPost = Post::model()->countBySql($sql);
  62.         //至少有一条记录符合查询条件
  63.         $ePost = Post::model()->exists('id = ?     AND    title = ?', array($post->id, $post->title));
  64.         //SELECT 1 FROM `tbl_post` `t` WHERE id = '1'     AND    title = 'post1' LIMIT 1
  65.     }
  66.     
  67.     public function testUpdatePost(){
  68.         $post = $this->posts('post1');
  69.         $post->title = 'update post 1';
  70.         
  71.         if ($post->isNewRecord){
  72.             $post->create_time = $post->update_time = new CDbExpression('NOW()');
  73.             //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
  74.         }else{
  75.             $post->update_time = time();
  76.         }
  77.         
  78.         $post->save();
  79.         
  80.         
  81.         //updateAll
  82.         $sql = "SELECT * FROM {{post}} WHERE title LIKE '%" . "post" . "%'";
  83.         //SELECT * FROM tbl_post WHERE title LIKE '%post%'
  84.         
  85.         $post = Post::model()->findBySql($sql);
  86.         $post->updateAll(array('update_time' => time()), 'id <= ?', array('2'));
  87.         //UPDATE `tbl_post` SET `update_time`=1302161123 WHERE id <= '2'
  88.         
  89.         $post->updateByPk($post->id + 2, array('title' => 'update post 3'));
  90.         $post->updateByPk($post->id, array('title' => 'update post 3'), 'id = ?', array('3'));
  91.         
  92.         //updateCounter 更新某个字段的数值,一般是计数器(+/-)。
  93.         $tag = $this->tags('tag1');
  94.         $uTag = Tag::model()->updateCounters(array('frequency' => '3'), 'id = ?', array('1'));
  95.     }
  96.     
  97.     public function testDeletePost(){
  98.         $post = $this->posts('post1');
  99.         $post->delete();
  100.         
  101.         
  102.         $this->assertEquals(1, $post->id); //删除数据库表中的记录,但是post的这个实例还在。
  103.         $post2 = Post::model()->findByPk($post->id);
  104.         $this->assertEquals(null, $post2);
  105.         
  106.         //多条记录
  107.         $delete = Post::model()->deleteAll('(id = ? AND title = ?) || (id = \'4\') ', array(1, 'post 1'));
  108.         $this->assertEquals(0, $delete);
  109.         
  110.         $delete = Post::model()->deleteAllByAttributes(array('id' => '2'), 'content = ?', array('content2'));
  111.         //DELETE FROM `tbl_post` WHERE `tbl_post`.`id`='2' AND (content = 'content2')
  112.         $this->assertEquals(1, $delete);
  113.     }
  114. }
  115. ?>
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP