首页 > 开发 > Php > 正文

FleaPHP框架数据库查询条件($conditions)写法总结

2020-02-21 20:47:30
字体:
来源:转载
供稿:网友

本文实例讲述了FleaPHP框架数据库查询条件($conditions)写法。分享给大家供大家参考,具体如下:

在FleaPHP中,凡是用到数据库查询的函数,都需要查询条件参数$conditions,现讲述用法如下:

举例:

// $conditions 保存查询条件$conditions = 'level_ix > 1';// $tableOrders 是一个订单数据表的表数据入口对象$order = $tableOrders->find($conditions, 'created DESC', 'id, title, body');$conditions = array('username' => 'dualface');// $tableUsers 是一个用户信息数据表的表数据入口对象$user = $tableUsers->find($conditions);

$conditions 参数可以是整数、字符串和数组三种类型:

1.如果 $conditions 参数是一个整数,则假定该整数为主键字段值。

// 查询主键字段值为1的记录$user = $tableUsers->find(1);// 如果主键字段名为"id",则生成的where字句为"WHERE `id` = 1"

2.如果 $conditions 参数是一个字符串,则该字符串将直接作为查询条件,这种方式可以支持最灵活的查询条件。 例如:

$conditions = 'id < 3'$user = $tableUsers->find($conditions);//生成的where字句为"WHERE id < 3"

3.1.如果 $conditions 参数是一个数组,且指定了键名和值,则查询条件中字段名为键名,字段值等于键值。例如:

// 查询id字段值为3的记录$conditions = array(  'id' => '1', );$user = $tableUsers->find($conditions);//生成的where字句为"WHERE `id` = 1"

3.2.如果 $conditions 参数是一个数组,但其中的元素没有键名, 则假定键值为自定义查询条件,例如:

$conditions = array('id = 1');// 生成的where字句为"WHERE `id` = 1"$user = $tableUsers->find($conditions);

3.3.$conditions 为数组时,可以混用字符串和键值对两种风格:

$conditions = array(  'id < 3',  'sex' => 'male',);$user = $tableUsers->find($conditions);// 生成的where字句为"id < 3 AND `sex` = 'male'"

$conditions 为数组时,多个查询条件之间将使用 AND 布尔运算符进行连接。

3.4."in()"查询在FleaPHP中的实现。(原文由DreamPig发表于http://www.fleaphp.org/bbs/viewthread.php?tid=2168)
我们有时候要用到in这样的操作,那么在condition里面怎么写呢?

// 假如主键名为"id",需要查询id的值为1、2、3其中之一,则可以这样写:$condition = array(  'in()' => array(1,2,3),)$user = $tableUsers->find($conditions);// 生成的where子句为"WHERE `id` IN (1, 2, 3)"

那么如果不是主键的话怎么写了呢? 也很简单,提供键值对即可。例如:

$condition = array(  'in()' => array(          'username' => array('username1','username2')         )  )$user = $tableUsers->find($conditions);// 生成的where子句为"WHERE `username` IN ('username1', 'username2')"            
发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表