fleaphp中的常用函数findAll,主要用与构造sql语句并查询数据,findAll方法的使用和示例
findAll的函数原型是
- /**
- * 查询所有符合条件的记录及相关数据,返回一个包含多行记录的二维数组,失败时返回 false
- *
- * @param mixed $conditions
- * @param string $sort
- * @param mixed $limit
- * @param mixed $fields
- * @param mixed $queryLinks
- *
- * @return array
- */
- function & findAll( $conditions = null, $sort = null, $limit = null, $fields = '*' , $queryLinks = true)
- {
- list($whereby , $distinct ) = $this ->getWhere( $conditions );
- // 处理排序
- $sortby = $sort != '' ? " ORDER BY {$sort}" : '' ;
- // 处理 $limit
- if ( is_array ( $limit )) {
- list($length , $offset ) = $limit ;
- } else {
- $length = $limit ;
- $offset = null;
- }
- // 构造从主表查询数据的 SQL 语句
- $enableLinks = count ( $this ->links) > 0 && $this ->autoLink && $queryLinks ;
- $fields = $this ->dbo->qfields( $fields , $this ->fullTableName, $this ->schema);
- if ( $enableLinks ) {
- // 当有关联需要处理时,必须获得主表的主键字段值
- $sql = "SELECT {$distinct} {$this->qpka}, {$fields} FROM {$this->qtableName} {$whereby} {$sortby}" ;
- } else {
- $sql = "SELECT {$distinct} {$fields} FROM {$this->qtableName} {$whereby} {$sortby}" ;
- }
- // 根据 $length 和 $offset 参数决定是否使用限定结果集的查询
- if (null !== $length || null !== $offset ) {
- $result = $this ->dbo->selectLimit( $sql , $length , $offset );
- } else {
- $result = $this ->dbo->execute( $sql );
- }
- if ( $enableLinks ) {
- /**
- * 查询时同时将主键值单独提取出来,
- * 并且准备一个以主键值为键名的二维数组用于关联数据的装配
- */
- $pkvs = array ();
- $assocRowset = null;
- $rowset = $this ->dbo->getAllWithFieldRefs( $result , $this ->pka, $pkvs , $assocRowset );
- $in = 'IN (' . implode( ',' , array_map ( array (& $this ->dbo, 'qstr' ), $pkvs )) . ')' ;
- } else {
- $rowset = $this ->dbo->getAll( $result );
- }
- unset($result );
- // 如果没有关联需要处理或者没有查询结果,则直接返回查询结果
- if (! $enableLinks || empty empty ( $rowset ) || ! $this ->autoLink) {
- return $rowset ;
- }
- /**
- * 遍历每一个关联对象,并从关联对象获取查询语句
- *
- * 查询获得数据后,将关联表的数据和主表数据装配在一起
- */
- $callback = create_function( '& $r, $o, $m' , '$r[$m] = null;' );
- foreach ( $this ->links as $link ) {
- /* @var $link FLEA_Db_TableLink */
- $mn = $link ->mappingName;
- if (! $link ->enabled || ! $link ->linkRead) { continue ; }
- if (! $link ->countOnly) {
- array_walk ( $assocRowset , $callback , $mn );
- $sql = $link ->getFindSQL( $in );
- $this ->dbo->assemble( $sql , $assocRowset , $mn , $link ->oneToOne, $this ->pka, $link ->limit);
- } else {
- $link ->calcCount( $assocRowset , $mn , $in );
- }
- }
- return $rowset ;
- }
/**
* 查询所有符合条件的记录及相关数据,返回一个包含多行记录的二维数组,失败时返回 false
*
* @param mixed $conditions
* @param string $sort
* @param mixed $limit
* @param mixed $fields
* @param mixed $queryLinks
*
* @return array
*/
function & findAll($conditions = null, $sort = null, $limit = null, $fields = '*', $queryLinks = true)
{
list($whereby, $distinct) = $this->getWhere($conditions);
// 处理排序
$sortby = $sort != '' ? " ORDER BY {$sort}" : '';
// 处理 $limit
if (is_array($limit)) {
list($length, $offset) = $limit;
} else {
$length = $limit;
$offset = null;
}
// 构造从主表查询数据的 SQL 语句
$enableLinks = count($this->links) > 0 && $this->autoLink && $queryLinks;
$fields = $this->dbo->qfields($fields, $this->fullTableName, $this->schema);
if ($enableLinks) {
// 当有关联需要处理时,必须获得主表的主键字段值
$sql = "SELECT {$distinct} {$this->qpka}, {$fields} FROM {$this->qtableName} {$whereby} {$sortby}";
} else {
$sql = "SELECT {$distinct} {$fields} FROM {$this->qtableName} {$whereby} {$sortby}";
}
// 根据 $length 和 $offset 参数决定是否使用限定结果集的查询
if (null !== $length || null !== $offset) {
$result = $this->dbo->selectLimit($sql, $length, $offset);
} else {
$result = $this->dbo->execute($sql);
}
if ($enableLinks) {
/**
* 查询时同时将主键值单独提取出来,
* 并且准备一个以主键值为键名的二维数组用于关联数据的装配
*/
$pkvs = array();
$assocRowset = null;
$rowset = $this->dbo->getAllWithFieldRefs($result, $this->pka, $pkvs, $assocRowset);
$in = 'IN (' . implode(',', array_map(array(& $this->dbo, 'qstr'), $pkvs)) . ')';
} else {
$rowset = $this->dbo->getAll($result);
}
unset($result);
// 如果没有关联需要处理或者没有查询结果,则直接返回查询结果
if (!$enableLinks || empty($rowset) || !$this->autoLink) {
return $rowset;
}
/**
* 遍历每一个关联对象,并从关联对象获取查询语句
*
* 查询获得数据后,将关联表的数据和主表数据装配在一起
*/
$callback = create_function('& $r, $o, $m', '$r[$m] = null;');
foreach ($this->links as $link) {
/* @var $link FLEA_Db_TableLink */
$mn = $link->mappingName;
if (!$link->enabled || !$link->linkRead) { continue; }
if (!$link->countOnly) {
array_walk($assocRowset, $callback, $mn);
$sql = $link->getFindSQL($in);
$this->dbo->assemble($sql, $assocRowset, $mn, $link->oneToOne, $this->pka, $link->limit);
} else {
$link->calcCount($assocRowset, $mn, $in);
}
}
return $rowset;
}
findAll各个参数的说明
$conditions = null, 查询条件
通常数组,包含字段名和值
例如
- array ( 'fieldname' => 'value1' , 'fieldnameb' => 'value2' )
array('fieldname' => 'value1','fieldnameb' => 'value2')
$sort = null, 排序
字段以及排序的方式,通常这是一个字串
例如
- 'ID ASC,post_date DESC' //如果只有一个条件可以这样 'ID ASC'
'ID ASC,post_date DESC' //如果只有一个条件可以这样 'ID ASC'
$limit = null, 限定数量
通常可以是一个数字,或者是从多少位开始取多少个结果
- '3,10' //意思是从第10个开始,取3个记录,也可以直接数字3 表示取3条记录
'3,10' //意思是从第10个开始,取3个记录,也可以直接数字3 表示取3条记录
$fields = ‘*’, 需要查询显示的字段,默认全部显示
例如
- array ( 'ID' , 'post_title' , 'post_parent' )
array('ID','post_title','post_parent')
$queryLinks = true
fleaphp函数findAll方法的使用和示例
- $rowsets = $tableposts ->findAll( array ( 'post_type' => 'post' ), 'ID ASC,post_date DESC' , array (10,0), array ( 'ID' , 'post_title' , 'post_parent' ));
- dump($rowsets );
$rowsets = $tableposts->findAll(array('post_type'=>'post'),'ID ASC,post_date DESC',array(10,0),array('ID','post_title','post_parent'));
dump($rowsets);
更多的findAll示例可以查看这篇文章 http://php.dayanmei.com/fleaphp-hasmany-manytomany.htm