Chinaunix首页 | 论坛 | 博客
  • 博客访问: 710968
  • 博文数量: 160
  • 博客积分: 8847
  • 博客等级: 中将
  • 技术积分: 1656
  • 用 户 组: 普通用户
  • 注册时间: 2010-11-25 16:46
个人简介

。。。。。。。。。。。。。。。。。。。。。。

文章分类

全部博文(160)

文章存档

2015年(1)

2013年(1)

2012年(4)

2011年(26)

2010年(14)

2009年(36)

2008年(38)

2007年(39)

2006年(1)

分类: 系统运维

2011-04-07 15:37:29

  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. ?>
阅读(956) | 评论(0) | 转发(0) |
0

上一篇:正则表达式 -- 1

下一篇:正则表达式 -- 2

给主人留下些什么吧!~~