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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T00:52:48+00:00 2026-06-14T00:52:48+00:00

I’m struggling to create the correct query in Yii, I believe I’m making progress

  • 0

I’m struggling to create the correct query in Yii, I believe I’m making progress though.

I need to check a related table and only want to return records that don’t have a record in the related table. This was answered here- Yii determining existence of related models

What is complicating this and I’m unsure how to overcome it, is that multiple users can have records in this related table. Therefore the full requirement is to return records where no related record exists, but only counting records for the logged in users.

The two related objects are as follows-
SurveyQuestion
AnsweredQuestion

SurveyQuestion HAS_MANY AnsweredQuestion

AnsweredQuestion table has the following columns-

id – survey_question_id – user_id

survey_question_id is the foreign key for the SurveyQuestion table.

My approach so far is to try and limit the records to those relevant to the logged in user with the relation definition-

public function relations()
{
    // NOTE: you may need to adjust the relation name and the related
    // class name for the relations automatically generated below.
    return array(
        'survey_answer'=>array(self::HAS_MANY,'SurveyAnswer','survey_question_id'),
        'answered_questions' => array(self::HAS_MANY, 'AnsweredQuestion', 'question_id',
            'condition'=>'answered_questions.user_id = '.Yii::app()->user->id,
            'joinType'=>'LEFT JOIN',
            ),
    );
}

To limit the the query to records in the parent table with no relevant ones in the child table I’ve used a condition in the findAll function like so-

            $questions = SurveyQuestion::model()->with(array(
                                            'survey_answer',
                                            'answered_questions'=>array(

                                                    'select'=>false,

                                                    'joinType'=>'LEFT JOIN',
                                                    'condition'=>'`answered_questions` . `id` is NULL'
                                                    ),))->findAll();

The two pieces of code return no results even when the child table is cleared.

Can anyone spot where I’m going wrong, in approach or execution?

Many thanks,

Nick

Update

As requested, here’s the sql statement that is run. It’s the second Join that is relevant, the first Join collects the multiple choice answers.

SELECT `t`.`id` AS `t0_c0`, `t`.`area_id` AS `t0_c1`,
`t`.`question_text` AS `t0_c2`, `t`.`date_question` AS `t0_c3`,
`survey_answer`.`id` AS `t1_c0`, `survey_answer`.`survey_question_id` AS
`t1_c1`, `survey_answer`.`answer_text` AS `t1_c2`, `survey_answer`.`tag_id`
AS `t1_c3` FROM `tbl_survey_questions` `t`  LEFT OUTER JOIN
`tbl_survey_answers` `survey_answer` ON
(`survey_answer`.`survey_question_id`=`t`.`id`)  LEFT JOIN
`tbl_answered_questions` `answered_questions` ON
(`answered_questions`.`question_id`=`t`.`id`)  WHERE
((answered_questions.user_id = 2) AND (`answered_questions` . `id` is
NULL))
  • 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-14T00:52:49+00:00Added an answer on June 14, 2026 at 12:52 am

    Had a though after I posted the comment about visually running your query.

    I think you need to put the condition for user_id in the on clause of the relation, rather than the condition clause. Because it’s only returning parent rows where the child has NULL id and user_id of 2. This will obviously never happen. But you need it to be in the JOIN criteria. So it should read:

    'answered_questions' => array(self::HAS_MANY, 'AnsweredQuestion', 'question_id',
        'on'=>'answered_questions.user_id = '.Yii::app()->user->id,
        'joinType'=>'LEFT JOIN',
    ),
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

link Im having trouble converting the html entites into html characters, (&# 8217;) i
Basically, what I'm trying to create is a page of div tags, each has
I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
I want use html5's new tag to play a wav file (currently only supported
In my XML file chapters tag has more chapter tag.i need to display chapters
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
I need a function that will clean a strings' special characters. I do NOT
I have thousands of HTML files to process using Groovy/Java and I need to
I'm trying to create an if statement in PHP that prevents a single post
I need to clean up various Word 'smart' characters in user input, including but

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.