如何使用左连接在Yii中进行分页

时间:2015-04-13 12:55:42

标签: php yii pagination

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

这是控制器

        $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)); 

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

3 个答案:

答案 0 :(得分:0)

为什么不使用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/

答案 1 :(得分:0)

缺少" ON" join子句中的关键字。应该是你的情况:" LEFT JOIN Table2 s ON t.id = s.reference"。

答案 2 :(得分:0)

您可以在模型搜索方法中加入如下所示的两个表格。使用:$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']),
        ));