Yii order another table CActiveDataProvider

I am a little puzzled by this. I basically have two tables:

Page:

  • ID
  • name

Points:

  • id-
  • Pageid
  • points

I am looking to get entries from the Page table, and sort them by the number of points that it has in the table of points (field of points)

I currently have:

$dataProvider=new CActiveDataProvider('Page',array(
        'criteria'=>array(
            'condition'=>"active = 1 AND userid IN (".$ids.")",
            'order'=>"???",
        ),

        'pagination'=>array(
            'pageSize'=>30,
        ),
    ));

I just don’t know how to sort it by the value of the Points table for the corresponding record. I set the relation for the Page / Points tables this way:

(in page model)

'pagepoints' => array(self::HAS_ONE, 'Points', 'pageid'),

thank

+3
source share
3 answers

You need to do two things:

  • Add relation pagepointsto withquery criteria part
  • The link to the column you want to sort in orderterms of criteria

, :

$dataProvider = new CActiveDataProvider('Page', array(
    'criteria'=>array(
        'with' => array('pagepoints'),  // #1 
        'condition' => 'active = 1 AND userid IN ('.$ids.')',
        'order' => 'pagepoints.points', // #2
    ),

    'pagination'=>array(
        'pageSize'=>30,
    ),
));

, , , Yii SQL- ( LEFT OUTER JOIN Points), , Page model ( , it pagepoints) . , :

SELECT ... FROM Page ... LEFT OUTER JOIN `Points` `pagepoints` ...

, pagepoints.points: pagepoints - , Points - .

+3

$dataProvider=new CActiveDataProvider('Page',array(
        'criteria'=>array(
            'with'=>array('pagepoints'),
            'condition'=>"active = 1 AND userid IN (".$ids.")",
            'order'=>"t.points DESC",
        ),

        'pagination'=>array(
            'pageSize'=>30,
        ),
    ));
+1

this is the sql you want to generate:

select * from the internal connection points of the page on the page .id = points.page_id order by points.points desc

0
source

All Articles