yman 2015-06-09 22:19:04 3745次浏览 1条回复 0 0 0
<?php
namespace app\backend\modules\applyadmin\controllers;

use Yii;
use yii\web\Controller;
use app\frontend\models\Apply;
use yii\data\Pagination;

class DefaultController extends Controller
{

function actionIndex(){  
        $query = Apply::find();
        $countQuery = clone $query;
        $pages = new Pagination(['totalCount' =>$countQuery->count(),'pageSize' => '2']);
        $models = $query->offset($pages->offset)
            ->limit($pages->limit)
            ->OrderBy(['status'=>SORT_DESC])
            ->all();
 
       $this->render('index', [
           'models'=>$models,
           'pages'=>$pages,
       ]);  
}  
  

   
}

<?php
use yii\helpers\Html;
use yii\widgets\LinkPager;
use \yii\helpers\Markdown;
use \yii\helpers\Url;
?>
<?php foreach ($models as $val) :?>
<?php echo $val->id;?>
<?php endforeach; ?>
<?php

// 显示分页
echo LinkPager::widget([

'pagination' => $pages,
]);
?>

为什么我的这样输出不了数据 在foreach 外面就能输出数据。

数据格式是

array(2) { [0]=> object(app\frontend\models\Apply)#35 (8) { ["_attributes":"yii\db\BaseActiveRecord":private]=> array(7) { ["id"]=> int(5) ["uid"]=> string(1) "1" ["centent"]=> string(516) "a:20:{s:9:"is_people";s:1:"1";s:7:"company";s:11:"dsadsaddsad";s:7:"license";s:6:"dsadas";s:5:"legal";s:6:"dsadsa";s:6:"number";s:6:"dsadsa";s:8:"business";s:6:"dsadsa";s:7:"address";s:4:"dsad";s:5:"range";s:8:"dsadsdsa";s:4:"tell";s:6:"dsadsa";s:4:"name";s:6:"dsadsa";s:6:"iphone";s:9:"dsadsaads";s:11:"license_img";s:0:"";s:8:"code_img";s:0:"";s:9:"legal_img";s:0:"";s:5:"apply";s:0:"";s:10:"safety_img";s:0:"";s:12:"sanitary_img";s:0:"";s:8:"star_img";s:0:"";s:9:"plate_img";s:0:"";s:5:"agree";a:1:{i:0;s:1:"1";}}" ["is_people"]=> int(0) ["status"]=> int(2) ["created_at"]=> int(0) ["updated_at"]=> int(0) } ["_oldAttributes":"yii\db\BaseActiveRecord":private]=> array(7) { ["id"]=> int(5) ["uid"]=> string(1) "1" ["centent"]=> string(516) "a:20:{s:9:"is_people";s:1:"1";s:7:"company";s:11:"dsadsaddsad";s:7:"license";s:6:"dsadas";s:5:"legal";s:6:"dsadsa";s:6:"number";s:6:"dsadsa";s:8:"business";s:6:"dsadsa";s:7:"address";s:4:"dsad";s:5:"range";s:8:"dsadsdsa";s:4:"tell";s:6:"dsadsa";s:4:"name";s:6:"dsadsa";s:6:"iphone";s:9:"dsadsaads";s:11:"license_img";s:0:"";s:8:"code_img";s:0:"";s:9:"legal_img";s:0:"";s:5:"apply";s:0:"";s:10:"safety_img";s:0:"";s:12:"sanitary_img";s:0:"";s:8:"star_img";s:0:"";s:9:"plate_img";s:0:"";s:5:"agree";a:1:{i:0;s:1:"1";}}" ["is_people"]=> int(0) ["status"]=> int(2) ["created_at"]=> int(0) ["updated_at"]=> int(0) } ["_related":"yii\db\BaseActiveRecord":private]=> array(0) { } ["_errors":"yii\base\Model":private]=> NULL ["_validators":"yii\base\Model":private]=> NULL ["_scenario":"yii\base\Model":private]=> string(7) "default" ["_events":"yii\base\Component":private]=> array(2) { ["beforeInsert"]=> array(1) { [0]=> array(2) { [0]=> array(2) { [0]=> object(yii\behaviors\TimestampBehavior)#39 (5) { ["createdAtAttribute"]=> string(10) "created_at" ["updatedAtAttribute"]=> string(10) "updated_at" ["value"]=> NULL ["attributes"]=> array(2) { ["beforeInsert"]=> array(2) { [0]=> string(10) "created_at" [1]=> string(10) "updated_at" } ["beforeUpdate"]=> string(10) "updated_at" } ["owner"]=> RECURSION } [1]=> string(18) "evaluateAttributes" } [1]=> NULL } } ["beforeUpdate"]=> array(1) { [0]=> array(2) { [0]=> array(2) { [0]=> object(yii\behaviors\TimestampBehavior)#39 (5) { ["createdAtAttribute"]=> string(10) "created_at" ["updatedAtAttribute"]=> string(10) "updated_at" ["value"]=> NULL ["attributes"]=> array(2) { ["beforeInsert"]=> array(2) { [0]=> string(10) "created_at" [1]=> string(10) "updated_at" } ["beforeUpdate"]=> string(10) "updated_at" } ["owner"]=> RECURSION } [1]=> string(18) "evaluateAttributes" } [1]=> NULL } } } ["_behaviors":"yii\base\Component":private]=> array(1) { [0]=> object(yii\behaviors\TimestampBehavior)#39 (5) { ["createdAtAttribute"]=> string(10) "created_at" ["updatedAtAttribute"]=> string(10) "updated_at" ["value"]=> NULL ["attributes"]=> array(2) { ["beforeInsert"]=> array(2) { [0]=> string(10) "created_at" [1]=> string(10) "updated_at" } ["beforeUpdate"]=> string(10) "updated_at" } ["owner"]=> RECURSION } } } [1]=> object(app\frontend\models\Apply)#48 (8) { ["_attributes":"yii\db\BaseActiveRecord":private]=> array(7) { ["id"]=> int(2) ["uid"]=> string(1) "1" ["centent"]=> string(516) "a:20:{s:9:"is_people";s:1:"1";s:7:"company";s:11:"dsadsaddsad";s:7:"license";s:6:"dsadas";s:5:"legal";s:6:"dsadsa";s:6:"number";s:6:"dsadsa";s:8:"business";s:6:"dsadsa";s:7:"address";s:4:"dsad";s:5:"range";s:8:"dsadsdsa";s:4:"tell";s:6:"dsadsa";s:4:"name";s:6:"dsadsa";s:6:"iphone";s:9:"dsadsaads";s:11:"license_img";s:0:"";s:8:"code_img";s:0:"";s:9:"legal_img";s:0:"";s:5:"apply";s:0:"";s:10:"safety_img";s:0:"";s:12:"sanitary_img";s:0:"";s:8:"star_img";s:0:"";s:9:"plate_img";s:0:"";s:5:"agree";a:1:{i:0;s:1:"1";}}" ["is_people"]=> int(0) ["status"]=> int(1) ["created_at"]=> int(0) ["updated_at"]=> int(0) } ["_oldAttributes":"yii\db\BaseActiveRecord":private]=> array(7) { ["id"]=> int(2) ["uid"]=> string(1) "1" ["centent"]=> string(516) "a:20:{s:9:"is_people";s:1:"1";s:7:"company";s:11:"dsadsaddsad";s:7:"license";s:6:"dsadas";s:5:"legal";s:6:"dsadsa";s:6:"number";s:6:"dsadsa";s:8:"business";s:6:"dsadsa";s:7:"address";s:4:"dsad";s:5:"range";s:8:"dsadsdsa";s:4:"tell";s:6:"dsadsa";s:4:"name";s:6:"dsadsa";s:6:"iphone";s:9:"dsadsaads";s:11:"license_img";s:0:"";s:8:"code_img";s:0:"";s:9:"legal_img";s:0:"";s:5:"apply";s:0:"";s:10:"safety_img";s:0:"";s:12:"sanitary_img";s:0:"";s:8:"star_img";s:0:"";s:9:"plate_img";s:0:"";s:5:"agree";a:1:{i:0;s:1:"1";}}" ["is_people"]=> int(0) ["status"]=> int(1) ["created_at"]=> int(0) ["updated_at"]=> int(0) } ["_related":"yii\db\BaseActiveRecord":private]=> array(0) { } ["_errors":"yii\base\Model":private]=> NULL ["_validators":"yii\base\Model":private]=> NULL ["_scenario":"yii\base\Model":private]=> string(7) "default" ["_events":"yii\base\Component":private]=> array(2) { ["beforeInsert"]=> array(1) { [0]=> array(2) { [0]=> array(2) { [0]=> object(yii\behaviors\TimestampBehavior)#49 (5) { ["createdAtAttribute"]=> string(10) "created_at" ["updatedAtAttribute"]=> string(10) "updated_at" ["value"]=> NULL ["attributes"]=> array(2) { ["beforeInsert"]=> array(2) { [0]=> string(10) "created_at" [1]=> string(10) "updated_at" } ["beforeUpdate"]=> string(10) "updated_at" } ["owner"]=> RECURSION } [1]=> string(18) "evaluateAttributes" } [1]=> NULL } } ["beforeUpdate"]=> array(1) { [0]=> array(2) { [0]=> array(2) { [0]=> object(yii\behaviors\TimestampBehavior)#49 (5) { ["createdAtAttribute"]=> string(10) "created_at" ["updatedAtAttribute"]=> string(10) "updated_at" ["value"]=> NULL ["attributes"]=> array(2) { ["beforeInsert"]=> array(2) { [0]=> string(10) "created_at" [1]=> string(10) "updated_at" } ["beforeUpdate"]=> string(10) "updated_at" } ["owner"]=> RECURSION } [1]=> string(18) "evaluateAttributes" } [1]=> NULL } } } ["_behaviors":"yii\base\Component":private]=> array(1) { [0]=> object(yii\behaviors\TimestampBehavior)#49 (5) { ["createdAtAttribute"]=> string(10) "created_at" ["updatedAtAttribute"]=> string(10) "updated_at" ["value"]=> NULL ["attributes"]=> array(2) { ["beforeInsert"]=> array(2) { [0]=> string(10) "created_at" [1]=> string(10) "updated_at" } ["beforeUpdate"]=> string(10) "updated_at" } ["owner"]=> RECURSION } } } }

是不是表有问题还是咋呢 一直纠结

  • 回复于 2015-06-10 08:34 举报

    我把分页方法封装到基类里面 就能正常分页

      /**
         * 
         * @param pageSize:设置每页的大小
         * @param order:数据的排序
         * @param rows:返回的数组中数据对象的键名
         * @param pages:返回的数组中分页对象的键名
         * @return \yii\web\Pagination
         */
        public function getPagedRows($query,$config=[])
        {
            $countQuery = clone $query;
            $pages=new Pagination(['totalCount' => $countQuery->count()]);
            if(isset($config['pageSize']))
            {
                    $pages->setPageSize($config['pageSize'],true);
            }
    
            $rows = $query->offset($pages->offset)->limit($pages->limit);
            if(isset($config['order']))
            {
                    $rows = $rows->orderBy($config['order']);
            }
            $rows = $rows->all();
    
    
            $rowsLable='rows';
            $pagesLable='pages';
    
            if(isset($config['rows']))
            {
                    $rowsLable=$config['rows'];
            }
            if(isset($config['pages']))
            {
                    $pagesLable=$config['pages'];
            }
    
            $ret=[];
            $ret[$rowsLable]=$rows;
            $ret[$pagesLable]=$pages;
    
            return $ret;
    }
    

    求指教

您需要登录后才可以回复。登录 | 立即注册