Home > Backend Development > PHP Tutorial > Yii基于数组和对象的Model查询技巧实例详解_PHP

Yii基于数组和对象的Model查询技巧实例详解_PHP

WBOY
Release: 2016-05-28 11:49:59
Original
1263 people have browsed it

本文实例讲述了Yii基于数组和对象的Model查询技巧。分享给大家供大家参考,具体如下:

代码如下:

Post::model()->findByAttributes(array('title'=>'abc'))

其它方法:

代码如下:

findAll("username=:name",array(":name"=>$username));


代码如下:

findAllByPk(array(1,2));


代码如下:

findAllByAttributes(array('username'=>'admin'));


代码如下:

findAllBySql("select *from admin where username=:name",array(':name'=>'admin'));

二、查询对像的方法

代码如下:

findByPk(1);


代码如下:

find('username=:name',array(':name'=>'admin'));


代码如下:

findByAttributes(array('username'=>'admin'));


代码如下:

findBySql("select *from admin where username=:name",array(':name'=>'admin'));


代码如下:

count("username=:name",array(":name"=>$username));


代码如下:

countBySql("select *from admin where username=:name",array(':name'=>'admin'));


3、$exists=Post::model()->exists($condition,$params);
该方法是根据一个条件查询查询得到的数组有没有数据,如果有数据返回一个true,否则没有找到

四、添加的方法

$admin=new Admin;
$admin->username=$username;
$admin->password=$password;
if($admin->save()>0){
  echo "添加成功";
}else{
  echo "添加失败";
}

Copy after login

五、修改的方法

1、Post::model()->updateAll($attributes,$condition,$params);

$count = Admin::model()->updateAll(array('username'=>'11111','password'=>'11111'),'password=:pass',array(':pass'=>'1111a1'));
if($count>0){
  echo "修改成功";
}else{
  echo "修改失败";
}

Copy after login

2、Post::model()->updateByPk($pk,$attributes,$condition,$params);

$count = Admin::model()->updateByPk(1,array('username'=>'admin','password'=>'admin'));
$count = Admin::model()->updateByPk(array(1,2),array('username'=>'admin','password'=>'admin'),'username=:name',array(':name'=>'admin'));
if($count>0){
  echo "修改成功";
}else{
  echo "修改失败";
}

Copy after login

$pk代表主键,可以是一个也可以是一个集合,$attributes代表是要修改的字段的集合,$condition代表条件,$params传入的值

3、Post::model()->updateCounters($counters,$condition,$params);

$count =Admin::model()->updateCounters(array('status'=>1),'username=:name',array(':name'=>'admin'));
if($count>0){
  echo "修改成功";
}else{
  echo "修改失败";
}

Copy after login

array('status'=>1)代表数据库中的admin表根据条件username='admin',查询出的所有结果status字段都自加1

六、删除的方法

1、Post::model()->deleteAll($condition,$params);

$count = Admin::model()->deleteAll('username=:name and password=:pass',array(':name'=>'admin',':pass'=>'admin'));
      $id=1,2,3
      deleteAll('id in(".$id.")');删除id为这些的数据
if($count>0){
  echo "删除成功";
}else{
  echo "删除失败";
}

Copy after login

2、Post::model()->deleteByPk($pk,$condition,$params);

$count = Admin::model()->deleteByPk(1);
$count = Admin::model()->deleteByPk(array(1,2),'username=:name',array(':name'=>'admin'));
if($count>0){
  echo "删除成功";
}else{
  echo "删除失败";
}

Copy after login

希望本文所述对大家基于Yii框架的PHP程序设计有所帮助。

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template