【问题标题】:How can i do pagination in Yii using Left join如何使用左连接在 Yii 中进行分页
【发布时间】:2015-05-17 03:53:12
【问题描述】:

我必须从两个表中获取数据

这里是控制器

        $criteria = new CDbCriteria;
        $count= Table1::model()->count($criteria);
        $criteria->mergeWith(array(
            'join'=>'LEFT JOIN Table2 s t.id = s.reference'               
        ));   
        $pages=new CPagination($count);
        $pages->pageSize=20;
        $pages->applyLimit($criteria);
        $list=Table1::model()->findAll($criteria);
        $this->render('opendatabase',array('list'=>$list, 'pages' =>$pages)); 

但我遇到了错误。我该如何解决这个问题?

【问题讨论】:

    标签: php yii pagination


    【解决方案1】:

    为什么不使用 C SQL 数据提供程序?

    $count=Yii::app()->db->createCommand('SELECT COUNT(*) FROM tbl_user')->queryScalar();
    $sql='SELECT * FROM tbl_user';
    $dataProvider=new CSqlDataProvider($sql, array(
    'totalItemCount'=>$count,
    'sort'=>array(
        'attributes'=>array(
             'id', 'username', 'email',
        ),
    ),
    'pagination'=>array(
        'pageSize'=>10,
    ),
    ));
    
    // $dataProvider->getData() will return a list of arrays.
    

    这是官方网站上的工作示例

    http://www.yiiframework.com/wiki/765/use-cgridview-to-sort-and-filter-csqldataprovider-when-used-with-unrelated-tables-or-complex-queries/

    【讨论】:

    • Left Join 在哪里?
    • 它是一个简单的分页。我想要的是两张表的分页
    • @Sona SELECT COUNT(*) FROM tbl_user
    【解决方案2】:

    连接子句中缺少“ON”关键字。在您的情况下应该是:“LEFT JOIN Table2 s ON t.id = s.reference”。

    【讨论】:

    • 我仍然没有从 Table2 获取数据
    • 您现在收到什么错误信息? (受保护/运行时/application.log)
    【解决方案3】:

    您可以在模型搜索方法中加入如下两个表格。使用:$criteria->join

            $criteria = new CDbCriteria;
            $criteria->distinct = true;
            $criteria->select = 't.*, authItem.displayName as role, p.type as parentTypes';
    
            $criteria->join = 'LEFT JOIN AuthAssignment authAssignment ON authAssignment.userId = t.id
                LEFT JOIN AuthItem authItem ON authItem.name = authAssignment.itemName
                LEFT JOIN Parent p ON p.userId = t.id
                LEFT JOIN Device d ON d.parentId = p.id';
    
            $criteria->compare('t.id', $this->id);
            $criteria->compare('t.username', $this->username, true);
            $criteria->compare('t.password', $this->password, true);
            $criteria->compare('t.tempPass', $this->tempPass, true);
            $criteria->compare('t.pin', $this->pin, true);
            //$criteria->compare('t.type', $this->parentTypes, true);
            $criteria->compare('p.type', $this->parentTypes, true);
            $criteria->compare('t.firstName', $this->firstName, true);
            $criteria->compare('t.lastName', $this->lastName, true);
            $criteria->compare('t.dob', $this->dob, true);
            $criteria->compare('t.gender', $this->gender, true);
            $criteria->compare('t.email', $this->email, true);
            $criteria->compare('t.alternateEmail', $this->alternateEmail, true);
            $criteria->compare('t.mobileNo', $this->mobileNo, true);
            $criteria->compare('t.phoneNo', $this->phoneNo, true);
            $criteria->compare('faxNo', $this->faxNo, true);
            $criteria->compare('address', $this->address, true);
            $criteria->compare('alternateAddress', $this->alternateAddress, true);
            $criteria->compare('startDate', $this->startDate, true);
            $criteria->compare('lastLoginAt', $this->lastLoginAt, true);
            $criteria->compare('countryId', $this->countryId);
    
            if (Yii::app()->user->checkAccess('oDeviceDeviceManageByOrganization')) { //only for a ogranization user
    //            if (Yii::app()->user->organization) // to avoid wrong permission assignment causing issues
                $criteria->addCondition('d.organizationId IN (' . Yii::app()->user->organization->organizationId . ')');
            }
            if (Yii::app()->user->checkAccess('oDeviceDeviceManageByInstitute')) { //only for a institute user
    //            if (Yii::app()->user->institute) // to avoid wrong permission assignment causing issues
                $criteria->addCondition('d.instituteId IN (' . Yii::app()->user->institute->instituteId . ')');
            }
    
            if (TK::isEmpty($this->status))
                $criteria->addCondition('t.status NOT IN (' . AppModel::STATUS_DELETE . ')');
            else
                $criteria->compare('t.status', $this->status, true);
    
            $criteria->addCondition('t.type NOT IN (' . User::TYPE_SYSTEM . ', ' . User::TYPE_STUDENT . ', ' . User::TYPE_ORGANIZATION . ', ' . User::TYPE_TEACHER . ', ' . User::TYPE_INSTITUTE . ')');
            $criteria->compare('t.createdById', $this->createdById);
            $criteria->compare('t.createdAt', $this->createdAt, true);
            $criteria->compare('t.updatedById', $this->updatedById);
            $criteria->compare('t.updatedAt', $this->updatedAt, true);
            //print_r($criteria);
    
            $sort = new CSort();
            $sort->defaultOrder = 't.id ASC';
            $sort->attributes = array(
                'id' => array('asc' => 't.id', 'desc' => 't.id DESC'),
                'lastName' => array('asc' => 't.lastName', 'desc' => 't.lastName DESC'),
                'firstName' => array('asc' => 't.firstName', 'desc' => 't.firstName DESC'),
                'username' => array('asc' => 't.username', 'desc' => 't.username DESC'),
                'email' => array('asc' => 't.email', 'desc' => 't.email DESC'),
                'parentTypes' => array('asc' => 'parentTypes', 'desc' => 'parentTypes DESC'),
                'createdAt' => array('asc' => 't.createdAt', 'desc' => 't.createdAt DESC'),
            );
    
            return new CActiveDataProvider($this, array(
                'sort' => $sort,
                'criteria' => $criteria,
                'pagination' => array('pageSize' => Yii::app()->params['defaultPageSize']),
            ));
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2017-01-19
      • 2014-05-13
      • 2021-11-25
      • 2015-04-13
      • 2015-12-23
      • 2012-05-28
      相关资源
      最近更新 更多