Yii框架下数据操作-model、对象

 1 ==================查询==================
 2 
 3 条件-绑定参数-返回多个对象
 4 $admin = Admin::model()->findAll($condition, $params);
 5 $admin = Admin::model()->findAll("username = :name", [":name" => $username]);
 6 $admin = Admin::model()->findAll("status = 1 ORDER BY id DESC limit 10");
 7 
 8 条件-绑定参数-返回一个对象
 9 $admin = Admin::model()->find($condition, $params);
10 $admin = Admin::model()->find('username = :name', ':name'=>'admin');  
11 
12 
13 主键-条件-绑定参数-返回多个对象
14 $admin = Admin::model()->findAllByPk($postIDs, $condition, params);
15 $admin = Admin::model()->findAllByPk($id,"name like :name and age=:age",array(':name'=>$name,'age'=>$age));
16 $admin = Admin::model()->findAllByPk(array(1,2));
17 
18 主键-条件-绑定参数-返回一个对象
19 $admin = Admin::model()->findByPk($postID, $condition, $params);
20 $admin = Admin::model()->findByPk(1);
21 
22 条件数组-条件-绑定参数-返回多个对象
23 $admin = Admin::model()->findAllByAttributes($attributes, $condition, $params);
24 $admin = Admin::model()->findAllByAttributes(array('username' => 'admin'));
25 
26 条件数组-条件-绑定参数-返回一个对象
27 $admin = Admin::model()->findByAttributes($attributes, $condition, $params);
28 $admin = Admin::model()->findByAttributes(array('username' => 'admin'));
29 
30 sql语句-绑定参数-返回多个对象
31 $admin = Admin::model()->findAllBySql($sql, $params);
32 $admin = Admin::model()->findAllBySql("select id from admin where username = :name", [':name'=>'%ad%']);
33 
34 sql语句-绑定参数-返回一个对象
35 $admin = Admin::model()->findBySql($sql, $params);
36 $admin = Admin::model()->findBySql("select * from admin where username=:name",array(':name'=>'admin'));
37 
38 $criteria = new CDbCriteria;
39 $criteria->select = 'id';//查询的字段
40 $criteria->condition = 'nmae = :name';//有且只有一个条件
41 $criteria->params = [':name' => 'admin'];//绑定参数
42 $criteria->join = 'xxx';//连接表
43 $criteria->with = 'xxx';//调用relations
44 $criteria->order = 'id desc';//指定字段排序
45 $criteria->offset = 1;//起始量,通常和limit配合使用
46 $criteria->limit = 3;//指定查询数据量
47 $criteria->group = 'xxx';//分组
48 $criteria->having = 'xxx';
49 $criteria->distinct = false;//是否唯一查询
50 $criteria->addCondition('id = 1');//id = 1
51 $criteria->addCondition('id = 1', 'or');//or id = 1
52 $criteria->addInCondition('id', [1, 2, 3]);//id in (1,2,3)
53 $criteria->addNotInCondition('id', [1, 2, 3])//id not in (1,2,3)
54 $criteria->addSearchCondition('name', '你好');//name like '%你好%'
55 $criteria->addBetweenCondition('id', 1, 10);//id between 1 and 10
56 $criteria->compare('id', 1);//如果第二个参数是数字,则按照addCondition处理
57 $criteria->compare('id', [1, 2, 3]);//如果第二个参数是数组,则按照addInCondition处理
58 $admin = Admin::model()->find($criteria);
59 
60 条件-绑定参数-返回数据记录数
61 $count = Admin::model()->count($condition, $params);
62 $count = Admin::model()->count('name = "你好"');
63 $count = Admin::model()->count('name = :name', [':name' => '你好']);
64 
65 sql语句-绑定参数-返回数据记录数
66 $count = Admin::model()->countBySql($sql, $params);
67 $count = Admin::model()->countBySql('select count(*) from admin where name = "你好"');
68 $count = Admin::model()->countBySql('select count(*) from admin where name = :name', [':name' => '你好']);
69 
70 条件-绑定参数-返回true或false
71 $exists = Admin::model()->exists($condition, $params);
72 $exists = Admin::model()->exists('name = "你好"');
73 $exists = Admin::model()->exists('name = :name', [':name' => '你好']);
74 
75 ==================新增==================
76 $admin = new Admin;
77 $admin->username = $username;
78 $admin->time = time();
79 $save = $admin->save();//需要指定全部字段,否则会返回false
80 
81 ==================修改==================
82 Admin::model()->updateAll($attributes, $condition, $params);//$attributes是要修改的字段的集合,$condition条件,$params传入的值
83 $update = Admin::model()->updateAll(['name' => '你好吗'], 'id = :id', [':id' => 1]);//将id = 1的name的值修改成‘你好吗’
84 
85 Admin::model()->updateByPk($pk, $attributes, $condition, $params);//$pk主键,可以是一个也可以是一个集合,$attributes是要修改的字段的集合,$condition条件,$params传入的值
86 $update = Admin::model()->updateByPk(1, ['name' => '你好吗']);
87 $update = Admin::model()->updateByPk([1, 2], ['name' => '你好吗']);
88 
89 Admin::model()->updateCounters($counters, $condition, $params);
90 $update = Admin::model()->updateCounters(['tel' => 1], 'id = :id', [':id' => 1]);//将id = 1的tel字段自加1,类似set tel = tel + 1,如果是负数则自减1,set tel = tel - 1;
91 
92 ==================删除==================
93 Admin::model()->deleteAll($condition, $params);
94 $delete = Admin::model()->deleteAll('id = :id', [':id' => 1]);//删除id = 1的数据
95 $delete = Admin::model()->deleteAll('id = 1');//删除id = 1的数据
96 
97 Admin::model()->deleteByPk($pk, $condition, $params);
98 $delete = Admin::model()->deleteByPk(1, 'name = :name', [':name' => '你好']);//删除主键id = 1 and name = '你好'的数据,如果id = 1但是name != '你好'会删除失败,所以这个方法一般只需要传主键的值就好了

 

posted @ 2019-08-20 16:27  第一夫人  阅读(1397)  评论(0编辑  收藏  举报