I’m trying to select data from two tables and show that data in a paginated grid view. The problem is that Yii is joining the data with a SQL join (because I told it to do it) and because of that it’s not showing the correct number of items per page.
To make in clear, I’m selecting from topics and messages_in_topics, and I’m joining then with
$criteria->together = true;
This makes MySQL to return a row for each message (and the related topics). Example:
id_topic topic id_messages message
1 topic1 1 message1_in_topic1
1 topic1 2 message2_in_topic1
1 topic1 3 message3_in_topic1
2 topic2 4 message1_in_topic2
2 topic2 5 message2_in_topic2
There are only 2 topics, but Yii’s paginator thinks there are 5.
The fastest way to “fix” this is grouping by the id_topic field, anyways, I can’t do that because there’s a where condition which searches with the like statement.
Thank you
EDIT:
Here’s my action code:
$criteria = new CDbCriteria;
$get_s = Yii::app()->request->getQuery('s', '');
if( $get_s ){
$criteria->compare("topic_title", $get_s, true);
$criteria->compare("message_text", $get_s, true, 'OR');
}
$criteria->with = array('messages');
$criteria->addCondition(array( ...... )); <--- some rules like if the topic is validated...
$dataProvider = new CActiveDataProvider('Topics', array(
'criteria'=>$criteria,
'pagination=>array('pageSize'=>15)
));
Actually, what happens there is that the information being displayed is in regard to your messages. The repeated topic values are because these topics are the corresponding related data to the message.
You could try to tell your query to use GROUP BY in the results…
This way, more or less, you can display the count of messages-per-topic.
I could help you more if you’d show us your SQL.
EDIT: After seeing your code, here is my guess:
$criteria = new CDbCriteria;
You can make sure the query isn’t complicated by yii’s formatting by pouring it using only three basic properties of CDbCriteria:
Also, You can also make sure to set these properties directly, so you actually descompose your query into the CDbCriteria object. For instance:
would be like this
IMPORTANT: take into account that since you pass your
Topicsclassname to theCActiveDataProviderconstructor, the table underTopicswill be known ast. Any other tables must be specified as well (Pretty much likemessages mormessages AS m)in the join condition otherwise you might get acolumn xxxx is ambiguouswarning.Don’t pass out the chance of giving an eye to CDbCriteria and CActiveDataProvider for any questions you might have.