Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8840111
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T10:23:27+00:00 2026-06-14T10:23:27+00:00

I’m trying to select data from two tables and show that data in a

  • 0

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)
));
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-14T10:23:29+00:00Added an answer on June 14, 2026 at 10:23 am

    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…

    SELECT t.id_topic, t.topic, COUNT(m.id_messages)
      FROM topics t LEFT JOIN messages m ON t.id_topic = m.id_topic
    GROUP BY t.id_topic
    

    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;

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

    You can make sure the query isn’t complicated by yii’s formatting by pouring it using only three basic properties of CDbCriteria:

    1. $criteria->select is exactly the list of columns you want to select.
    2. $criteria->condition is exactly the WHERE condition, I honestly prefer using a string to an array, since using the string allows me to use the exact condition I put in here.
    3. $criteria->join is attached immediately after the $model->tableName() you specify in the CActiveDataProvider constructor.
    4. $criteria->group is added at the end of the query, you just need to specify the grouping column.

    Also, You can also make sure to set these properties directly, so you actually descompose your query into the CDbCriteria object. For instance:

    SELECT t.id_topic, t.topic, COUNT(m.id_messages)
      FROM topics t LEFT JOIN messages m ON t.id_topic = m.id_topic
    GROUP BY t.id_topic
    

    would be like this

    /*1*/ $criteria->select = 't.id_topic, t.topic, COUNT(m.id_messages)';
    /*2*/ $criteria->condition = 't.topic_title LIKE %'.$get_s.'% OR ...';/* add your conditions here*/
    /*3*/ $criteria->join = 'LEFT JOIN messages m ON t.id_topic = m.id_topic'; //Full join statement here, including the nature of the join.
    /*4*/ $criteria->group = 't.id_topic';
    

    IMPORTANT: take into account that since you pass your Topics classname to the CActiveDataProvider constructor, the table under Topics will be known as t. Any other tables must be specified as well (Pretty much like messages m or messages AS m)in the join condition otherwise you might get a column xxxx is ambiguous warning.

    Don’t pass out the chance of giving an eye to CDbCriteria and CActiveDataProvider for any questions you might have.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I'm trying to select an H1 element which is the second-child in its group
I've tracked down a weird MySQL problem to the two different ways I was
I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out
I am currently running into a problem where an element is coming back from
I'm trying to create an if statement in PHP that prevents a single post
I have a view passing on information from a database: def serve_article(request, id): served_article
I'm trying to convert HTML to plain text. I get many &\#8217; &\#8220; etc.
I am trying to understand how to use SyndicationItem to display feed which is
Basically, what I'm trying to create is a page of div tags, each has

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.