fleaphp常用函数findAll方法的使用和示例

fleaphp中的常用函数findAll,主要用与构造sql语句并查询数据,findAll方法的使用和示例

findAll的函数原型是

  1. /**  
  2.      * 查询所有符合条件的记录及相关数据,返回一个包含多行记录的二维数组,失败时返回 false  
  3.      *  
  4.      * @param mixed $conditions  
  5.      * @param string $sort  
  6.      * @param mixed $limit  
  7.      * @param mixed $fields  
  8.      * @param mixed $queryLinks  
  9.      *  
  10.      * @return array  
  11.      */   
  12.     function  & findAll( $conditions  = null,  $sort  = null,  $limit  = null,  $fields  =  '*'$queryLinks  = true)  
  13.     {  
  14.         list($whereby$distinct ) =  $this ->getWhere( $conditions );  
  15.         // 处理排序   
  16.         $sortby  =  $sort  !=  ''  ?  " ORDER BY {$sort}"  :  '' ;  
  17.         // 处理 $limit   
  18.         if  ( is_array ( $limit )) {  
  19.             list($length$offset ) =  $limit ;  
  20.         } else  {  
  21.             $length  =  $limit ;  
  22.             $offset  = null;  
  23.         }  
  24.   
  25.         // 构造从主表查询数据的 SQL 语句   
  26.         $enableLinks  =  count ( $this ->links) > 0 &&  $this ->autoLink &&  $queryLinks ;  
  27.         $fields  =  $this ->dbo->qfields( $fields$this ->fullTableName,  $this ->schema);  
  28.         if  ( $enableLinks ) {  
  29.             // 当有关联需要处理时,必须获得主表的主键字段值   
  30.             $sql  =  "SELECT {$distinct} {$this->qpka}, {$fields} FROM {$this->qtableName} {$whereby} {$sortby}" ;  
  31.         } else  {  
  32.             $sql  =  "SELECT {$distinct} {$fields} FROM {$this->qtableName} {$whereby} {$sortby}" ;  
  33.         }  
  34.   
  35.         // 根据 $length 和 $offset 参数决定是否使用限定结果集的查询   
  36.         if  (null !==  $length  || null !==  $offset ) {  
  37.             $result  =  $this ->dbo->selectLimit( $sql$length$offset );  
  38.         } else  {  
  39.             $result  =  $this ->dbo->execute( $sql );  
  40.         }  
  41.   
  42.         if  ( $enableLinks ) {  
  43.             /**  
  44.              * 查询时同时将主键值单独提取出来,  
  45.              * 并且准备一个以主键值为键名的二维数组用于关联数据的装配  
  46.              */   
  47.             $pkvs  =  array ();  
  48.             $assocRowset  = null;  
  49.             $rowset  =  $this ->dbo->getAllWithFieldRefs( $result$this ->pka,  $pkvs$assocRowset );  
  50.             $in  =  'IN ('  . implode( ','array_map ( array (&  $this ->dbo,  'qstr' ),  $pkvs )) .  ')' ;  
  51.         } else  {  
  52.             $rowset  =  $this ->dbo->getAll( $result );  
  53.         }  
  54.         unset($result );  
  55.   
  56.         // 如果没有关联需要处理或者没有查询结果,则直接返回查询结果   
  57.         if  (! $enableLinks  ||  empty empty ( $rowset ) || ! $this ->autoLink) {  
  58.             return   $rowset ;  
  59.         }  
  60.   
  61.         /**  
  62.          * 遍历每一个关联对象,并从关联对象获取查询语句  
  63.          *  
  64.          * 查询获得数据后,将关联表的数据和主表数据装配在一起  
  65.          */   
  66.         $callback  = create_function( '& $r, $o, $m''$r[$m] = null;' );  
  67.         foreach  ( $this ->links  as   $link ) {  
  68.             /* @var $link FLEA_Db_TableLink */   
  69.             $mn  =  $link ->mappingName;  
  70.             if  (! $link ->enabled || ! $link ->linkRead) {  continue ; }  
  71.             if  (! $link ->countOnly) {  
  72.                 array_walk ( $assocRowset$callback$mn );  
  73.                 $sql  =  $link ->getFindSQL( $in );  
  74.                 $this ->dbo->assemble( $sql$assocRowset$mn$link ->oneToOne,  $this ->pka,  $link ->limit);  
  75.             } else  {  
  76.                 $link ->calcCount( $assocRowset$mn$in );  
  77.             }  
  78.         }  
  79.   
  80.         return   $rowset ;  
  81.     }  
/**
     * 查询所有符合条件的记录及相关数据,返回一个包含多行记录的二维数组,失败时返回 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, 查询条件
通常数组,包含字段名和值
例如

  1. array ( 'fieldname'  =>  'value1' , 'fieldnameb'  =>  'value2' )  
array('fieldname' => 'value1','fieldnameb' => 'value2')

$sort = null, 排序
字段以及排序的方式,通常这是一个字串
例如

  1. 'ID ASC,post_date DESC'   //如果只有一个条件可以这样 'ID ASC'   
'ID ASC,post_date DESC' //如果只有一个条件可以这样 'ID ASC'

$limit = null, 限定数量
通常可以是一个数字,或者是从多少位开始取多少个结果

  1. '3,10'   //意思是从第10个开始,取3个记录,也可以直接数字3 表示取3条记录   
'3,10' //意思是从第10个开始,取3个记录,也可以直接数字3 表示取3条记录

$fields = ‘*’, 需要查询显示的字段,默认全部显示
例如

  1. array ( 'ID' , 'post_title' , 'post_parent' )  
array('ID','post_title','post_parent')

$queryLinks = true

fleaphp函数findAll方法的使用和示例

  1. $rowsets  =  $tableposts ->findAll( array ( 'post_type' => 'post' ), 'ID ASC,post_date DESC' , array (10,0), array ( 'ID' , 'post_title' , 'post_parent' ));  
  2. 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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值