---------------------------------------------------------- yii ar
$cri = new CDbCriteria;
$cri->select = 'file_name';
// $cri->addCondition = 'id = 8';
$cri->addCondition("id=:id"); // = 'id = 8';
// $cri->params[':id'] = 8;
$cri->params = array(':id'=>8);
$file = UserFile::model()->find($cri);
$file = UserFile::model()->find(array(
'select'=>'file_name',
'condition'=>'file_path=:file_path',
'params'=>array(':file_path'=>'/3/aaa')
));
$file = UserFile::model()->findAllBySql("select * from simiyun_files where file_path like '/1/公共目录%'");
$file = UserFile::model()->findAllBySql("select * from simiyun_files where file_path like :file_path ",array(':file_path'=>'/1/公共目录%'));
$file = UserFile::model()->count('file_path=:path',array(':path'=>$name));
$file = UserFile::model()->countBySql('select * from simiyun_files where file_path=:path',array(':path'=>$name));
$file = UserFile::model()->exists('file_path=:path',array(':path'=>$name)); //($condition,$params);
$file = UserFile::model()->updateAll(array('file_path'=>'/1/bbbaa'),'file_name=:name',array(':name'=>'bbb')); //将文件名为bbb的文件夹路径更改为‘/1/bbbaa’ 返回值为1
$file = UserFile::model()->updateByPk(array(9,10),array('file_path'=>'/1/bbbaacc')); //将主键id为9,10的文件路径改为‘/1/bbbaacc’ 返回值为2
$file = UserFile::model()->updateByPk(array(9,10),array('file_path'=>'/1/bbbaaccddd'),'file_name=:name',array(':name'=>'ccc'));
$file = UserFile::model()->updateCounters(array('download_number'=>1),'file_path like :name',array(':name'=>'/1/bbbaacc%')); //将路径为‘/1/bbbaacc%’ 的文件下载次数加1
$file = UserFile::model()->deleteByPk(8);
$file = UserFile::model()->deleteByPk(array(8,9,10),'file_path like :path',array(':path'=>'/1/bbbaacc%'));
Post::model()->deleteAll($condition,$params);
2.查询
$criteria = new CDbCriteria;
$criteria->select='title'; // 只查询出相应的 title 字段,不要这句话讲返回整条数据
$criteria->condition='nid=:newsID';
$criteria->params=array(':newsID'=>5);
$news = News::model()->find($criteria); // $params不是必须的
$news = News::model()->find(array(
'select'=>'title',
'condition'=>'nid=:newsID',
'params'=>array(':newsID'=>5),
));
$post=News::model()->find($condition,$params); // 通过指定的条件进行查询
$post=News::model()->findByPk($postID,$condition,$params); // 通过news表的primary key 进行查询
$post=News::model()->findByAttributes($attributes,$condition,$params); // 通过指定关键值继续查询
$post=News::model()->findBySql($sql,$params);
$admin=Admin::model()->findAll($condition,$params);
$admin=Admin::model()->findAllByAttributes($attributes,$condition,$params);
$admin=Admin::model()->findAllByPk($postIDs,$condition,$params);
$admin=Admin::model()->findBySql($sql,$params);
$n=Post::model()->count($condition,$params);
$n=Post::model()->countBySql($sql,$params);
$exists=Post::model()->exists($condition,$params);
3.添加
$admin=new Admin;
$admin->username=$username;
$admin->password=$password;
if($admin->save()>0)
4.修改 updateAll() updateByPk() updateCounters()
Post::model()->updateAll($attributes,$condition,$params);
Post::model()->updateByPk($pk,$attributes,$condition,$params);
Post::model()->updateCounters($counters,$condition,$params);
5.删除 deleteByPk() deleteAll()
Post::model()->deleteByPk($pk,$condition,$params);
Post::model()->deleteAll($condition,$params);
$sqlup = "update ".UserAdditionals::model()->tableName()." set space = {$userspace} where is_admin = 0 and user_id in ({$userstr})";
$is_true = Yii::app()->db->createCommand($sqlup)->execute();
---------------------------------------------------- yii 2.0
$db->createCommand('INSERT INTO customer (name) VALUES (:name)', [
':name' => 'Qiang',
])->execute();
1.查询方法 where(), join(), orderBy()
$customers = Customer::find()
->where(['status' => Customer::STATUS_ACTIVE])
->orderBy('id')
->all();
$customer = Customer::find()
->where(['id' => 1])
->one();
$count = Customer::find()
->where(['status' => Customer::STATUS_ACTIVE])
->count();
$customers = Customer::find()->indexBy('id')->all();
$sql = 'SELECT * FROM customer';
$customers = Customer::findBySql($sql)->all();
------------------------------------
$customer = Customer::findOne(1);
$customer = Customer::findOne([
'id' => 1,
'status' => Customer::STATUS_ACTIVE,
]);
$customers = Customer::findAll([1, 2, 3]);
$customer = Customer::findAll([
'status' => Customer::STATUS_DELETED,
]);
//将查询的结果转换成json格式
$customers = Customer::find()
->asArray()
->all();
3.遍历
foreach (Customer::find()->each(10) as $customer) {
}
4.保存,修改,删除
$customer = new Customer();
$customer->name = 'James';
$customer->email = 'james@example.com';
$customer->save();
$customer = Customer::findOne($id);
$customer->email = 'james@example.com';
$customer->save();
Customer::updateAllCounters(['age' => 1]);
$customer = Customer::findOne($id);
$customer->delete();
Customer::deleteAll('age > :age AND gender = :gender', [':age' => 20, ':gender' => 'M']);