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 8116911
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T03:58:47+00:00 2026-06-06T03:58:47+00:00

I’m trying to figure out how to build a query using Yii’s CDbCriteria that

  • 0

I’m trying to figure out how to build a query using Yii’s CDbCriteria that would be equivalent to the following:

 SELECT 
        * 
FROM 
        user u 
JOIN ( 
        SELECT * 
        FROM skill_assessment s 
        WHERE s.skill = 'HTML' 
        AND s.score >= 80 
) b ON 
        (u.id = b.userId) 
JOIN ( 
        SELECT * 
        FROM skill_assessment s 
        WHERE s.skill = 'CSS3' 
        AND s.score >= 80
) c ON 
        (u.id = c.userId);
etc...

Here’s what I have so far, that isn’t working:

$criteria = new CDbCriteria();
$criteria->alias = "u";
$criteria->select = "*";
$criteria->join = "JOIN skill_assessment s ON (u.id=s.userId)";
for($i = 0; $i < count($skill_filters); $i++) {
    $criteria->addCondition("s.skill='".$skill_filters[$i]->skill."' AND s.score >= ".$skill_filters[$i]->level);
}

$users = UserModel::model()->findAll($criteria);

Any help would be greatly appreciated.
Thanks in advance.

EDIT:
I was able to build out the sql query as a string and use findAllBySql, which returned the correct UserModels that matched my search criteria, the problem is that I haven’t been able to get it to return the related SkillAssessmentModels. They don’t come back either with the initial query, like this:

$users = UserModel::model()->with('skill_assessments')->findAllBySql($sql);

nor if I get the results like this:

$users = UserModel::model->findAllBySql($sql);
foreach($users as $user)
{
     $user->skill_assessments = $user->getRelated('skill_assessments');
}

Any thoughts on how I can get those related models?
The strange thing is that elsewhere in my application, I CAN get the related models if I do this:

$user = UserModel::model->findByPk($id);
$user->skill_assessments->getRelated('skill_assessments');
  • 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-06T03:58:48+00:00Added an answer on June 6, 2026 at 3:58 am

    Thanks a lot to Boris Belenski!
    What you suggested wasn’t quite what I needed, but it did get me to what I needed. Here is what I ended up with that works:

    $str = "abcdefghijklmnopqrstuvwxyz";
                $idxs = str_split($str);
    
                $criteria = new CDbCriteria();
                $criteria->alias = "u";
                $joins = array();
                for($i = 0; $i < count($skill_filters); $i++)
                {
                    $joins[] = " JOIN (SELECT * FROM skill_assessment s WHERE s.skill = '" . $skill_filters[$i]->skill ."' AND s.score >= " . $skill_filters[$i]->level . ") " . $idxs[$i] . " ON (u.id = " . $idxs[$i] . ".userId) ";
                }
    
                $criteria->join = join(' ', $joins);
                $users = UserModel::model()->findAll($criteria);
                foreach($users as $user)
                {
                    $user->skill_assessments = $user->getRelated('skill_assessments');
                }
    

    The really key part is that $joins array.
    This will allow you to get all the users that match ALL the criteria for row matches in the skill_assessments table.

    Also, I could probably lazily load the skill assessments in the View in a normal view, but I need to get all the skill_assessments for each user now in order to pass them back in an ajax response with the user.

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

Sidebar

Related Questions

I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out
I'm new to using the Perl treebuilder module for HTML parsing and can't figure
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I'm using v2.0 of ClassTextile.php, with the following call: $testimonial_text = $textile->TextileRestricted($_POST['testimonial']); ... and
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
We're building an app, our first using Rails 3, and we're having to build
I'm trying to select an H1 element which is the second-child in its group
I know there's a lot of other questions out there that deal with this
I'm trying to create an if statement in PHP that prevents a single post
I am trying to understand how to use SyndicationItem to display feed which is

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.