Answer the question
In order to leave comments, you need to log in
Yii: Pagination error when together = true. How to fix?
I've been fighting for the third day. Googled ad nauseam. Kindly point me in the right direction.
Situation: there are users on the site. Users have certain characteristics that are stored in related tables. You need to find users that match certain characteristics.
Problem: shown below in the screenshots
Model Users
public function relations() {
return array(
'usersMotivations' => array(self::HAS_MANY, 'UsersMotivations', 'userID'),
);
}
public function actionIndex() {
$criteria = new CDbCriteria;
$criteria->with = array('usersMotivations');
$criteria->addInCondition('motivationID',array(1,2));
$criteria->together = true;
$dataProvider = new CActiveDataProvider('Users',array(
'criteria'=>$criteria,
'pagination'=>array('pageSize'=>10)
));
$this->render('index', array( 'dataProvider' => $dataProvider));
}
$this->widget('zii.widgets.CListView', array(
'dataProvider'=>$dataProvider,
'itemView'=>'_view',
));
$criteria->addInCondition('motivationID',array(1,2));
Answer the question
In order to leave comments, you need to log in
Try
$criteria->group='t.user_id';
'usersMotivation' => array(self::HAS_ONE, 'UsersMotivations', 'userID'),
And use the same criteria$criteria = new CDbCriteria;
$criteria->with = array('usersMotivation');
$criteria->addInCondition('motivationID',array(1,2));
$criteria->together = true;
This is because of the one-to-many relationship. You need something like this:
public function actionIndex() {
$criteria = new CDbCriteria;
$criteria->addCondition(" (SELECT COUNT(*) FROM `UsersMotivations` WHERE userID = t.ID AND `motivationID` IN (:motivationID) ) > 0 ");
$criteria->params[":motivationID"] = implode(',',array(1,2));
$dataProvider = new CActiveDataProvider('Users',array(
'criteria'=>$criteria,
'pagination'=>array('pageSize'=>10)
));
$this->render('index', array( 'dataProvider' => $dataProvider));
}
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question