PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Tuesday, March 8, 2022

[FIXED] YII: How to use "with" and sort at the same time?

 March 08, 2022     php, yii, yii2     No comments   

Issue

I've got a problem on YII relation tables.

As the title says, I just want to combine table "A" with table "B" and sort the combined table according to the "clickTimes" in table "B".

        $A = self::find()
        -> with(['B'=>['order'=>'clickTimes DESC']])
        -> all();

The above is what I've learned from the Internet but it didn't work. The error goes below.

PHP Warning – yii\base\ErrorException
call_user_func() expects parameter 1 to be a valid callback, array must have exactly two members

The relation works fine except for the sorting. Any suggestion? Thank you so much!


Solution

You may use CDbCriteria like this:

$criteria = new CDbCriteria();
$criteria->with = array('foreign_table1', 'foreign_table2',  'foreign_table2.foreign_table3');
$criteria->order = 'foreign_table3.col5 DESC';


Answered By - Jobayer Ahmed
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing