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

2025-05-29 0 87

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

对于一个Model Post 有如下的4中查询方法,返回对象或者对象数组

?

1

2

3

4

5

6

7

8
//查找满足指定条件的结果中的第一行 find the first row satisfying the specified condition

$post=Post::model()->find($condition,$params);

//查找具有指定主键值的那一行 find the row with the specified primary key

$post=Post::model()->findByPk($postID,$condition,$params);

//查找具有指定属性值的行 find the row with the specified attribute values

$post=Post::model()->findByAttributes($attributes,$condition,$params);//未找到返回null

//通过指定的SQL 语句查找结果中的第一行 find the first row using the specified SQL statement

$post=Post::model()->findBySql($sql,$params);

如果find 方法找到了一个满足查询条件的行,它将返回一个Post 实例,实例的属性含有数据表行中相应列的值。然后我们就可以像读取普通对象的属性那样读取载入的值,例如echo $post->title;。如果使用给定的查询条件在数据库中没有找到任何东西, find 方法将返回null。

调用find 时,我们使用$condition 和$params 指定查询条件。此处$condition 可以是SQL 语句中的WHERE 字符串,$params 则是一个参数数组,其中的值应绑定到$condation 中的占位符。例如:假设我们查询postID = 10的数据

?

1

2
// find the row with postID=10

$post=Post::model()->find('postID=:postID', array(':postID'=>10));

条件$condition 就是我们sql里的where部分,那参数怎么办呢,通过params传递,不过名字是加了":"的。

YII有个CDbCriteria类来构造查询,如果我们查询postId为10的title,CdbCriteria是这样构造的

?

1

2

3

4

5
$criteria=new CDbCriteria;

$criteria->select='title'; // only select the 'title' column

$criteria->condition='postID=:postID';

$criteria->params=array(':postID'=>10);

$post=Post::model()->find($criteria); // $params is not needed

一种替代CDbCriteria 的方法是给find 方法传递一个数组数组的键和值各自对应标准( criterion)的属性名和值,上面的例子可以重写为如下:

?

1

2

3

4

5
$post=Post::model()->find(array(

'select'=>'title',

'condition'=>'postID=:postID',

'params'=>array(':postID'=>10),

));

当然也适用于findAll()

?

1

2

3

4

5

6
self::$_items[$type]=array();

$models=self::model()->findAll(array(

'condition'=>'type=:type',

'params'=>array(':type'=>$type),

'order'=>'position',

));

当一个查询条件是关于按指定的值匹配几个列时,我们可以使用findByAttributes()。我们使$attributes 参数是一个以列名做索引的值的数组
findByAttributes 里的$attributes就是字段的名字.查询title为abc怎么查询呢?见下面

复制代码 代码如下:

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

其它方法:

1、$admin=Admin::model()->findAll($condition,$params);

该方法是根据一个条件查询一个集合,如:

复制代码 代码如下:

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


2、$admin=Admin::model()->findAllByPk($postIDs,$condition,$params);
findAllByPk($id,"name like ':name' and age=:age" ,array(':name'=>$name,'age'=>$age));
该方法是根据主键查询一个集合,可以使用多个主键,如:

复制代码 代码如下:

findAllByPk(array(1,2));


3、$admin=Admin::model()->findAllByAttributes($attributes,$condition,$params);

该方法是根据条件查询一个集合,可以是多个条件,把条件放到数组里面,如:

复制代码 代码如下:

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


4、$admin=Admin::model()->findAllBySql($sql,$params);

该方法是根据SQL语句查询一个数组,如:

复制代码 代码如下:

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

二、查询对像的方法

1、$admin=Admin::model()->findByPk($postID,$condition,$params);

根据主键查询出一个对象,如:

复制代码 代码如下:

findByPk(1);


2、$row=Admin::model()->find($condition,$params);

根据一个条件查询出一组数据,可能是多个,但是他只返回第一行数据,如:

复制代码 代码如下:

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


3、$admin=Admin::model()->findByAttributes($attributes,$condition,$params);

该方法是根据条件查询一组数据,可以是多个条件,把条件放到数组里面,他查询的也是第一条数据,如:

复制代码 代码如下:

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


4、$admin=Admin::model()->findBySql($sql,$params);

该方法是根据SQL语句查询一组数据,他查询的也是第一条数据,如:

复制代码 代码如下:

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


5、拼一个获得SQL的方法,在根据find查询出一个对象

?

1

2

3

4

5
$criteria=new CDbCriteria;

$criteria->select='username'; // only select the 'title' column

$criteria->condition='username=:username';

$criteria->params=array(':username=>'admin');

$post=Post::model()->find($criteria); // $params is not needed

三、查询个数,判断查询是否有结果

1、$n=Post::model()->count($condition,$params);

该方法是根据一个条件查询一个集合有多少条记录,返回一个int型数字,如

复制代码 代码如下:

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


2、$n=Post::model()->countBySql($sql,$params);

该方法是根据SQL语句查询一个集合有多少条记录,返回一个int型数字,如

复制代码 代码如下:

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


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

四、添加的方法

?

1

2

3

4

5

6

7

8
$admin=new Admin;

$admin->username=$username;

$admin->password=$password;

if($admin->save()>0){

echo "添加成功";

}else{

echo "添加失败";

}

五、修改的方法

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

?

1

2

3

4

5

6
$count = Admin::model()->updateAll(array('username'=>'11111','password'=>'11111'),'password=:pass',array(':pass'=>'1111a1'));

if($count>0){

echo "修改成功";

}else{

echo "修改失败";

}

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

?

1

2

3

4

5

6

7
$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 "修改失败";

}

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

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

?

1

2

3

4

5

6
$count =Admin::model()->updateCounters(array('status'=>1),'username=:name',array(':name'=>'admin'));

if($count>0){

echo "修改成功";

}else{

echo "修改失败";

}

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

六、删除的方法

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

?

1

2

3

4

5

6

7

8
$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 "删除失败";

}

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

?

1

2

3

4

5

6

7
$count = Admin::model()->deleteByPk(1);

$count = Admin::model()->deleteByPk(array(1,2),'username=:name',array(':name'=>'admin'));

if($count>0){

echo "删除成功";

}else{

echo "删除失败";

}

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

收藏 (0) 打赏

感谢您的支持,我会继续努力的!

打开微信/支付宝扫一扫,即可进行扫码打赏哦,分享从这里开始,精彩与您同在
点赞 (0)

声明:本站所有文章,如无特殊说明或标注,均为本站原创发布。任何个人或组织,在未征得本站同意时,禁止复制、盗用、采集、发布本站内容到任何网站、书籍等各类媒体平台。如若本站内容侵犯了原著者的合法权益,可联系我们进行处理。

快网idc优惠网 建站教程 Yii基于数组和对象的Model查询技巧实例详解 https://www.kuaiidc.com/99555.html

相关文章

发表评论
暂无评论